1

i would like to create a simple trigger to check a stored variable from a table.
if the value of the variable is '1', then approve the insertion
else if the value of the variable is '2', then prompt error message.

CREATE OR REPLACE TRIGGER approval 
BEFORE INSERT ON VIP
REFERENCING OLD AS MEMBER
FOR EACH ROW
DECLARE 
  CONDITION_CHECK NUMBER;
BEGIN
  SELECT CONDITION INTO CONDITION_CHECK FROM MEMBER; 
  IF CONDITION_CHECK = '2' THEN
    RAISE_APPLICATION_ERROR (-20000, ' UPGRADE DENIED!');
  END IF;
END;

But this trigger disable all the entries even when the condition value is '1'.

3 Answers 3

3

Use AFTER triggers to check conditions: the values can be modified by other BEFORE triggers. In AFTER triggers, you are guaranteed that the value won't be changed afterwards.

It seems you're missing a condition in your SELECT (if as I believe you're trying to get the value from another table):

CREATE OR REPLACE TRIGGER approval 
AFTER INSERT ON VIP
FOR EACH ROW
DECLARE 
  CONDITION_CHECK NUMBER;
BEGIN
  SELECT CONDITION 
    INTO CONDITION_CHECK 
    FROM MEMBER 
   WHERE member_id = :new.member_id; 
  IF CONDITION_CHECK = '2' THEN
    RAISE_APPLICATION_ERROR (-20000, 'UPGRADE DENIED!');
  END IF;
END;
Sign up to request clarification or add additional context in comments.

Comments

3

You can simply reference the :new.condition in your trigger

CREATE OR REPLACE TRIGGER approval 
  BEFORE INSERT ON VIP
  FOR EACH ROW
BEGIN
  IF :new.condition = '2' THEN
    RAISE_APPLICATION_ERROR (-20000, ' UPGRADE DENIED!');
  END IF;
END;

It would, however, make little sense to put this sort of logic into a trigger. It would make far more sense to define a CHECK constraint on the column that ensures that the condition is one of an allowed set of values.

Comments

0

You are referencing the old data rather than the new - presumably that's not what you intended?

2 Comments

Did you mean for this to be an answer? If so, you may want to make it look... more like an answer.
Thanks for that advice. I believe it is an answer, sufficient at least to tell him what the problem is - I don't feel I need to hold hands.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.