tags:

views:

350

answers:

3

For example:

declare @bitHaveRows bit
 select @bitHaveRows = count(*)
   from table
  where (predicate)

Are there any functions I can call on this line:

select @bitHaveRows = count(*)

to assign this bit a value of 0 if there are no rows, or 1 if there are one or more rows?

+1  A: 

According to the conversion chart, there's an implicit conversion from int to bit. But if for some reason that doesn't work:

CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END
Joel Coehoorn
That's what I needed. Thanks. I was hoping the implicit conversion would work in that context but apparently not.
hypoxide
A: 

If your database supports it, you can use a CASE statement:

declare @bitHaveRows
select @bitHaveRows = case when count(*) > 0 then 1 else 0 end
from yourtable
Andomar
+1  A: 
declare @bRowsExist
SELECT @bRowsExist = CAST(count(*) as bit)
FROM yourtable

...not sure if it's a better query than the other suggestions

Dan