When to use SET vs SELECT when assigning values to variables in SQL Server
Written By: Atif Shehzad — 11/25/2009 — print — free stuff —
Following are some scenarios when consideration is required in choosing between SET or SELECT. Scripts using the AdventureWorks database are provided for further clarification.
Part 1 and 2 are mentioned in the scripts below. It would be better if you run each part of the script separately so you can see the results for each method.
Returning values through a query
Whenever you are assigning a query returned value to a variable, SET will accept and assign a scalar (single) value from a query. While SELECT could accept multiple returned values. But after accepting multiple values through a SELECT command you have no way to track which value is present in the variable. The last value returned in the list will populate the variable. Because of this situation it may lead to un-expected results as there would be no error or warning generated if multiple values were returned when using SELECT. So, if multiple values could be expected use the SET option with proper implementation of error handling mechanisms.
To further clarify the concept please run script # 1 in two separate parts to see the results
Part 1 of the script should be successful. The variable is populated with a single value through SET. But in part 2 of the script the following error message will be produced and the SET statement will fail to populate the variable when more than one value is returned.
Hence SET prevented assignment of an ambiguous value.
In case of SELECT, even if multiple values are returned by the query, no error will be generated and there will be no way to track that multiple values were returned and which value is present in the variable. This is demonstrated in the following script.
Both part 1 and 2 were executed successfully. In part 2, multiple values have been assigned and accepted, without knowing which value would actually populate the variable. So when retrieval of multiple values is expected then consider the behavioral differences between SET and SELECT and implement proper error handling for these circumstances.
Assigning multiple values to multiple variables
If you have to populate multiple variables, instead of using separate SET statements each time consider using SELECT for populating all variables in a single statement. This can be used for populating variables directly or by selecting values from database.
Consider the following script comparing the use of SELECT and SET.
If you are using SET then each variable would have to be assigned values individually through multiple statements as shown below.
Obviously SELECT is more efficient than SET while assigning values to multiple variables in terms of statements executed, code and network bytes.
What if variable is not populated successfully
If a variable is not successfully populated then behavior for SET and SELECT would be different. Failed assignment may be due to no result returned or any non-compatible value assigned to the variable. In this case, SELECT would preserve the previous value if any, where SET would assign NULL. Because of the difference functionality, both may lead to unexpected results and should be considered carefully.
This is shown in following script
We can see that part 1 generates NULL when no value is returned for populating variable. Where as part 2 produces the previous value that is preserved after failed assignment of the variable. This situation may lead to unexpected results and requires consideration.
Following the standards
Using SELECT may look like a better choice in specific scenarios, but be aware that using SELECT for assigning values to variables is not included in the ANSI standards. If you are following standards for code migration purposes, then avoid using SELECT and use SET instead.
Best practice suggests not to stick to one method. Depending on the scenario you may want to use both SET or SELECT.
Following are few scenarios for using SET
Using SELECT is efficient and flexible in the following few cases.
Readers Who Read This Tip Also Read
Comment or Ask Questions About This Tip