开发者

SQL nullable bit in where clause

开发者 https://www.devze.com 2023-03-26 18:22 出处:网络
create procedure [dbo].[MySproc] @Value bit = null as select columna from tablea where columnb = @Value This does not work if I pass in null to the parameter. Obviously when I change the predicate
create procedure [dbo].[MySproc]
@Value bit = null
as

select columna from tablea where columnb = @Value

This does not work if I pass in null to the parameter. Obviously when I change the predicate to columnb is null it works.

What can I do to get this to work without using conditional logic (if/else)?

Update: I figured 开发者_StackOverflow社区it out based on @gbn answer

where (@Value is null and columnb is null) or (columnb = @Value)


Assuming you want true if 1=1, 0=0 or NULL=NULL

select columna from tablea 
where columnb = @Value OR (columnb IS NULL AND @Value IS NULL)

What about 0/1 in column, NULL as parameter. Normally, this would be "just give me the rows"

where columnb = ISNULL(@Value, columnb)

Your logic doesn't make sense because you are using NULL to mean something...

0

精彩评论

暂无评论...
验证码 换一张
取 消