从另一个表更新一个表时改变触发器表

时间:2018-06-04 14:04:54

标签: oracle plsql

表运行此代码时会发生变异请帮助我哪里出错了?谢谢

`CREATE OR REPLACE TRIGGER test_tri
after update of country ON test_1
for each row
when (new.country = 'SomeCountry')
begin
update test_2 set column_1 = 'Something'
where test_1.id = (Select id from test_1,test_2 where test_1.id=test_2.id) `

2 个答案:

答案 0 :(得分:1)

试图澄清,而不仅仅是一个真正的答案;如果我理解你需要,你的触发器过于复杂,你可能只需要:

CREATE OR REPLACE TRIGGER test_tri
    AFTER UPDATE OF country
    ON test_1
    FOR EACH ROW
    WHEN(new.country = 'SomeCountry')
BEGIN
    UPDATE test_2
       SET column_1    = 'Something'
     WHERE test_2.id = :new.id;
END;

例如:

SQL> select *
  2  from test_2;

        ID COLUMN_1
---------- ----------------
         1 xx

SQL> update test_1
  2  set country = 'SomeCountry';

1 row updated.

SQL> select *
  2  from test_2;

        ID COLUMN_1
---------- ----------------
         1 Something

这适用于这样的结构,没有触发器:

create table test_1 (id number, country varchar2(100));
create table test_2 (id number, column_1 varchar2(100));

如果你有不同的表,其他触发器等,请发布它们。

答案 1 :(得分:0)

变异表定义为正在变化的表。但在处理触发器时,它是一个可以改变的表。

如果您确实需要在SQL上使用表test_1,则必须添加提示pragma autonomous_transaction

像这样:

CREATE OR REPLACE TRIGGER test_tri
after update of country ON test_1
for each row
when (new.country = 'SomeCountry')
 pragma autonomous_transaction;
begin
update test_2 set column_1 = 'Something'
where test_1.id = (Select id from test_1,test_2 where test_1.id=test_2.id)