create or replace trigger trg_t3 after
insert OR update or delete of salary on t2
for each row
begin
if
to_char(systimestamp,'hh24') not between 9 and 17
then
insert into t3 values (:new.salary, :old.salary, sysdate);
else
DBMS_OUTPUT.put_line ('update is not possible between 9:00 and 17:00');
end if;
end;
/
this will insert old salary ,new salary and the time on table t3
based on the conditions mentioned in trigger on table t2
. But i need the name of the employee whose salary is updated or delete.
if i am updating a salary on t2
i need to insert the name of the specific employee whose salary i have modified into t3
. But current method will only insert old salary, new salary and time
here is the code for creating the tables
create table t2 ( name varchar(20), salary varchar2(20));
create table t3 (salary_new varchar2(50), salary_old varchar2(20), log_date date);
insert all
into t2 values('hari',2000)
into t2 values('sam',40000)
into t2 values('ravi',60000)
into t2 values('manoj',8000)
into t2 values('pratheep',10000)
into t2 values('john',3000)
into t2 values('joe',50000)
into t2 values('scott',70000)
select * from dual;
Your trigger is marked as after insert or update or delete
. You have to handle all three situations otherwise it will not work correctly, because when inserting we have no :old
values and when deleting there are no :new
.
So if you use :new.name
then delete
will put null value. If you use :old.name
then insert
will not work correctly. Use variable or do it like here:
create or replace trigger trg_t3 after
insert or update or delete of salary on t2 for each row
begin
if to_char(systimestamp,'hh24') between 9 and 17 then
dbms_output.put_line ('update is not possible between 9:00 and 17:00');
else
if deleting then
insert into t3 (salary_new, salary_old, name, log_date)
values (null, :old.salary, :old.name, sysdate);
elsif inserting then
insert into t3 (salary_new, salary_old, name, log_date)
values (:new.salary, null, :new.name, sysdate);
elsif updating then
insert into t3 (salary_new, salary_old, name, log_date)
values (:new.salary, :old.salary, :new.name, sysdate);
end if;
end if;
end;