2014-09-17 5 views
1

매개 변수가 @NumeroTransferencia int 인 SP가 있습니다. 널이 아닐 때 해당 변수와 일치하는 행을 선택해야합니다. 그러나이 null 때, 나는 H.CodigoTransferencia = null이 나는 다음과 같은 노력이 사람을 반환해야하지만 좋은 결과를 얻을하지 않았다 :SQL Server : null 비교를 사용하는 WHERE 절

SELECT 
* 
FROM RUTEO.HOJADERUTA H 
WHERE 
    (H.CodigoTransferencia IS NULL OR H.CodigoTransferencia = @NumeroTransferencia) 

감사합니다!

답변

1
SELECT 
* 
FROM RUTEO.HOJADERUTA H 
WHERE 
    ((H.CodigoTransferencia IS NULL AND @NumeroTransferencia IS NULL) OR H.CodigoTransferencia = @NumeroTransferencia) 
+0

그것은 잘 작동했습니다 시도 할 수 있습니다 ... 감사합니다! – user1545878

0
SELECT * 
FROM RUTEO.HOJADERUTA H 
WHERE (
      @NumeroTransferencia IS NULL 
      AND H.CodigoTransferencia IS NULL 
     ) 
     OR 
     (
      @NumeroTransferencia IS NOT NULL 
      AND H.CodigoTransferencia = @NumeroTransferencia 
     ) 
0
SELECT 
* 
FROM RUTEO.HOJADERUTA H 
WHERE 
    ((@NumeroTransferencia IS NULL OR H.CodigoTransferencia = @NumeroTransferencia) H.CodigoTransferencia IS NULL) 

This will only perform the search if your variable is not null otherwise it will look for H.CodigoTransferencia is equal to your variable otherwise where H.CodigoTransferencia = null 
1

당신은 내가 생각했던 것보다 이것이 더 쉽게

SELECT 
* 
FROM RUTEO.HOJADERUTA H 
WHERE 
    (@NumeroTransferencia IS NULL 
     AND H.CodigoTransferencia IS NULL) 
OR (@NumeroTransferencia IS NOT NULL 
     AND H.CodigoTransferencia = @NumeroTransferencia)