Multiple SQL SELECT columns with multiple INTO variables

I am converting SQL from Teradata to SQL Server

in teradata, they have the format

SELECT col1, col2 FROM table1 INTO @variable1, @variable2 

In SQL Server, I found

 SET @variable1 = ( SELECT col1 FROM table1 ); 

This allows you to use only one column / variable for each statement. How to assign 2 or more variables with a single SELECT statement?

+55
sql sql-server teradata
Aug 27 '09 at 12:31
source share
2 answers
 SELECT @variable1 = col1, @variable2 = col2 FROM table1 
+112
Aug 27 '09 at 12:32
source share
 SELECT @var = col1, @var2 = col2 FROM Table 

Here is some interesting information about SET / SELECT

  • SET is the ANSI standard for assigning a variable, SELECT is not.
  • SET can assign only one variable at a time, SELECT can do several tasks at once.
  • When assigned from a query, SET can only assign a scalar value. If the query returns multiple values ​​/ rows, then SET will raise an error. CHOOSE will assign one of the values ​​to the variable and hide the fact that several values ​​have been returned (so you probably never know why something is going wrong in another place - have fun troubleshooting this)
  • When assigning from a query, if there is no return value, SET assign NULL, where SELECT will not make an assignment at all (so the variable will not be changed from its previous value)
  • As for the difference in speed - there are direct restrictions between SET and SELECT. However, SELECT’s ability to make multiple assignments in one shot gives it a small speed advantage over SET.
+29
Aug 27 '09 at 12:33
source share



All Articles