结构时的情况

时间:2015-08-28 15:06:07

标签: sql-server case

为什么这不起作用?

UPDATE [REPORTING].[dbo].[dim_lite_Std_Suite_Upg] 
SET ProductHierarchyID = 
    CASE
        SUBSTRING(ArticleCode, 1, 4) IN ('Live') 
        AND SUBSTRING(ArticleCode,11,4) IN (Select lite from Lite_Ac)
        WHEN SUBSTRING(ArticleCode, 9, 2) = 'St' AND SUBSTRING (BundleArticleCode, 1, 3) IN ('BuS') 
          THEN '244218'
        WHEN SUBSTRING(ArticleCode,9,2) = 'Ed' AND SUBSTRING(BundleArticleCode, 1, 3) IN ('BuS') 
          THEN '244330'
        ELSE ProductHierarchyID
    END 

由于此部分SUBSTRING(ArticleCode,1,4) IN ('Live') AND SUBSTRING(ArticleCode,11,4) IN (Select lite from Lite_Ac)适用于WHEN条件,因此有一种方法可以将它组合起来,以便它适用于条件时。

另一种方法是写:

CASE 
   WHEN SUBSTRING(ArticleCode, 1, 4) IN ('Live') AND SUBSTRING(ArticleCode, 11, 4) IN (SELECT lite FROM Lite_Ac) AND SUBSTRING(ArticleCode, 9, 2) = 'St' AND SUBSTRING(BundleArticleCode, 1, 3) IN ('BuS') 
     THEN '244218'

2 个答案:

答案 0 :(得分:1)

Boolean expression之后你不能拥有CASE。您可以在WHEN之后获得它。

例如这是不正确

CASE A = B
WHEN B = C THEN 1
WHEN A = C THEN 1 
ELSE 0
END

正确表达式为:

CASE 
WHEN A = B AND B = C THEN 1
WHEN A = B AND A = C THEN 1 
ELSE 0
END

答案 1 :(得分:0)

TSQL语言语法不支持您想要的内容。如果它对所有行都是通用的,您可以将它放在WHERE子句中:

UPDATE [REPORTING].[dbo].[dim_lite_Std_Suite_Upg] 
SET ProductHierarchyID = 
    CASE
        WHEN SUBSTRING(ArticleCode, 9, 2) = 'St' THEN '244218'
        WHEN SUBSTRING(ArticleCode,9,2) = 'Ed'   THEN '244330'
        ELSE ProductHierarchyID
    END 
WHERE   SUBSTRING(ArticleCode, 1, 4) IN ('Live')
        AND SUBSTRING(ArticleCode,11,4) IN (Select lite from Lite_Ac)
        AND SUBSTRING (BundleArticleCode, 1, 3) IN ('BuS')