2010-11-23 5 views
1

나는이 하나 개의 테이블 I 쿼리확인할 매개 변수가 null이거나 SQL Server 2008에서 null이 아닙니다.

declare @a varchar(10) 

    select * from employee where ? 

에 매개 변수를 전달

 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 
관련 문제