Solved

Difference between SELECT and SET?

Posted on 2000-03-14
6
592 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:Jeremy_D
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 25 total points
ID: 2616253
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
 
LVL 4

Expert Comment

by:mitek
ID: 2616718
i also found out that SELECT @var = 'value' is slightly faster than SET @var = 'value'
0
 
LVL 6

Expert Comment

by:crsankar
ID: 2618715
Select gives an option for assigning a variable with the value of a column in table whereas set do not allow this.
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2618802
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
 
LVL 6

Expert Comment

by:crsankar
ID: 2618855
Ofcourse, when you are assigning a value to a variable you can assign only a single value.
0
 
LVL 4

Author Comment

by:Jeremy_D
ID: 2618887
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question