Need BOOLEAN Result from SQL EXISTS Statement without using a WHERE Clause

blarson picture blarson · Jun 15, 2012 · Viewed 25.8k times · Source

Is there any way in a simple query to return a Boolean value using an SQL EXISTS statement without using a WHERE clause?

All of the 2008 R2 SQL Server Books Online examples show another WHERE clause and two tables. Website examples show either a WHERE or an IF-THEN-ELSE in a procedure.

I was hoping to do the following on one table:

EXISTS
(SELECT  cx.id
 FROM fdd.admissions_view as cx  
 WHERE cx.id=1111 and cx.campus='MEXI') 

The SELECT statement works fine and returns the ID. I just want to add EXISTS to return a BOOLEAN, but the syntax above is not valid.

Can I do something like this? If so, what am I missing syntax-wise? If not, what other technique may work?

Please advise. Thanks.

Answer

500 - Internal Server Error picture 500 - Internal Server Error · Jun 15, 2012

How about something like

select case when count(cx.id) > 0 then 1 else 0 end 
 FROM fdd.admissions_view as cx  
 WHERE cx.id=1111 and cx.campus='MEXI'

?