我正在做一個小項目,複製CVS Pharmacy DB。在PL/SQL中學習觸發器時,我認爲創建一個觸發器是一個好主意,當產品在商店(事務)中購買時,該觸發器將更新商店的產品庫存。PL/SQL觸發器無法讀取表AFTER INSERT
現在爲了做到這一點,我必須在productLine_T上進行AFTER INSERT(在交易中的產品正在購買一定數量的表),存儲transactionID,找到新插入的transaction的employeeID,get該交易的員工的storeID,然後允許我通過storeID獲取相應的庫存,以便通過productLine_T中的orderedQuantity更新該商店的庫存中的productID。
創建這個觸發後,我收到以下錯誤,當我嘗試插入productLine_T,上述:
Error starting at line 193 in command:
INSERT INTO productLine_T(transactionID, productID, productQuantity, productFinalPrice)
VALUES(00000006, 00000001, 2, 1.50)
Error report:
SQL Error: ORA-04091: table BSEWARDS.PRODUCTLINE_T is mutating, trigger/function may not see it
ORA-06512: at "BSEWARDS.ONPRODUCTPURCHASED", line 10
ORA-04088: error during execution of trigger 'BSEWARDS.ONPRODUCTPURCHASED'
04091. 00000 - "table %s.%s is mutating, trigger/function may not see it"
*Cause: A trigger (or a user defined plsql function that is referenced in
this statement) attempted to look at (or modify) a table that was
in the middle of being modified by the statement which fired it.
*Action: Rewrite the trigger (or function) so it does not read that table.
我不明白爲什麼觸發器不能在之後,插入新的價值看?當需要時,我可以包括我的ERD和任何其他需要幫助的視覺表示。
* 觸發:*
create or replace
trigger onProductPurchased
AFTER INSERT on productLine_T
for each row
declare
transQty productLine_T.productQuantity%type;
transID productLine_T.transactionID%type;
prodID productLine_T.productID%type;
empID Employee_T.employeeID%type;
stID Store_T.storeID%type;
--outOfStock exception;
begin
--store new transactionID in transID for query
select transactionID, productID, productQuantity into transID, prodID, transQty
FROM productLine_T
WHERE productLine_T.transactionID = :new.transactionID;
--find store by employee of transactionID to get inventory of store
-- get employeeID of newly inserted transaction
select emp.employeeID into empID FROM Employee_T emp, Transaction_T trans
WHERE trans.transactionID = transID;
-- get storeID from employee at store who made the transaction
select st.storeID into stID FROM Store_T st, Employee_T emp
WHERE emp.employeeID = empID;
--get inventory by storeID and change inventoryQty based on productID quantity
update Inventory_T
set inventoryQuantity = inventoryQuantity - :new.productQuantity
WHERE Inventory_T.storeID = stID AND Inventory_T.storeID = prodID;
end;
閱讀此:http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/triggers.htm#LNPLS759。你可以嘗試複合觸發器:http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/triggers.htm#LNPLS743 – Art
這是不可能的,但沒有複合觸發器呢?這應該是一個相對簡單的觸發器,不應該中斷與變異表.. –