2011-06-16 100 views
1

獲得更新的值我有一個更新查詢:從UPDATE查詢

UPDATE FKMS_GNST_Transaction_Details 
    SET Received_Quantity=Received_Quantity+(
         CASE 
         WHEN (@int_Updated_Qty)>=(GTD.Quantity-GTD.Received_Quantity) 
     THEN GTD.Quantity-GTD.Received_Quantity 
         ELSE (@int_Updated_Qty) 
         END) 
     ,@int_GNST_Reference_Id=GTD.Transaction_Detail_Id 
    FROM FKMS_GNST_Transaction_Details GTD 
INNER JOIN #tbl_transactions tmp 
    ON tmp.Transaction_id=GTD.Transaction_id 
    AND GTD.Item_id=tmp.Item_id 

我想它被添加到Received_Quantity場的數量。也就是說,如果(@int_Updated_Qty)>=(GTD.Quantity-GTD.Received_Quantity) then GTD.Quantity-GTD.Received_Quantity其他明智的@int_Updated_Qty

我們怎樣才能把這個值(變成一個變量或任何其他方式)?請幫忙。

回答

1

使用OUTPUT clause

UPDATE FKMS_GNST_Transaction_Details 
    SET Received_Quantity=Received_Quantity+(
         CASE 
         WHEN (@int_Updated_Qty)>=(GTD.Quantity-GTD.Received_Quantity) 
     THEN GTD.Quantity-GTD.Received_Quantity 
         ELSE (@int_Updated_Qty) 
         END) 
     ,@int_GNST_Reference_Id=GTD.Transaction_Detail_Id 

--start gbn code 
OUTPUT INSERTED.Received_Quantity 
--end gbn code 

    FROM FKMS_GNST_Transaction_Details GTD 
INNER JOIN #tbl_transactions tmp 
    ON tmp.Transaction_id=GTD.Transaction_id 
    AND GTD.Item_id=tmp.Item_id 

輸出結果可以去

  • 爲表(真實的,臨時或變量)
  • 到客戶端作爲一個記錄

您不能直接分配到本地變量