首页 > 解决方案 > 从另一个表更新一个表时改变触发器表

问题描述

运行此代码时表正在变异,请帮助我哪里出错了?谢谢

`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) `

标签: oracleplsql

解决方案


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

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));

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


推荐阅读