SqlShare.com Logo
 
Skip Navigation Links
Home
Video List
Events
Classes
About Us
Login / Register
Subscribe RSS Feed 

Takes Query Plan Analysis and Performance Monitoring to a New Level

SET vs SELECT For Assigning Values to Variables

It's common to see code like [SET @FirstName = 'Andy'] as well as [Select @FirstName = 'Andy]. Which is right? Or better? They both do the job, and in the end - in our opinion - which you choose should be about readability more than any concerns about performance, there just isn't enough difference to matter in most cases. We'll show you how to use both and point a common mistake that can really give you a headache down the road!

Duration:
4 mins 60 secs
Skill Level:
100
Rating:
4.15 out of 5
Publish Date:
October 04, 2010
SET vs SELECT For Assigning Values to Variables You must be logged in to view this video.  
Bookmark and Share
 
1=Poor, 3=Good, 5=Excellent

About the Author

Image of Andy Warren
Andy Warren is a software trainer focusing on SQL Server, a member of the PASS Board of Directors, and a principal in this site - SQLShare.com.

References



Comments
Shane on 10/4/2010
As you mentioned in this video, when setting large lists of variables it's better to use the select method to assign values to variables. Our company has seen some significant performance improvements at least with that aspect of the content seen here. Thanks for sharing! Great info.

WChaster on 10/4/2010
good to know the methods are similar and inter-chagable.

thierry on 10/5/2010
nice info but maybe you could have gone a bit further and do some if/else logic with the result. If you get no values, you are probably comparing null and that's a special case, right?

G Bryant McClellan on 10/5/2010
There is an advantage to using the SET @Variable = (SELECT...). If the SELECT returns multiple rows, the SET statement will throw a 512 error. So if identical code leads to ambiguity later because of a schema change, that method will detect it due to the subquery error.

E2F948EA23 on 10/5/2010
The video ignored the situation where you need to capture multiple pieces of information such as Rowcount and error id after a SQL statement.

Brent Templeton on 10/5/2010
Really like the video series, short snippets, easy to digest.

P Gerb on 10/5/2010
sql was hard to see.

Gabin on 10/5/2010
not very useful , at least i was excepting a benchmark (single loads , parallels loads) to compare (as the title says 'Vs') the efficiency on multiple assignment, big variables,etc.. I did not learn a thing here.. if you do not have time to do the benchmark, at least you could explain that with select we can do iteration...like for example declare @test varchar(max) = '' select top 10 @test = (case @test when '' then EmailAdress else @test + '|' + EmailAdress end) from person.contact print @test Have a good day

Nate on 10/5/2010
Always good to review the basics no matter how experienced you are.

Nate on 10/5/2010
Relax guys there is only so much you can cram in a 5 minute video. I think this video was very informative as it showed how SET and SELECT were different and alike. The title never mentioned doing benchmark comparisons.

Sam on 10/5/2010
The screen is slightly blurred, otherwise I would have given a 5!

Robert Weinstein on 10/5/2010
no video

Andy Warren on 10/6/2010
Thanks for the comments everyone, I'll continue to try to get as much into a lesson as I can, it's hard to know when the bucket is too full or not full enough sometimes.

Nagarajan on 10/6/2010
Nice video. i like it. Thanks

Gabriel Dunn on 10/6/2010
So you think 'SET' is more readable in the first half, but 'SELECT' is more readable in the 2nd half ? I think SET is more intuitive that a variable is being modified. SELECT is so often used when viewing records that it's not immediately obviouse a valuse is changing.

Carla Wilson on 10/6/2010
Are there ways to break the syntax? like: set @Firstname = 'x', @Lastname = 'y'?

Robbie on 10/14/2010
I see these all the time and don't know what technique is the best. Good topic!

Pradeep Ranjan on 10/20/2010
While assigning multiple variables you need to write n no of SET statements for n no of variables. But it can be achieved in one go with SELECT. e.g. DECLARE @A VARCHAR(2), @B VARCHAR(2) SET @A = 1, @B = 2 SELECT @A, @B Above code will through an exception... (comma) What you need to do is... SET @A = 1 SET @B = 2 Now with Select DECLARE @A VARCHAR(2), @B VARCHAR(2) SELECT @A = 1, @B = 2 SELECT @A, @B It is perfectly work. It is totally depends on the situation, means what we want to assign and how many variables are there.

Derek on 11/22/2010
This was very helpful having coming from a background in Oracle SQL

P on 6/9/2011
It's a personal approach



Must Be Logged In
 

How Do I Become a Video Author? |  Newsletter History

Copyright © Fourdeuce, Inc., 2005-2009. All Rights Reserved | Privacy Policy | Terms & Conditions