2010-11-23 57 views
1

我有一个表 员工在sql server 2008中检查参数为空或不为空?

 Eno  ename image 

     1  aaa  a.jpg 
     2  bbb  b.jpg 
     3  ccc  null 
     4  ddd  null 

我的参数传递给查询

declare @a varchar(10) 

    select * from employee where ? 

?如果我通过图像不是零的意思是我希望所有员工的细节谁有图像其他明智

我希望图像是空的员工细节谁没有图像。

回答

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

不是100%肯定,但我想你想

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

这是短期和甜而不SARGable所以不会表现良好:

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

为了获得更好的性能,我愿意与:

if @a is not null 
    select * 
     from employee 
     where image = @a 
else 
    select * 
     from employee 
     where image is null 
相关问题