日期:2014-05-18  浏览次数:20512 次

请教:带case when的check约束怎么写
a表
returnflag=1时,验证amount=isnull(quantity,0)*isnull(realprice,0)
returnflag=0时,验证costamount=isnull(quantity,0)*isnull(costprice,0)

ALTER TABLE a
ADD CHECK 
(returnflag IN('0','1') and case returnflag when 0 then costamount=isnull(quantity,0)*isnull(costprice,0) when 1 then amount=isnull(quantity,0)*isnull(realprice,0) end )
这样写提示我:第 4 行: '=' 附近有语法错误。

------解决方案--------------------

SQL code

ALTER TABLE a
ADD CHECK 
(
    (returnflag=0 and costamount=isnull(quantity,0)*isnull(costprice,0)) 
 or 
    (returnflag=1 and amount=isnull(quantity,0)*isnull(realprice,0))
)

------解决方案--------------------
探讨

引用:

SQL code
ALTER TABLE a
ADD CHECK
(costamount=
case returnflag
when 0 then isnull(quantity,0)*isnull(costprice,0)
when 1 then isnull(quantity,0)*isnull(realprice,0) end )

额,不是……