is it possible to select EXISTS directly as a bit?

后端 未结 9 1168
轻奢々
轻奢々 2020-12-02 09:09

I was wondering if it\'s possible to do something like this (which doesn\'t work):

select cast( (exists(select * from theTable where theColumn like \'theValue

9条回答
  •  执念已碎
    2020-12-02 09:30

    I'm a bit late on the uptake for this; just stumbled across the post. However here's a solution which is more efficient & neat than the selected answer, but should give the same functionality:

    declare @t table (name nvarchar(16))
    declare @b bit
    
    insert @t select N'Simon Byorg' union select N'Roe Bott'
    
    
    select @b = isnull((select top 1 1 from @t where name = N'Simon Byorg'),0)
    select @b whenTrue
    
    select @b = isnull((select top 1 1 from @t where name = N'Anne Droid'),0)
    select @b whenFalse
    

提交回复
热议问题