ORA-04082: 及 ORA-06512 异常,table trigger 影响自身修改示例

来源:互联网 发布:淘宝卖家说他补差价 编辑:程序博客网 时间:2024/05/02 15:01

1.示例表创建:

-- Create table
create table TESTTABLE
(
  COL1 NVARCHAR2(4) not null,
  COL2 NVARCHAR2(4)
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );

2.需求:列col2如果没有录入值,使用默认值来替换.(注:不知道什么原因,设置中文的默认值时,始终不成功,所以只好绕路实现.)

实现方式:触发器.

create or replace trigger Test_Trigger
  before insert on testtable FOR EACH ROW 
 
declare
sNameStr nvarchar2(4):='赵';--默认址
begin
  if :new.col2 is null then
     select sNameStr into:new.col2 from dual;
     --update testtable set col2='a' where col1=:new.col1;
  end if;
end Test_Trigger;

3.标题异常.

ORA-04082 触发器LEVEL问题,在非foreach row 触发器中使用了new或old.

ORA-06512 :table trigger中自身变化时再使用SQL语句修改自身记录是不被允许的.可以参照上例中,在变化前,将新变化值替换,而不要去修改已有表记录.

 

原创粉丝点击