1

I'm trying to assign a value to a variable based on an if statement to be used to insert into a table as part of a trigger

I'm trying to assign a value to a variable based if another variable that is calculated is great than zero.

 CREATE OR REPLACE TRIGGER transactions_checking_trigger
   AFTER INSERT OR UPDATE ON checking
   FOR EACH ROW
 DECLARE
   account_type        char(4);     
   account_num         char(4);           
   trans_date          date;             
   trans_amt           number;            
   trans_type          char(4);            
   trans_comments      varchar(25);    
 BEGIN

   account_type    := 'CHEK';
   trans_date      := sysdate;
   trans_amt       := :new.balance-:old.balance;

   if trans_amt > 0 
      trans_type := 'DEPT'
   ELSE
      trans_type := 'WDRW'
   end if;

   IF UPDATE THEN
      INSERT INTO transactions (account_type, account_num, trans_date, 
      trans_amt, trans_type,trans_comments)
      VALUES (account_type, :new.account_num,trans_date,trans_amt, trans_type, 
      new:trans_comments);
   END IF;
END;
/

I expect the trigger to insert DEPT if trans_amt > 0 and WDRW if trans_amt < 0

2
  • 1
    What is the current behavior? Are you getting compilation or runtime errors? Commented Aug 20, 2019 at 19:42
  • I haven't ran it because I'm getting a syntax error with the IF statement. Commented Aug 20, 2019 at 19:55

1 Answer 1

1

Syntax is IF-THEN-ELSE; you're missing the THEN. Also, statement has to be terminated with a semi-colon.

if trans_amt > 0 
then                        --> this
   trans_type := 'DEPT';    --> semi-colon
ELSE
   trans_type := 'WDRW';    --> semi-colon
end if;

A simpler option:

trans_type := case when trans_amt > 0 then 'DEPT' 
                   else 'WDRW'
              end;

This is wrong:

IF UPDATE THEN

Did you mean updating instead?

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

Comments

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.