The*_*rff 10 sql oracle triggers plsql
在oracle中我可以指定列,这应该会触发触发器:
create or replace trigger my_trigger
before update of col1, col2, col3 on my_table for each row
begin
// the trigger code will be executed only if col1 or col2 or col3 was updated
end;
Run Code Online (Sandbox Code Playgroud)
现在我想要执行以下操作:当只更新一列时,我不希望触发器触发.这怎么可能?
我可以列出除一个列之外的所有列,这些列不应该触发触发器.对于包含许多列的表来说,这非常麻烦.
另一种方法是使用UPDATING函数,如下所示:
if not updating('COL3') then ...
Run Code Online (Sandbox Code Playgroud)
但是,如果我立刻更改了COL1 和 COL3,则该语句的计算结果为false.这不是我想要的,因为我只想更新一列(COL3)时限制执行.
Ton*_*ews 11
你可以这样做:
create or replace trigger my_trigger
before update on my_table
for each row
declare
n_cols integer := 0;
begin
for r in (select column_name from all_tab_columns
where table_name = 'MY_TABLE'
and owner = 'MY_SCHEMA')
loop
if updating(r.column_name) then
n_cols := n_cols + 1;
exit when n_cols > 1;
end if;
end loop;
if n_cols > 1 then
do_something;
end if;
end;
Run Code Online (Sandbox Code Playgroud)
虽然可能效率不高!
我昨天遇到了同样的问题.我想编写一个触发器,除了一个字段外,每个字段都会触发,该表有103个列.
首先我编码:
if (:OLD.col1<>:NEW.col1 or :OLD.col2<>:NEW.col2 or :OLD.col3<>:NEW.col3 ....)
Run Code Online (Sandbox Code Playgroud)
但我有一些null值的问题,所以我补充说:
if (NVL(:OLD.col1,0)<>NVL(:NEW.col1,0) or NVL(:OLD.col2,0)<>NVL(:NEW.col2,0) ....)
Run Code Online (Sandbox Code Playgroud)
但后来我在DATE列中遇到了一些问题,它变得一团糟......
我认为最好的解决方案是列出要在"OF"中验证的所有列:
AFTER INSERT OR UPDATE of cOL1, col2, col3 ... colN ON table1
Run Code Online (Sandbox Code Playgroud)
它不是"优雅"但是......它完美无缺.
| 归档时间: |
|
| 查看次数: |
23342 次 |
| 最近记录: |