0

Currently we have a trigger on tbl_number. It is as follows,

create or replace TRIGGER TRIGGER_EXAMPLE
BEFORE DELETE
ON TBL_NUMBER
FOR EACH ROW
BEGIN


-- Insert record into TBL_NUMBER _DUMMY table
INSERT INTO TBL_NUMBER _DUMMY
 ( NAME, NO_OF_PEOPLE)
  VALUES
 ( :old.NAME, :old.NO_OF_PEOPLE)
 END;

Now we want to modify trigger and check value NO_OF_People in original table tbl_number and if it is (-1), we want to put 0 (or null) in column NO_of_People of table tbl_number_dummy.

Any pointers for it would be appreciated.

Thanks, -Adi

1 Answer 1

2
INSERT INTO TBL_NUMBER_DUMMY
 ( NAME, NO_OF_PEOPLE)
  VALUES
 ( :old.NAME, decode(:old.NO_OF_PEOPLE, -1, 0, :old.NO_OF_PEOPLE)
 END;

Or, if you want to insert into TBL_NUMBER _DUMMY only if NO_OF_PEOPLE = -1,

CASE WHEN :old.NO_OF_PEOPLE = -1 THEN
INSERT INTO TBL_NUMBER _DUMMY
 ( NAME, NO_OF_PEOPLE)
  VALUES
 ( :old.NAME, :old.NO_OF_PEOPLE)
ELSE null;
END;

UPDATE(response to comment):

INSERT INTO TBL_NUMBER_DUMMY
 ( NAME, NO_OF_PEOPLE)
  VALUES
 ( :old.NAME, case when :old.NO_OF_PEOPLE < 0 then 0; else :old.NO_OF_PEOPLE; end)
 END;

or, with decode

INSERT INTO TBL_NUMBER_DUMMY
 ( NAME, NO_OF_PEOPLE)
  VALUES
 ( :old.NAME, decode(sign(:old.NO_OF_PEOPLE), -1, 0, :old.NO_OF_PEOPLE)
 END;

:)

And yes you can insert null into a number column, except the situation when the column has a NOT NULL. constraint.

Sign up to request clarification or add additional context in comments.

1 Comment

tHANKS ! i needed to insert it into dummy table every time not just (-1). And first thing worked ! And one more thing, how to change no-of-people to 0 when original number of people is any negative number ? and can we insert it as null in new dummy table ? column type is number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.