加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql – Oracle:不包括用于触发触发器的一列的更新

发布时间:2020-12-12 08:51:19 所属栏目:MsSql教程 来源:网络整理
导读:在oracle中,我可以指定列,这应该引发触发器的触发: create or replace trigger my_triggerbefore update of col1,col2,col3 on my_table for each rowbegin // the trigger code will be executed only if col1 or col2 or col3 was updatedend; 现在我要执
在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;

现在我要执行以下操作:当只更新一列时,我不希望触发器触发.这怎么可能?

我可以列出除了那个之外的所有列,这不应该引发触发器的触发.对于具有许多列的表,这是非常麻烦的.

另一种方法是使用UPDATING函数,如下所示:

if not updating('COL3') then ...

但是如果我一次更改COL1和COL3,则该语句的计算结果为false.这不是我想要的,因为我只是在更新一列(COL3)时限制执行.

解决方法

你可以这样做:
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;

可能不是非常有效率!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读