2010-11-23 11 views
1

私は、クエリSQL Server 2008で確認するパラメータがnullまたはnullではありませんか。

declare @a varchar(10) 

    select * from employee where ? 

にパラメータを渡す1台 従業員

 Eno  ename image 

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

がありますか?私が合格した画像がヌルではない場合、私は他の人の賢明なイメージを持っているすべての従業員の詳細を望むことを意味する

私はイメージがnullです。

答えて

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 
関連する問題