2012-12-11 4 views
0

t-SQL 프로그래밍을 처음 사용합니다. Java, Javascript 및 기타 여러 프로그래밍 언어에 대한 배경 지식이 있습니다. 변수 @notes의 값을 설정하려고합니다. 내 코드는 아래와 같습니다.CASE 문을 통해 값 설정

기본적으로 @minBatch@maxBatch이 null이 아닌 경우 변수의 값을 @notes으로 설정합니다. @minBatch@maxBatch이 내가 전달하는 값인지 확인하기 위해 print 문을 추가했습니다.

간단히 말해서 다음 코드의 문제점은 무엇입니까?

 
    set @notes = 
      case 
      when @minBatch != null and @maxBatch != null then 
       case 
        when @minBatch != @maxBatch then 
        'Start batch: ' + cast(@minBatch as varchar(8)) + ' End batch: ' + cast(@maxBatch as varchar(8)) 
        else 
        'Batch ' + cast(@minBatch as varchar(8)) 
       end 
      else 
       null 
      end 

답변

2

사용 is null 대신 =null

SELECT @notes = 
    CASE 
     WHEN (@minBatch + @maxBatch) is not null --Comment: if any null result is null 
       THEN 
       CASE @minBatch WHEN @maxBatch [email protected] or @max still can be null 
         THEN 'Batch ' + cast(ISNULL(@minBatch,0) as varchar(8)) 
         ELSE 'Start batch: ' + 
          cast(ISNULL(@minBatch,0) as varchar(8)) + ' End batch: ' + 
          cast(ISNULL(@maxBatch,0) as varchar(8)) 
       END 
     ELSE null 
    END 

편집 :

--Comment: if any null result is null 
+0

아름다운 (@minBatch + @maxBatch) is not null를 사용하여 간소화 된. 당신이 만든 편집 내용이 저에게 효과가 있습니다. 고맙습니다! – acedanger

관련 문제