上上周,帮别人调试存储过程,触发器中的异常处理没有进行处理。把代码贴出来。
另外触发器一般不能用commit和rollback语句,除非声明事务,加上
create or replace trigger TRG_test
before insert or delete or update on user
for each row
DECLARE
Table_name varchar(255);
begin
select TABLE_NAME into Table_name from user_triggers where TRIGGER_NAME='TRG_test';
begin
update nc set deletetime = to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')
where id=Table_name;
exception
when others then
DBMS_OUTPUT.PUT_LINE('delete');
end;
end if;
if UPDATING then
begin
update nc set updatetime = to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')
where id=Table_name;
exception
when others then
DBMS_OUTPUT.PUT_LINE('update');
end;
end if;
exception
when others then
DBMS_OUTPUT.PUT_LINE('EXCEPTION');
end;
delete或update的时,出现异常时,并没有打印‘delete’或‘update’,不知道为什么,于是加一个参数判断是否存在这条记录,如果不存在再插入,简略的实现
select count(*) into countN from nc where id = Table_name;
if countN >1 then
update……
end if;
。
另外触发器一般不能用commit和rollback语句,除非声明事务,加上
pragma autonomous_transaction;