Difference between SELECT and SET?

What is the difference (if any) between these two syntaxes:

SET @LocalVar = expression
SELECT @LocalVar = expression

when 'expression' is a non-SQL statement, for instance:

DECLARE @myvar varchar(255)
DECLARE @myint int
SET @myvar = 'First SET; '
SELECT @myvar = @myvar + 'First SELECT; '
SET @myvar = @myvar + 'Second SET; '
SELECT @myvar = @myvar + 'Second SELECT.'
SET @myint = 7
SELECT @myint = @myint * 18
SET @myint = @myint / 63
SELECT @myint = @myint - 2
LVL 4
Jeremy_DAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
SELECT allows you to affect more variables using one statement,
SET allows to affect a cursor to a local variable,
for the rest they are the same
0
 
mitekCommented:
i also found out that SELECT @var = 'value' is slightly faster than SET @var = 'value'
0
 
crsankarCommented:
Select gives an option for assigning a variable with the value of a column in table whereas set do not allow this.
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
crsankar: Try to do this:
SET @var = ( SELECT COLUMN FROM TABLE WHERE .. )
the select must return 1 row or an error occurs, but for the rest it works
0
 
crsankarCommented:
Ofcourse, when you are assigning a value to a variable you can assign only a single value.
0
 
Jeremy_DAuthor Commented:
angelIII: What I expected, thanks for the confirmation.

mitek: Thanks for the tip. Up to now I've used SET for assigning values to variables, and SELECT for SQL-statements, purely for esthetical purposes. I'll do a little test, and if you're right, it's back to using SELECT for all purposes again.



0
All Courses

From novice to tech pro — start learning today.