更新后在 DML 触发器中调用对象成员函数(ORACLE 数据库)
Call object member function in a DML trigger after update (ORACLE database)
我有一个包含 table PRODUCTS
个对象 Product
的数据库,其中 PRIMARY KEY
是 EAN
字段。每次更新一行我都想调用新对象Product
修改的成员函数。我制作了这个触发器:
CREATE OR REPLACE TRIGGER SHOW_PROFIT_PERCENTAGE
AFTER UPDATE ON PRODUCTS
FOR EACH ROW
DECLARE
P Product;
BEGIN
SELECT VALUE(PS) INTO P
FROM PRODUCTS PS
WHERE EAN = :NEW.EAN;
DBMS_OUTPUT.PUT_LINE('Profit percentage: ' || P.getProfit());
END;
但是当我更新 PRODUCTS
时它给了我这个错误:
table PRODUCTS is mutating, trigger/function may not see it
如我所料。有没有办法做到这一点?谢谢!
您无法从 table 中查找行(对象),因为这会导致您看到并期望看到的变异 table 错误。
可以直接引用受影响的对象,at least from 10g:
You can use the OBJECT_VALUE
pseudocolumn in a trigger on an object table since 10g Release 1 (10.1). OBJECT_VALUE
means the object as a whole. This is one example of its use. You can also call a PL/SQL function with OBJECT_VALUE as the datatype of an IN formal parameter.
所以你可以这样做:
BEGIN
DBMS_OUTPUT.PUT_LINE('Profit percentage: ' || :NEW.OBJECT_VALUE.getProfit());
END;
/
我有一个包含 table PRODUCTS
个对象 Product
的数据库,其中 PRIMARY KEY
是 EAN
字段。每次更新一行我都想调用新对象Product
修改的成员函数。我制作了这个触发器:
CREATE OR REPLACE TRIGGER SHOW_PROFIT_PERCENTAGE
AFTER UPDATE ON PRODUCTS
FOR EACH ROW
DECLARE
P Product;
BEGIN
SELECT VALUE(PS) INTO P
FROM PRODUCTS PS
WHERE EAN = :NEW.EAN;
DBMS_OUTPUT.PUT_LINE('Profit percentage: ' || P.getProfit());
END;
但是当我更新 PRODUCTS
时它给了我这个错误:
table PRODUCTS is mutating, trigger/function may not see it
如我所料。有没有办法做到这一点?谢谢!
您无法从 table 中查找行(对象),因为这会导致您看到并期望看到的变异 table 错误。
可以直接引用受影响的对象,at least from 10g:
You can use the
OBJECT_VALUE
pseudocolumn in a trigger on an object table since 10g Release 1 (10.1).OBJECT_VALUE
means the object as a whole. This is one example of its use. You can also call a PL/SQL function with OBJECT_VALUE as the datatype of an IN formal parameter.
所以你可以这样做:
BEGIN
DBMS_OUTPUT.PUT_LINE('Profit percentage: ' || :NEW.OBJECT_VALUE.getProfit());
END;
/