SQL触发器错误ORA-04098

时间:2018-07-06 14:14:49

标签: sql oracle

我有两个名为childs和parent的表。

create table parent(
    wallet_id NUMBER generated always as identity,
    amount int,
    customer_id int,
    primary key(wallet_id),
    Foreign key (customer_id) REFERENCES childs(customer_id) 
);

create table childs(
    customer_id number generated always as identity,
    name varchar2 (255)
);

现在我要实现的是从childs中获取名称,并为parent表中的childs分配一个wallet_id,因为父表具有customer_id的外键。 为此,我创建了一个触发器。

create or replace 
TRIGGER TRIGGER1 
AFTER INSERT ON CHILDS 
Declare 
id int;
BEGIN
select MAX(customer_id) into id FROM childs;
insert into parent (customer_id ) values ( id );

END;

现在创建触发器,但是当我在childs中插入值时,

  

插入子级(名称)值(“ jarral”);

现在出现以下错误:

  

SQL错误:ORA-04098:触发器'SYSTEM.TRG'无效且重新验证失败   04098. 00000-“触发器'%s。%s'无效且重新验证失败”   *原因:尝试检索触发器以执行该触发器              发现无效。这也意味着编译/授权              触发失败。   *操作:选项用于解决编译/授权错误,              禁用触发器,或放下触发器。

我该如何解决?

1 个答案:

答案 0 :(得分:1)

我不完全确定我会遵循您的问题,但这是您所追求的吗?

SQL> create table t1 ( id number generated as identity , x int);

Table created.

SQL> create table t2 ( id number generated as identity , t1_id int);

Table created.

SQL>
SQL> create or replace
  2  trigger trg
  3  after insert on t1
  4  for each row
  5  begin
  6    insert into t2 (t1_id) values ( :new.id);
  7  end;
  8  /

Trigger created.

SQL> insert into t1 (x) values ( 0);

1 row created.

SQL> select * from t1;

        ID          X
---------- ----------
         1          0

SQL> select * from t2;

        ID      T1_ID
---------- ----------
         1          1