我创建了一站表hello,有uname,pwd两个字段,现在想用一个触发器实现在更新hello数据之后,输出表中的所有数据,因为要统计,更新另外一张表。
初学pl/sql,现在写了个触发器,但是出问题了,请大家帮忙看下!
create or replace trigger tri_hello
after update on hello for each row
declare
cursor msgs is select * from hello;
msg msgs%rowtype;
begin
if msgs%isopen then close msgs;end if;
open msgs;
loop
fetch msgs into msg;
exit when msgs%notfound;
dbms_output.put_line(msg.uname||msg.pwd);
end loop;
end;
/
执行
update hello set pwd='sss' where uname='Tom';
时报错如下:
ORA-04091: 表 SCOTT.HELLO 发生了变化, 触发器/函数不能读它
ORA-06512: 在 "SCOTT.TRI_HELLO", line 2
ORA-06512: 在 "SCOTT.TRI_HELLO", line 6
ORA-04088: 触发器 'SCOTT.TRI_HELLO' 执行过程中出错
------解决思路----------------------
在行级触发器中,不能对自己进行查询
这是想达到什么效果呢
------解决思路----------------------
1、在触发器中不能对表本身进行操作;
2、看你的意思是要输出修改后的数据,可通过dbms_output.put_line(:new.uname
------解决思路----------------------
:new.pwd)即可,没有必要用游标;若是要输出所有数据,就必须在应用层来实现。