Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question.

Asked: January 09, 2019 - 4:11 pm UTC

Last updated: January 10, 2019 - 7:15 am UTC

Version: 12.0

Viewed 1000+ times

You Asked

Hi,

I have one requirement on trigger.

On EMP TABLE it has column Status.

If any insert happens then status has to update 1 ,
If any updates done the status 2.

We have to write in one Trigger.

Could you please write a trigger to that requirement.

Thanks in advance.

and Connor said...

SQL> create table t ( c1 int, status int );

Table created.

SQL> create or replace
  2  trigger TRG
  3  before insert or update on T
  4  for each row
  5  begin
  6    if inserting then :new.status := 1; end if;
  7    if updating  then :new.status := 2; end if;
  8  end;
  9  /

Trigger created.

SQL> insert into t (c1) values (10);

1 row created.

SQL> select * from t;

        C1     STATUS
---------- ----------
        10          1

SQL> update t set c1 = 20;

1 row updated.

SQL> select * from t;

        C1     STATUS
---------- ----------
        20          2


You might want to consider that there is also an operation called a MERGE command :-)

Is this answer out of date? If it is, please let us know via a Comment

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library