更新SQL Server中的Select Case语句:

时间:2012-05-10 03:47:12

标签: sql-server sql-server-2008 select

我编写了以下更新语句来更新PLN_DU_Vendor表的StatusID。

UPDATE 
PLN_DU_Vendor
SET
    PLN_DU_Vendor.StatusID = 2
FROM
    PLN_DU_Vendor 
INNER JOIN PMT_RFDDetail D ON PLN_DU_Vendor.DUID = D.DUID
INNER JOIN PMT_RFDHeader H ON H.RFDID = D.RFDID AND PLN_DU_Vendor.VendorID = H.VendorID
INNER JOIN PLN_LASCO_Header LH ON LH.LASCOID = H.LASCOID AND LH.ReleaseID = PLN_DU_Vendor.ReleaseID
AND PLN_DU_Vendor.VendorDUQuantity = (D.DespatchQuantity + ISNULL(PLN_DU_Vendor.RFDQuantity, 0))
WHERE
     H.RFDID = 10

如果条件

PLN_DU_Vendor.VendorDUQuantity = (D.DespatchQuantity + ISNULL(PLN_DU_Vendor.RFDQuantity, 0))

失败,我需要将StatusID更新为1.如何在同一更新选择语句中执行此操作。

2 个答案:

答案 0 :(得分:6)

您似乎希望将条件从JOIN移动到作业CASE声明:

UPDATE 
PLN_DU_Vendor
SET
    PLN_DU_Vendor.StatusID = CASE 
    WHEN PLN_DU_Vendor.VendorDUQuantity = (D.DespatchQuantity + ISNULL(PLN_DU_Vendor.RFDQuantity, 0))
    THEN 2
    ELSE 1
    END
FROM
    PLN_DU_Vendor 
INNER JOIN PMT_RFDDetail D ON PLN_DU_Vendor.DUID = D.DUID
INNER JOIN PMT_RFDHeader H ON H.RFDID = D.RFDID AND PLN_DU_Vendor.VendorID = H.VendorID
INNER JOIN PLN_LASCO_Header LH ON LH.LASCOID = H.LASCOID AND LH.ReleaseID = PLN_DU_Vendor.ReleaseID
WHERE
     H.RFDID = 10

答案 1 :(得分:0)

您无法将任何浮点数与运算符=

进行比较
PLN_DU_Vendor.VendorDUQuantity = (D.DespatchQuantity + ISNULL(PLN_DU_Vendor.RFDQuantity, 0))

所以,最好

PLN_DU_Vendor.VendorDUQuantity - (D.DespatchQuantity + ISNULL(PLN_DU_Vendor.RFDQuantity, 0)) < ERROR