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!

want to set a var in IF EXISTS subquery...can't

Status
Not open for further replies.

organicg

Programmer
Oct 21, 2002
151
0
0
US
This statement works:
Code:
 If EXISTS(select ReferredByUserID 
			from Referrals
			where ReferredEmail = 'me@you.com')
  PRINT 'YES!!!'

However, I want to retain the value of ReferredByUserID for the next statement. Why won't this work?:
Code:
DECLARE @UserID [int]
 If EXISTS(select @UserID = ReferredByUserID 
			from Referrals
			where ReferredEmail = 'me@you.com')
  PRINT 'YES!!!'
 
I believe the If Exist() statement will only evaluate a true or false. I dont believe it supports holding/setting scope of a variable.

Thats what makes the exists statements so efficient.

Is there any reason why you dont set the variable before then evaluate?


DECLARE @UserID int
select @UserID = ReferredByUserID
from Referrals
where ReferredEmail = 'me@you.com'
if(@UserID is not null)
Print 'YES!!!!'

Bygs



 
Thank you, I knew there was an easy way to do this with only one query.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top