oracle中,表A中共有a,b,c三个字段,c初始为0,只要a或b发生改变,那么c值变为1

MoCuishle' 2016-07-18 11:58:32
oracle中,表A中共有a,b,c三个字段,c初始为0,只要a或b发生改变,那么c值变为1
表A
a b c
1 1 0
1 2 0
1 3 0
如果
表A数据改变,希望对变化数据添加标识,即c变为1
a b c
1 1 0
2 2 1
3 3 1


各位大神,在oracle 中该怎么实现呢???
...全文
223 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ghx287524027 2016-07-19
  • 打赏
  • 举报
回复
引用 4 楼 wmxcn2000 的回复:
这个不能使用自主事务,也不能使用 commit ,更不能使用update 更新本表。 3# 的语法是对的;
学习了,3Q~
卖水果的net 2016-07-19
  • 打赏
  • 举报
回复
这个不能使用自主事务,也不能使用 commit ,更不能使用update 更新本表。 3# 的语法是对的;
小灰狼W 2016-07-19
  • 打赏
  • 举报
回复
create or replace trigger 触发器名 before update on 表名 for each row when (old.a!=new.a or old.b!=new.b) begin :new.c:=1; end; /
ghx287524027 2016-07-19
  • 打赏
  • 举报
回复
引用 1 楼 ghx287524027 的回复:
利用触发器,思路如下:
create or replace trigger is tri_name 
after update on  tableName
for each row--这个一般都要加上,因为我们一般都是行级触发器,即对每一行都操作
declare  --若没有变量,则可以省略declare

begin
	if :new.a<>:old.a or :new.b<>:old.b then
        update tableName t set t.c=1 where t.a=:old.a and t.b=:old.b;
    end if;
       
end;
少写了一点,需要声明为自定义事务,如下:

create or replace trigger is tri_name 
after update on  tableName
for each row--这个一般都要加上,因为我们一般都是行级触发器,即对每一行都操作
declare  --在变量申明的地方,指定自定义事务处理。
	 pragma autonomous_transaction; 
begin
	if :new.a<>:old.a or :new.b<>:old.b then
        update tableName t set t.c=1 where t.a=:old.a and t.b=:old.b;
		commit;
    end if;       
end;     
ghx287524027 2016-07-19
  • 打赏
  • 举报
回复
利用触发器,思路如下:
create or replace trigger is tri_name 
after update on  tableName
for each row--这个一般都要加上,因为我们一般都是行级触发器,即对每一行都操作
declare  --若没有变量,则可以省略declare

begin
	if :new.a<>:old.a or :new.b<>:old.b then
        update tableName t set t.c=1 where t.a=:old.a and t.b=:old.b;
    end if;
       
end;

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧