您的数据模型是一个问题,因为它应该按照Rene的建议进行标准化。但是,考虑到你不能那样做,并且因为你的问题的一部分已经是一个变异的表错误(来自评论)。 和假设您使用的是11g或更高版本,则可以使用复合触发器解决这两个问题。
这是避免变异表错误的方法之一,因为它允许您维护在行级构建的受影响行的列表,然后在语句级使用该集合。
这个想法只是修改了一点也保持跟踪你是否击中触发第二次在你的语句级触发器,你可以用它来避免递归:
让我们先从一个虚表和数据第一:
create table t42 (DEPT number, ID number, ICCODE varchar2(2), OTHER_FIELDS varchar2(10));
insert into t42 (dept, id, iccode, other_fields) values (10, 1, 'FA', 'Data1');
insert into t42 (dept, id, iccode, other_fields) values (20, 2, 'FA', 'Data2');
insert into t42 (dept, id, iccode, other_fields) values (30, 3, 'FA', 'Data3');
insert into t42 (dept, id, iccode, other_fields) values (40, 4, 'XY', 'Data4');
没有触发,更新一行,如:
update t42 set iccode = 'AF' where id = 1;
将只设置单行的价值AF。使用复合触发器来操纵集合,您可以从语句后触发器更新,但这会被递归调用。
所以这里采用dbms_application_info
(或其他机构)看到更新是否从触发器来本身,还是从别的地方:
create or replace trigger test_trigger
for update of iccode on t42
compound trigger
-- collection to hold old and new values
type t_changed_row is record (old_value t42.iccode%type, new_value t42.iccode%type);
type t_changed_rows is table of t_changed_row;
l_changed_rows t_changed_rows := t_changed_rows();
l_fixed_info constant varchar2(30) := 'compound trigger hack';
after each row is
l_info varchar2(30);
begin
dbms_application_info.read_client_info(l_info);
if l_info is null or l_info != l_fixed_info then
-- not in nested update; store old and new values
l_changed_rows.extend;
l_changed_rows(l_changed_rows.count).old_value := :old.iccode;
l_changed_rows(l_changed_rows.count).new_value := :new.iccode;
end if;
end after each row;
after statement is
l_old_info varchar2(30);
begin
-- could check current value here as well but may not be worth it;
-- the collection will be empty anyway on second-level hit
-- store existing value to restore later
dbms_application_info.read_client_info(l_old_info);
-- set info to block recursion
dbms_application_info.set_client_info(l_fixed_info);
-- update table based on all old/new value pairs at once
forall i in 1..l_changed_rows.count
update t42
set iccode = l_changed_rows(i).new_value
where iccode = l_changed_rows(i).old_value;
-- reset info
dbms_application_info.set_client_info(l_old_info);
end after statement;
end test_trigger;
/
而这现在更新所有的匹配值:
update t42 set iccode = 'AF' where id = 1;
1 row updated.
select * from t42;
DEPT ID IC OTHER_FIEL
---------- ---------- -- ----------
10 1 AF Data1
20 2 AF Data2
30 3 AF Data3
40 4 XY Data4
尽管只有一行显然正在更新,但所有的FA值都已更改为AF。
修复数据模型仍然会好得多,但这种方法可能会作为解决您的限制的解决方法。
你当前的触发器是什么样的?你真的遇到了一个死锁,或者一个变异的表错误,这是非常不同的? –
陷入僵局。最初得到了突变表错误,然后我使用了pragma autonomous_transaction;修复它并得到死锁错误。 – Bujji