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
 Exists() Function

Author  Topic 

gregoryagu
Yak Posting Veteran

80 Posts

Posted - 2008-07-21 : 15:33:33
This does not compile:
Declare @ret int
Select @ret = EXISTS(Select * from aTable))

The only way I have found is like this:

IF (EXISTS (SELECT * FROM aTable ))
SELECT @ret = 1
ELSE
SELECT @ret = 0

but I was trying to not use an IF construct.

Thanks,

Greg

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-07-21 : 16:05:56
That is correct. EXISTS() can be used in boolean expressions only, and in SQL Server boolean expressions and "normal" expressions are two different things and cannot be implicitly converted back and forth.

i.e., you cannot write:

declare @b bit
set @b = (1 > 2)

as in other languages; you must write:

declare @b bit
set @b = case when 1 > 2 then 1 else 0 end


- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

gregoryagu
Yak Posting Veteran

80 Posts

Posted - 2008-07-21 : 16:52:00
OK, I get it now. Thanks.

Greg
Go to Top of Page
   

- Advertisement -