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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assign query result to variable 1

Status
Not open for further replies.

Westond

Programmer
Jan 19, 2005
43
0
0
US
Here is basically what I have:

@in_1
@in_2

if exists(Select x as value from table)
BEGIN
Here I want to be able to say @in_1 = value but it doesn't work? says invalid column name? I also tried using the variable in place of value in the select but that didnt work either.
END;

ELSE
BEGIN

END;

thanks
 
There seems to be some confusion here, so let me try to explain.

Exists checks to see if there are any records in the query. So, if there is 1 record, or a 1000, then exists will return true. When you assign a value to a variable, you'll need to specify a single value (think single field from a single record). Generally, you would see stuff like...

Code:
Declare @in_1 [!]SomeDataType[/!]

if exists(Select x from table)
   BEGIN
      Select @in_1 = x
      From   Table
      Where  Field=Condition
   END
ELSE
  BEGIN

  END

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Or, to ensure a single value
Code:
Select @in_1 = MIN(x)
      From   Table
      Where  Field=Condition

And thread961-1215521


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top