2013-03-07 57 views
0

我已經創建了這個刪節的sql查詢。它需要一個只有2個字符的等級代碼(「GR」,「PH」等),我需要更改查詢以允許在一個實例中返回多個項目。SQL查詢幫助。如何正確獲取條款

這裏的where子句中的代碼

DECLARE @ALEVL_CODE varchar(5), @test varchar(100) 
    SET @ALEVL_CODE = 'GR' 
    SET @test = @ALEVL_CODE + ',' + 'PH' 

- 
        select ALEVL_CODE from PMP_Admissions_Letter_Data AD 
        INNER JOIN  PMP_LM_Review LMR 
    ON    LMR.PMP_Letter_Data_ID = AD.PMP_Admissions_Letter_Data_ID 
    WHERE   LMR.ToPrinter = 1 AND 
        LMR.Extracted = 0 AND 
        AD.ALEVL_CODE = @ALEVL_CODE     
        AND 
        AD.DSCN_MAX_CODE = 'PA' 

我遇到的問題是與線[email protected]_CODE。我想成爲像@ALEVL_CODEGR然後做GRPH其他只是做@ALEVL_CODE。這有意義嗎?一些幫助將不勝感激。

回答

1
  select ALEVL_CODE from PMP_Admissions_Letter_Data AD 
      INNER JOIN  PMP_LM_Review LMR 
ON    LMR.PMP_Letter_Data_ID = AD.PMP_Admissions_Letter_Data_ID 
WHERE   LMR.ToPrinter = 1 AND 
      LMR.Extracted = 0 AND 
      ((@ALEVL_CODE = 'GR' AND AD.ALEVL_CODE in ('GR', 'PH')) 
       OR (@ALEVL_CODE <> 'GR' AND AD.ALEVL_CODE = @ALEVL_CODE))     
      AND 
      AD.DSCN_MAX_CODE = 'PA' 
+0

這看起來可能是問題的解決方案,我只需要有人來運行報告,實際上沒有任何數據那會被拉回來,謝謝! – gmalenko 2013-03-07 18:56:07

0
and case when (@ALEVL_CODE = 'GR') then AD.ALEVL_CODE in ('GR', 'PH') 
     else AD.ALEVL_CODE = @ALEVL_CODE 
    end 
+0

我實際上已經嘗試過這我會得到「關鍵字附近的語法不正確」IN「 – gmalenko 2013-03-07 18:56:34

2
AND (AD.ALEVL_CODE = @ALEVL_CODE 
     OR (@ALEVL_CODE='GR' AND AD.ALEVL_CODE = 'PH')) 

的簡化的替代方案。

+0

這也適用 – gmalenko 2013-03-07 20:20:34