Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Assign SELECT result to variable

Author  Topic 

scmay
Starting Member

22 Posts

Posted - 2007-06-07 : 20:50:57
Hi this is probably a very stupid question, but I still need to know.

How do I set the result of a 'SELECT' statement to a variable? I know I can use CURSOR, but I am certain the SELECT statement will return either 1 record or NULL. Can I use something else apart from CURSOR?

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-06-07 : 21:07:57
[code]
Declare @result int -- or varchar or datetime or whatever the SELECT is returning.
SELECT @result = YourColumn
FROM YourTable
WHERE Conition
SELECT @result
[/code]

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

scmay
Starting Member

22 Posts

Posted - 2007-06-07 : 21:18:24
Just saw this article on SET vs SELECT http://ryanfarley.com/blog/archive/2004/03/01/390.aspx Felt it was worthwhile highlighting this!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-06-08 : 09:48:47
Also refer
http://vyaskn.tripod.com/differences_between_set_and_select.htm

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -