如何在触发器中使用来自更新表的数据?

时间:2019-01-11 23:40:30

标签: sql oracle plsql database-trigger ora-04091

所以我有这两个表:

create table CURRENCY
(
     name VARCHAR2(40 CHAR) PRIMARY KEY,
     value NUMBER(6,2)
);

create table EXCHANGE_RATE
(
     currency1 references CURRENCY(name),
     currency2 references CURRENCY(name),
     price NUMBER(6,2),
     PRIMARY KEY(currency1, currency2)
);

每当我插入新的CURRENCY时,我都想在所有先前的EXCHANGE_RATES与新的CURRIENCIES之间创建新的INSERT INTO CURRENCY VALUES("EURO", 2.0) 。假设我的桌子上只有一种货币:

INSERT INTO CURRENCY VALUES("DOLLAR", 1.0)

现在要插入一个新的

EXCHANGE_RATE

我的目标是插入"EURO", "DOLLAR", 2.0 "DOLLAR", "EURO", 0.5 行:

AFTER INSERT OR UPDATE

最好的方法是什么?我尝试使用CREATE OR REPLACE TRIGGER new_currency AFTER INSERT OR UPDATE ON CURRENCY FOR EACH ROW BEGIN INSERT INTO EXCHANGE_RATE (currency1, currency2, price) SELECT name, :new.name, price/:new.price FROM CURRENCY; INSERT INTO EXCHANGE_RATE (currency1, currency2, price) SELECT :new.name, name, :new.price/price FROM CURRENCY; END; / 触发:

CURRENCY

但是它不起作用,因为我必须查询render() { const { form, errorMessage } = this.props; const { getFieldDecorator, setFieldsValue } = form; return ( <Form> <FormItem> {getFieldDecorator(`slider`, {})( <div> <Slider onAfterChange={wantedValues => setFieldsValue({ slider: wantedValues })} range /> </div>, )} </FormItem> </Form> ); } } export default Form.create({ onValuesChange(props, changedValues, allValues) { console.log('on change', allValues); }, 表,该表正在更新,而我最终得到的是ORA-04091。

正确的方法是什么?

1 个答案:

答案 0 :(得分:0)

您无法查询导致触发器内部触发的表。

如果我是您,我将考虑使用存储过程方法。只需将您的插入脚本移到过程中

CREATE PROCEDURE INS_CURRENCY (p_currency varchar2, p_price number) as
BEGIN
    INSERT INTO CURRENCY VALUES(p_currency , p_Price);

    INSERT INTO EXCHANGE_RATE (currency1, currency2, price)
    SELECT name, p_currency, price/p_price
    FROM CURRENCY
    WHERE NAME != p_current;

    INSERT INTO EXCHANGE_RATE (currency1, currency2, price)
    SELECT  p_currency, name, p_price/price
    FROM CURRENCY
    WHERE name != p_currency;
END;