更新时棘手的SQL IF

时间:2013-04-28 02:07:47

标签: sql

我有这个UPDATE SQL:

UPDATE  o
SET     HolidayPay = lob.AllowHolidayPay ,
        AppreciationPay = lob.AllowAppreciationPay
FROM    Orders AS o
        INNER JOIN LineofBusiness AS lob ON o.LineofBusinessID = lob.LineofBusinessID
WHERE   o.StatusID != 156 -- ALL ORDERS THAT ARE NOT CLOSED
        AND o.TempOrDirect = 'T'
GO

我需要使WHERE子句不仅仅使用statusID。需要发生的是,如果statusID = 156,那么它还需要检查stageID的值。因此,如果statusID = 156,但stageID = 6,那就没问题。如果statusID仍然是156但stageID是任何其他值,则会观察到!=部分。

提前致谢!

2 个答案:

答案 0 :(得分:1)

使用

(o.StatusID != 156 OR (o.StageID=6 and o.StatusID=156))

答案 1 :(得分:1)

所以stageID = 6或StatusID!= 156?

WHERE (o.StatusID != 156 
        OR stageID = 6)
    AND o.TempOrDirect = 'T'