如何修复sql案例语法

时间:2011-12-19 08:19:47

标签: sql-server sql-server-2005

在sql-server-2005上工作

  SELECT    A.specific_customization_id ,
            A.customization_id ,
            A.customization_value ,
            A.customization_price ,
            A.customization_cost ,
            A.customization_code ,
            A.customization_check ,
            A.sort_order ,
            A.option_code ,
            A.product_id ,
            A.image_1 ,
            A.image_2 ,
            A.MainProductID ,
            A.customization_product_id ,
            A.inactive ,
            A.customization_description ,
            A.customization_select ,
            A.UOM ,
            A.allow_recur ,
            auto_reorder = CASE A.MainProductID --it's an int type column
                             WHEN NULL THEN A.allow_recur
                             ELSE ( SELECT  allow_recur
                                    FROM    wc_product
                                    WHERE   product_id = A.MainProductID
                                  )
                           END
  FROM      dbo.wc_product_specific A
  WHERE     A.product_id = 1133

使用上面的查询我想设置auto_reorder列值.want来设置两个登录

  1. 当我的MainProductID列为null时,我想设置allow_recur列值
  2. 当它不为null时,我想设置另一个表列值
  3. 我的逻辑2)工作正常,不明白为什么1)不工作?帮我解决这个问题?

    如果有任何疑问,请提前致谢。

1 个答案:

答案 0 :(得分:1)

这是否有效:?

SELECT    A.specific_customization_id ,
            A.customization_id ,
            A.customization_value ,
            A.customization_price ,
            A.customization_cost ,
            A.customization_code ,
            A.customization_check ,
            A.sort_order ,
            A.option_code ,
            A.product_id ,
            A.image_1 ,
            A.image_2 ,
            A.MainProductID ,
            A.customization_product_id ,
            A.inactive ,
            A.customization_description ,
            A.customization_select ,
            A.UOM ,
            A.allow_recur ,
            A.auto_reorder = CASE  --it's an int type column
                             WHEN A.MainProductID IS NULL THEN A.allow_recur
                             ELSE B.allow_recur
                             END

  FROM      dbo.wc_product_specific A
            left outer join wc_product B on  B.product_id = A.MainProductID
  WHERE     A.product_id = 1133