请教关于 after update触发器的问题。

Spr_Perfei 2010-12-14 03:37:54
有两个表A,B
A
A1
ID

B
B1
ID

请教建一个触发器,当对B表进行修改或插入的时候,在完成操作后,再执行 update b set b.b1=a.a1 where b.id=a.id;


create or replace trigger test_A_B
after insert or update
on B
for each row
begin

-- 执行 update b set b.b1=a.a1 where b.id=a.id;

end;


请教各位大牛。
谢谢。
...全文
411 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
Spr_Perfei 2010-12-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 spr_perfei 的回复:]
引用 1 楼 zhuomingwang 的回复:
SQL code
--这样试试
create or replace trigger test_A_B
after insert or update
on A --如果是修改A表 引发的话 这里用A
for each row
begin
update b set b.b1=:new.a1 where b.id=:new.id;
en……
[/Quote]

谢谢了 , 已经好了,非常感谢。
gelyon 2010-12-15
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 spr_perfei 的回复:]
a.id和b.id是唯一对应的
我是这样写的 只写的update 没写 insert

SQL code

create or replace trigger test_a_b
after update
on B
for each row
begin
update B set B.b1=A.a1 where B.id=A.id and A.id=old.id
end;

……
[/Quote]
我3楼已经给你说了
Spr_Perfei 2010-12-15
  • 打赏
  • 举报
回复
a.id和b.id是唯一对应的
我是这样写的 只写的update 没写 insert

create or replace trigger test_a_b
after update
on B
for each row
begin
update B set B.b1=A.a1 where B.id=A.id and A.id=old.id
end;


报错 ora-00904: A.ID:标识符无效

请教各位高手。

或者 还有什么其它的方法能实现这种效果。
Spr_Perfei 2010-12-15
  • 打赏
  • 举报
回复
我的意思是,
不管对 B 表进行更新还是插入操作,不管把B.b1改成什么值,都要再将b.b1=a.a1,都始终跟a.a1保持相等。
那应该用什么触发器?
minitoy 2010-12-15
  • 打赏
  • 举报
回复
这种操作不能使用after update类型的trigger.
minitoy 2010-12-15
  • 打赏
  • 举报
回复
存在的问题就是表a中id必须唯一,对a中的id做主键吧.
另外就是插入表b时,必须保证插入b的id在a中已经存在.
minitoy 2010-12-15
  • 打赏
  • 举报
回复
create table tablea (a1 number,id number);
create table tableb (b1 number,id number);
insert into tablea
select mod(rownum,3),rownum from dual connect by rownum<=20;

SQL> CREATE OR REPLACE TRIGGER tri_tableb
2 BEFORE INSERT OR UPDATE ON tableb
3 FOR EACH ROW
4 DECLARE
5 v_num NUMBER;
6 BEGIN
7 SELECT a1 INTO v_num FROM tablea t WHERE t.id = :NEW.id;
8 :NEW.b1 := v_num;
9 END;
10 /

Trigger created

SQL> insert into tableb values(5,1);

1 row inserted

SQL> select * from tableb;

B1 ID
---------- ----------
1 1

SQL>
Spr_Perfei 2010-12-15
  • 打赏
  • 举报
回复
就是 当对 B表进行修改或插入操作时,再将B表的b1字段修改为A表的a1字段。

让B.b1 一直等于 A.a1 。

各位老大 帮帮忙啊。。
minitoy 2010-12-14
  • 打赏
  • 举报
回复
没看懂你要什么
lijinsheng2010 2010-12-14
  • 打赏
  • 举报
回复
刚刚写错了,
create or replace trigger tri_a_b
after insert or update
on B
reference old as old new as new
for each row
begin
If inserting then
update b set b.b1=a.a1 where b.id=:new.id and b.id=(select a.id from A where a.id=:new.id);
If updating then
update b set b.b1=a.a1 where b.id=(select a.id from A where a.id=:new.id);
end;
lijinsheng2010 2010-12-14
  • 打赏
  • 举报
回复
create or replace trigger tri_a_b
after insert or update
on B
reference old as old new as new
for each row
begin
If inserting then
update b set b.b1=a.a1 where b.id=:new.id and b.id=a.id;
If updating then
update b set b.b1=a.a1 where b.id=a.id
end;
心中的彩虹 2010-12-14
  • 打赏
  • 举报
回复
[Quote=引用楼主 spr_perfei 的回复:]
有两个表A,B
A
A1
ID

B
B1
ID

请教建一个触发器,当对B表进行修改或插入的时候,在完成操作后,再执行 update b set b.b1=a.a1 where b.id=a.id;


create or replace trigger test_A_B
after insert or update
on B
for each row
……
[/Quote]
你到底是哪个为触发器的事件表
gelyon 2010-12-14
  • 打赏
  • 举报
回复

--你的after触发器是不能修改:new值的,要改成before触发器:
create or replace trigger test_A_B
before insert or update on B
for each row
begin
select a.a1 into :new.b1 where a.id=:new.id and rownum=1;
exception when others then
null;
end;

Spr_Perfei 2010-12-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zhuomingwang 的回复:]
SQL code
--这样试试
create or replace trigger test_A_B
after insert or update
on A --如果是修改A表 引发的话 这里用A
for each row
begin
update b set b.b1=:new.a1 where b.id=:new.id;
end;
[/Quote]



我是这样写的


create or replace trigger test_A_B
after insert or update
on B
for each row
begin
update b set :new.b1=a.a1 where :new.id=a.id;
end;

报错:PL/SQL:ORA-01747:user.table.column,table.column或列说明无效
PL/SQL:SQL Statement ignored

  • 打赏
  • 举报
回复
--这样试试
create or replace trigger test_A_B
after insert or update
on A --如果是修改A表 引发的话 这里用A
for each row
begin
update b set b.b1=:new.a1 where b.id=:new.id;
end;

17,140

社区成员

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

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