oracle trigger 问题。

[复制链接]
查看11 | 回复1 | 2011-5-17 16:22:59 | 显示全部楼层 |阅读模式
我创建了一个trigger,当我修改表study_sample上面的office_phone,home_phone,mobile_phone,address1,address2,address3 字段时,把study_sample中原来的phone和address记录到表study_sample_info_chg_wk中,创建trigger时显示成功创建,但是当我update study_sample中的phone和address时,就报错了。
trigger代码如下:
CREATE OR REPLACE TRIGGER tr_stu_sam_chg_wk
after update of office_phone,home_phone,mobile_phone,address1,address2,address3 on study_sample
for each row
declare
num number;
begin
select count(*) into num from study_sample where id_type = :old.id_type and id_no = :old.id_no;
if( num = 0 ) then
INSERT INTO study_sample_info_chg_wk

VALUES (:OLD.id_type, :OLD.id_no,

:OLD.address1

|| CHR (13)

|| :OLD.address2

|| CHR (13)

|| :OLD.address3,

:OLD.office_phone

|| CHR (13)

|| :OLD.home_phone

|| CHR (13)

|| :OLD.mobile_phone);
else
update study_sample_info_chg_wk set old_address = :OLD.address1

|| CHR (13)

|| :OLD.address2

|| CHR (13)

|| :OLD.address3, old_phone = :OLD.office_phone

|| CHR (13)

|| :OLD.home_phone

|| CHR (13)

|| :OLD.mobile_phone where id_type = :old.id_type and id_no = :old.id_no;

end if;
end;
报错信息如下:
ORA-04091: table PRODUCTIONDEV.STUDY_SAMPLE is mutating, trigger/function may not see it
ORA-06512: at "PRODUCTIONDEV.TR_STU_SAM_CHG_WK", line 4
ORA-04088: error during execution of trigger 'PRODUCTIONDEV.TR_STU_SAM_CHG_WK'

回复

使用道具 举报

千问 | 2011-5-17 16:22:59 | 显示全部楼层
使用自洽事务:CREATE OR REPLACE TRIGGER tr_stu_sam_chg_wkafter update of office_phone,home_phone,mobile_phone,address1,address2,address3 on study_samplefor each rowdeclarenum number;PRAGMA AUTONOMOUS_TRANSACTION;beginselect count(*) into num from study_sample where id_type = :old.id_type and id_no = :old.id_no;if( num = 0 ) th
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行