Home > Software engineering >  Trigger (After insert statement)
Trigger (After insert statement)

Time:07-28

create table example1 (id number(3));

create table example2 (name varchar2(20));

create or replace trigger trg1 after insert on example1 begin if (example1.id=1) then insert into example2 values('John'); end if; if (example1.id=2) then insert into example2 values('Denzel'); end if; end;


Guys when I wrote this code this gave me : "PLS-00357: Table,View Or Sequence reference not allowed in this context" error. What's the solution.

I want to create trigger that when i insert into example1 "1" then trigger will insert into example2 "John". Can i do this any code? with my code it's giving error.

CodePudding user response:

Take care to use "for each row" and ":new" when defining the trigger.

create table example1 (id number(3)); 

create table example2 (name varchar2(20)); 

create or replace trigger trg1 after insert on example1 for each row
begin 
  if (:new.id=1) then 
    insert into example2 values('John'); 
  end if; 
  if (:new.id=2) then 
    insert into example2 values('Denzel'); 
  end if; 
end;

insert into example1 values(1);

select * from example2;

Name
----
John
  • Related