开发者

parameter to check is null or is not null in sql server 2008?

开发者 https://www.devze.com 2023-01-26 20:07 出处:网络
I have one table Employee Enoenameimage 1aaaa.jpg 2bbbb.jpg 3cccnull 4dddnul开发者_Python百科l I pass the parameter to the query

I have one table Employee

       Eno     ename   image

       1        aaa     a.jpg
       2        bbb     b.jpg
       3        ccc     null
       4        ddd     nul开发者_Python百科l

I pass the parameter to the query

   declare @a varchar(10)

   select * from employee where ?

? if i pass image is not null mean i want all employee details who have image other wise

i want image is null employee details who not have image.


select * 
from employee 
where (@a is null and image is null)
    or (@a is not null and image is not null )


Not 100% sure but I think you want

WHERE     (@a is null AND image is NULL)
   OR (@a is not null AND image is not NULL)


This is short and sweet but not SARGable so it won't perform well:

select *
    from employee
    where coalesce(image,'') = coalesce(@a,'')

For better performance, I'd go with:

if @a is not null
    select *
        from employee
        where image = @a
else
    select *
        from employee
        where image is null
0

精彩评论

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