2015-06-24 181 views
0
SET @SQL = 
     'SELECT 
     CaseStatus = 
      CASE Level1Status 
       WHEN 1100 THEN ''Case Submitted to QC'' 
       WHEN 1200 THEN ''Pending QC'' 
       WHEN 1400 THEN ''Passed QC'' 

      END, 

我目前有以上的動態sql/case語句的問題,因爲我似乎無法把動態SQL字符串內,沒有人碰巧有一個解決方案來解決這個與case語句動態SQL

非常感謝

編輯

AS BEGIN DECLARE @SQL爲nvarchar(4000)

SET @SQL = 
    'SELECT 
    CaseStatus = 
     CASE Level1Status 
      WHEN 1100 THEN ''Case Submitted to QC'' 
      WHEN 1200 THEN ''Pending QC'' 
      WHEN 1400 THEN ''Passed QC'' 

     END, 

     CaseStartDateTime, 
     CaseEndDateTime, 
     StatusName, 
     Cell_NameDescription, 
     QCAnalystName, 
     AnalystName, 
     Upload_Datetime, 
     Requesting_Entity, 
     Legal_Entity_TypeDescription, 
     HighPriorityDescription, 
     DD_Level_RequiredDescription, 
     CountryDscr, 
     Maintable.KYCCaseId AS KYCCaseId 
    FROM 
     UACTc75760ab10784b51b585f082d4b25223 AS MI, 
     UACT175e55161660402692a53a4cdeb89bd6 AS MainTable, 
     UACT5996d6e5151245cab24e4e76e3e53540 AS Statuses, 
     UACTde5f05df6c5f4872a1e57b3cf8368301 AS AddressDetails 
    WHERE 
     (
     MI.CaseStartDateTime BETWEEN ' + quotename(convert(varchar(10), @CaseStartDateTime, 120), '''') + ' AND ' + quotename(convert(varchar(10), @CaseEndDateTime, 120), '''') + 
     ' OR 
     MI.CaseEndDateTime BETWEEN ' + quotename(convert(varchar(10), @CaseStartDateTime, 120), '''') + ' AND ' + quotename(convert(varchar(10), @CaseEndDateTime, 120), '''') + 
     ') 
    AND 
     MI.KYCCase_Id = MainTable.KYCCaseId' 
    IF @StatusName IS NOT NULL AND ltrim(rtrim(@StatusName)) != N'' 
     SET @SQL = @SQL + ' 
     AND 
      Statuses.SourceStatus = MainTable.Level1Status 
     AND 
      Statuses.StatusName = ' + quotename(@StatusName, '''') 
    IF @CountryDscr IS NOT NULL AND ltrim(rtrim(@CountryDscr)) != N'' 
    SET @SQL = @SQL + ' 
     AND 
      AddressDetails.CountryDscr = ' + quotename(@CountryDscr, '''')  
    IF @CellDscr IS NOT NULL AND ltrim(rtrim(@CellDscr)) != N'' 
     SET @SQL = @SQL + ' 
     AND 
      MainTable.Cell_NameDescription = ' + quotename(@CellDscr, '''') 
    IF @QCAnalystName IS NOT NULL AND ltrim(rtrim(@QCAnalystName)) != N'' 
     SET @SQL = @SQL + ' 
     AND 
      MainTable.QCAnalystName = ' + quotename(@QCAnalystName, '''') 
    IF @AnalystName IS NOT NULL AND ltrim(rtrim(@AnalystName)) != N'' 
     SET @SQL = @SQL + ' 
     AND 
      MainTable.AnalystName = ' + quotename(@AnalystName, '''') 
    IF @RequestingEntity IS NOT NULL AND ltrim(rtrim(@RequestingEntity)) != N'' 
     SET @SQL = @SQL + ' 
     AND 
      MainTable.Requesting_Entity = ' + quotename(@RequestingEntity, '''') 
    IF @EntityType IS NOT NULL AND ltrim(rtrim(@EntityType)) != N'' 
     SET @SQL = @SQL + ' 
     AND 
      MainTable.Legal_Entity_TypeDescription = ' + quotename(@EntityType, '''') 
    IF @HighPriority IS NOT NULL AND ltrim(rtrim(@HighPriority)) != N'' 
     SET @SQL = @SQL + ' 
     AND 
      MainTable.HighPriorityDescription = ' + quotename(@HighPriority, '''') 
    IF @DDLevelRequired IS NOT NULL AND ltrim(rtrim(@DDLevelRequired)) != N'' 
     SET @SQL = @SQL + ' 
     AND 
      MainTable.DD_Level_RequiredDescription = ' + quotename(@DDLevelRequired, '''') 

EXEC sp_executesql @SQL 

這是整個代碼中,我得到的錯誤是

消息156,級別15,狀態1,行附近關鍵字「案例」 32 不正確的語法。

+0

請澄清查詢,以便我們瞭解您正在嘗試做什麼。 –

+0

我想創建一個報告的過濾器,如果狀態是1100,1200和1400列被添加到表中以反映字符串'提交的案例','待定QC'和'通過QC',問題是因爲我的動態sql當前設置爲變量@SQL作爲字符串,我無法將字符串'Case Submitted','Pending QC'和'通過QC'添加到case語句 謝謝 – EmperorDev

+0

@EmperorDev它的使用動態SQL與您的代碼也工作得很好。 –

回答

0

這工作正常。

Create Table #Level1Status(Id int identity ,Level1Status int) 

Insert INto #Level1Status(Level1Status) values(1100),(1200),(1400) 


      Declare @SQL Nvarchar(Max) 
      SET @SQL = 
        'SELECT 
        CaseStatus = 
         CASE Level1Status 
          WHEN 1100 THEN ''Case Submitted to QC'' 
          WHEN 1200 THEN ''Pending QC'' 
          WHEN 1400 THEN ''Passed QC'' 

         END from #Level1Status'; 
      Exec SP_ExecuteSQL @SQL 
0

既然你串連你@SQL字符串局部變量「@xxx」,在任何情況下的thease可空,它想給任何輸出在級聯所以使用ISNULL()或者COALESCE()所有地方可變的你使用。