2015-05-18 57 views
0

我有以下SQL語句,在SQL 2008上執行它時,它可以正常工作,沒有問題。但在SQL 2000 excuted當它說:SQL 2000中的嵌套事件

內部SQL Server錯誤

我認爲,問題就來了,因爲在2000年的SQL這裏使用嵌套case語句是SQL語句:

WHEN 14 THEN 
        Case When (select COUNT(*) from [dbo].[mnrFnBI_Fixed](@CurrencyID) f where f.BAccCustID!=0x0 AND f.biMatID = bi.biMatID)>0 THEN 

        CASE (select top 1 PriceType from (select top 1 f.BDate,f.BNumber , tc.PriceType From [dbo].[mnrFnBI_Fixed](@CurrencyID) f inner join #tcust tc on tc.AccID = f.BAccCustID AND tc.PriceType!=0 Where f.biMatID =bi.biMatID AND f.BAccCustID != 0x0 order by f.BDate desc,f.BNumber desc) as k) -- Price From customer Card 
          WHEN 1 THEN 
          CASE @UseUnit WHEN 1 THEN mtEndUser1 
            WHEN 2 THEN mtEndUser2 
            WHEN 3 THEN mtEndUser3 
            WHEN 4 THEN mtEndUser4 
            WHEN 5 THEN mtEndUser5 
            WHEN 0 THEN CASE [mtDefUnit] WHEN 1 THEN mtEndUser1 
                WHEN 2 THEN mtEndUser2 
                WHEN 3 THEN mtEndUser3 
                WHEN 4 THEN mtEndUser4 
                WHEN 5 THEN mtEndUser5 
               END 
            END 
      ---*******************--- 
          WHEN 2 THEN 
          CASE @UseUnit WHEN 1 THEN mtWhole1 
            WHEN 2 THEN mtWhole2 
            WHEN 3 THEN mtWhole3 
            WHEN 4 THEN mtWhole4 
            WHEN 5 THEN mtWhole5 
             WHEN 0 THEN CASE [mtDefUnit] WHEN 1 THEN mtWhole1 
                   WHEN 2 THEN mtWhole2 
                   WHEN 3 THEN mtWhole3 
                   WHEN 4 THEN mtWhole4 
                   WHEN 5 THEN mtWhole5 
                  END 
             END 

        ---*******************---   

        END 
        END 

當我刪除'*****'之間的部分,它完美的工作!

請告訴我case case statement如何與SQL 2000兼容?

更新: 當上面的代碼更改爲:

WHEN 14 THEN 
        Case 
          WHEN (select COUNT(*) from [dbo].[mnrFnBI_Fixed](@CurrencyID) f where f.BAccCustID!=0x0 AND f.biMatID = bi.biMatID)>0 THEN 

       case 
       when (select top 1 PriceType from (select top 1 f.BDate,f.BNumber , tc.PriceType From [dbo].[mnrFnBI_Fixed](@CurrencyID) f inner join #tcust tc on tc.AccID = f.BAccCustID AND tc.PriceType!=0 Where f.biMatID =bi.biMatID AND f.BAccCustID != 0x0 order by f.BDate desc,f.BNumber desc) as k) =1 THEN 
         CASE @UseUnit WHEN 1 THEN mtEndUser1 
           WHEN 2 THEN mtEndUser2 
           WHEN 3 THEN mtEndUser3 
           WHEN 4 THEN mtEndUser4 
           WHEN 5 THEN mtEndUser5 
           WHEN 0 THEN CASE [mtDefUnit] WHEN 1 THEN mtEndUser1 
               WHEN 2 THEN mtEndUser2 
               WHEN 3 THEN mtEndUser3 
               WHEN 4 THEN mtEndUser4 
               WHEN 5 THEN mtEndUser5 
              END 
           END 


       when(select top 1 PriceType from (select top 1 f.BDate,f.BNumber , tc.PriceType From [dbo].[mnrFnBI_Fixed](@CurrencyID) f inner join #tcust tc on tc.AccID = f.BAccCustID AND tc.PriceType!=0 Where f.biMatID =bi.biMatID AND f.BAccCustID != 0x0 order by f.BDate desc,f.BNumber desc) as k) = 2 THEN 
         CASE @UseUnit WHEN 1 THEN mtWhole1 
           WHEN 2 THEN mtWhole2 
           WHEN 3 THEN mtWhole3 
           WHEN 4 THEN mtWhole4 
           WHEN 5 THEN mtWhole5 
            WHEN 0 THEN CASE [mtDefUnit] WHEN 1 THEN mtWhole1 
                  WHEN 2 THEN mtWhole2 
                  WHEN 3 THEN mtWhole3 
                  WHEN 4 THEN mtWhole4 
                  WHEN 5 THEN mtWhole5 
                 END 
            END 


       END 
       END 

它的工作原理,所以請告訴什麼是錯與第一SQL命令在SQL 2000?!

+0

你可能要考慮'COALESCE(NULLIF(@ UseUnit,0),mtDefUnit)'來減少嵌套/重複的量(當然,如果你可以改變你的設置,使一個''NULL' @ UseUnit'表示「回退到默認值」選項,那麼你可以消除上面的'NULLIF') –

+0

感謝您的提示,我注意到,當我刪除第二個選擇並將其替換爲其他條件,那麼它的工作原理!所以可能是由第二個select語句引起的 –

回答

0

這個例子可以幫助你。

UPDATE table_name 
    SET column_name=CASE 
WHEN column_name in ('value1', 'value2',.....) 
    THEN 'update_value' 
WHEN column_name in ('value1', 'value2',.....) 
    THEN 'update_value' 
END