Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning multiple columns in a SELECT statement in a procedure 1

Status
Not open for further replies.

DavidPike

Programmer
Oct 9, 2007
18
What is the syntax (if any) to set multiple variables from a sengle SELECT statement in a stored procedure. For instance, I might:
@WorkVar = (SELECT columnX FROM tableX WHERE condition)
and get a single variable filled. Is there a syntax for bringing back multiple columns into multiple variables with a single SELECT statement? Without using a CURSOR?
 
Note there is a difference between the two syntaxes:

In the first "select @var = (<query>)" the variable will always be set to a new value, null if the query returns no rows.

In the second "select @var1 = val1, @var2=val2 from blah' the two variables will be left at their values before the select statement if no rows are found that match the criteria.

So be careful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top