SET versus SELECT when assigning variables?
Quote, which summarizes from this article:
- SET is the ANSI standard for variable assignment, SELECT is not.
- SET can only assign one variable at a time, SELECT can make multiple assignments at once.
- 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)
- 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)
- 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
- SET is the ANSI standard for variable assignment, SELECT is not.
- SET can only assign one variable at a time, SELECT can make multiple assignments at once.
- 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)
- 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)
- 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
How to Combine Multiple Rows into a Comma-Delimited List in Oracle
Index For Finding an Element in a Json Array
MySQL Error:: 'Access Denied For User 'Root'@'Localhost'
Auto Increment After Delete in MySQL
SQL Statement Is Ignoring Where Parameter
Error 1452: Cannot Add or Update a Child Row: a Foreign Key Constraint Fails
Select N Random Rows from SQL Server Table
Get Top Results For Each Group (In Oracle)
What's the Difference Between Inner Join, Left Join, Right Join and Full Join
Explicit VS Implicit SQL Joins
How to Escape a Single Quote in SQL Server
How Does Database Indexing Work