kennedy
03-08-2008, 02:58
Yêu cầu:
-Field TotalAmount trong bảng InventoryTransactions tự động tính toán theo công thức QuantityReceived*UnitPrice+FreightCharge
Thực hiện:
-Mình viết 1 function tự động tính toán:
CREATE FUNCTION fnTotalAmount (@QuantityReceived int)
RETURNS money
AS
BEGIN
DECLARE @TotalAmount money
SET @TotalAmount=(@QuantityReceived*mUnitPrice)+mFreig htCharge
FROM InventoryTransactions JOIN Items ON InventoryTransactions.cItemId=Items.cItemId
JOIN PurchaseOrders ON PurchaseOrders.cPurchaseOrderId=InventoryTransacti ons.cPurchaseOrderId
RETURN @TotalAmount
END
-Sau đó mình tạo bảng InventoryTransactions và set field TotalAmount như sau:
mTotalAmount AS (dbo.fnTotalAmount (iQuantityReceived))
Kết quả: Insert record vào bảng InventoryTransactions
Insert record đầu tiên với UnitPrice=50: kết quả chính xác
Insert record tiếp theo với UnitPrice=100: Kết quả record thứ 2 chính xác, nhưng kết quả ở record đầu tiên bị thay đổi theo UnitPrice=100)
Insert record thứ ba với UnitPrice=50: Kết quả record thứ 3 chính xác, Kết quả record đầu tiên chính xác trở lại(vì tính theo UnitPrice=50 như ban đầu), Nhưng kết quả thứ 2 lại bị thay đổi theo UnitPrice=50
Nói chung: khi mình Insert thêm 1 record thì toàn bộ giá trị trong field TotalAmount đều bị thay đổi theo UnitPrice mới Insert vào.
Các bạn có cách nào sửa được lỗi trên thì xin chỉ giúp mình với! Hay là làm cách nào khác? Phải dùng đến trigger hả các bạn? Thanks!
-Field TotalAmount trong bảng InventoryTransactions tự động tính toán theo công thức QuantityReceived*UnitPrice+FreightCharge
Thực hiện:
-Mình viết 1 function tự động tính toán:
CREATE FUNCTION fnTotalAmount (@QuantityReceived int)
RETURNS money
AS
BEGIN
DECLARE @TotalAmount money
SET @TotalAmount=(@QuantityReceived*mUnitPrice)+mFreig htCharge
FROM InventoryTransactions JOIN Items ON InventoryTransactions.cItemId=Items.cItemId
JOIN PurchaseOrders ON PurchaseOrders.cPurchaseOrderId=InventoryTransacti ons.cPurchaseOrderId
RETURN @TotalAmount
END
-Sau đó mình tạo bảng InventoryTransactions và set field TotalAmount như sau:
mTotalAmount AS (dbo.fnTotalAmount (iQuantityReceived))
Kết quả: Insert record vào bảng InventoryTransactions
Insert record đầu tiên với UnitPrice=50: kết quả chính xác
Insert record tiếp theo với UnitPrice=100: Kết quả record thứ 2 chính xác, nhưng kết quả ở record đầu tiên bị thay đổi theo UnitPrice=100)
Insert record thứ ba với UnitPrice=50: Kết quả record thứ 3 chính xác, Kết quả record đầu tiên chính xác trở lại(vì tính theo UnitPrice=50 như ban đầu), Nhưng kết quả thứ 2 lại bị thay đổi theo UnitPrice=50
Nói chung: khi mình Insert thêm 1 record thì toàn bộ giá trị trong field TotalAmount đều bị thay đổi theo UnitPrice mới Insert vào.
Các bạn có cách nào sửa được lỗi trên thì xin chỉ giúp mình với! Hay là làm cách nào khác? Phải dùng đến trigger hả các bạn? Thanks!