Set Versus Select When Assigning Variables

SET versus SELECT when assigning variables?

Quote, which summarizes from this article:

  1. SET is the ANSI standard for variable assignment, SELECT is not.
  2. SET can only assign one variable at a time, SELECT can make multiple assignments at once.
  3. If assigning from a query, SET can only assign a scalar value. If the query returns multiple values/rows then SET will raise an error. SELECT will assign one of the values to the variable and hide the fact that multiple values were returned (so you'd likely never know why something was going wrong elsewhere - have fun troubleshooting that one)
  4. When assigning from a query if there is no value returned then SET will assign NULL, where SELECT will not make the assignment at all (so the variable will not be changed from its previous value)
  5. As far as speed differences - there are no direct differences between SET and SELECT. However SELECT's ability to make multiple assignments in one shot does give it a slight speed advantage over SET.

SET vs. SELECT - What's the difference?

SET is the ANSI standard way of assigning values to variables, and SELECT is not. But you can use SELECT to assign values to more than one variable at a time. SET allows you to assign data to only one variable at a time. So that in performance is where SELECT will be a winner.

For more detail and examples refer to: Difference between SET and SELECT when assigning values to variables

Assigning variable from select statement

Difference between set vs select

  1. SET is the ANSI standard for variable assignment, SELECT is not.
  2. SET can only assign one variable at a time, SELECT can make multiple assignments at once.
  3. If assigning from a query, SET can only assign a scalar value. If the query returns multiple values/rows then SET will raise an error. SELECT will assign one of the values to the variable and hide the fact that multiple values were returned (so you'd likely never know why something was going wrong elsewhere - have fun troubleshooting that one)
  4. When assigning from a query if there is no value returned then SET will assign NULL, where SELECT will not make the assignment at all (so the variable will not be changed from its previous value)
  5. As far as speed differences - there are no direct differences between SET and SELECT. However SELECT's ability to make multiple assignments in one shot does give it a slight speed advantage over SET.

Multiple variables assignment using SET and SELECT

Set only assigns one value at a time.

You should use

   SELECT @cfMitt = CfMittente, 
@cfDest = CfDestinatario
FROM Messaggi
WHERE IDMessaggio = @IDMessaggio

SQL Set variable to select result

You can try something like

(declare variables first...)

SELECT TOP 1 @var1=col1, @var2=col2, @var3=col3, [...] FROM YourTable WHERE YourFilter

EDIT: All together this seems not to be the best approach... With SQL you should not think in values and single rows but rather in result sets (set based programming). Your thinking leads to many tiny selects, while loops, cursors and all this stuff one should avoid.

Select and assign to variable in one statement?

Unfortunately it has to be done in two operations.

Test:

DECLARE @VAR DATETIME
SELECT @VAR=GETDATE(), GETDATE()

Yields error message 141.

Here's another SO post on this.

How to declare variable from WITH ... SELECT statement

SELECT @WorkingDays = COUNT(DateData) ...



Related Topics



Leave a reply



Submit