Oracle 从一个表更新另一个表时对触发器表进行变异
运行此代码时,表正在发生变化请帮助我哪里出错?多谢各位Oracle 从一个表更新另一个表时对触发器表进行变异,oracle,plsql,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 tes
`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) `
试图澄清,而不是真正的答案;如果我很了解您的需要,您的触发器过于复杂,您可能只需要:
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));
如果您有不同的表、其他触发器等,请发布它们。突变表定义为正在更改的表。但在处理触发器时,它是一个有可能改变的表 如果确实需要在SQL上使用表test_1,则必须添加提示pragma autonomy_事务 像这样:
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)
你试过搜索吗?关于类似的问题有很多问题,但是“什么时候(new.country='SomeCountry')会使所有问题变得不同在测试2上有触发因素吗?他们可能正在更新测试_1吗?您尚未显示完整的代码,该代码将告诉我们您在update语句中尝试执行的操作。此触发器将不会编译。使用Oracle DB时,使用触发器的最佳实践是。。。不要使用它们(当您想要实现业务逻辑时)。使用DML程序创建特定的包,使用它们并避免触发头痛。无论因为我更新了测试中的不同列,列id都将是新的?我无法理解这一点。请发布您的完整表结构、一些示例数据、您所做的更新查询以及您期望的结果。这将有助于人们理解问题。我尝试了这段代码,并向您发送了反馈,效果非常好,这解决了变异表“WHERE test_2.id=:new.id;”的问题