2012-05-10 199 views
2

我寫了下面的更新語句來更新PLN_DU_Vendor表的StatusID。在SQL Server中更新Select Case語句:

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.如何在相同的更新SELECT語句做到這一點。

+0

什麼是「PLN_DU_Vendor.VendorDUQuantity」的數據類型? –

+0

VendorDUQuantity,DespatchQuantity和RFDQuantity的數據類型爲十進制(15,3) – thevan

回答

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 
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