0

Tried to create following trriger:

CREATE TRIGGER EWR.INS_STU
AFTER INSERT ON EWR.STUDENT
    FOR EACH ROW
BEGIN
   IF ( :NEW.ROLL_NO > 60 ) THEN
      INSERT INTO EWR.STUDENT_DIV VALUES ( :NEW.ROLL_NO,'P');
   END IF;

   IF( :NEW.ROLL_NO < 60)  
   THEN
     INSERT INTO EWR.STUDENT_DIV VALUES (:NEW.ROLL_NO,'F');
   END IF;
END
!

But it is giving the following error:

DB21034E The command was processed as an SQL statement because it was not a valid Command Line Processor command. During SQL processing it returned: SQL0104N An unexpected token ":NEW.ROLL_NO > 30 ) THEN INSERT INT" was found following "H ROW BEGIN IF (". Expected tokens may include:
"". LINE NUMBER=6. SQLSTATE=42601

SQL0104N An unexpected token ":NEW.ROLL_NO > 30 ) THEN INSERT INT" was found following "H ROW BEGIN IF (". Expected tokens may include: "".

1
  • Which database are you using? Commented Apr 19, 2013 at 10:12

1 Answer 1

2

Why are you using NEW with the notation as a host variable (:)? You do not need to put the colon before the variable name, because this is a trigger that uses only sql (sql pl).

Also, you have to declare how you are going to reference the new values, defined in the header.

REFERENCING NEW AS N

I recreated your case, and it works for me like this:

db2 "create table ewr.student(roll_no int)"
db2 "create table ewr.student_div(roll_no int, other char(1))"

trigger.sql

CREATE TRIGGER EWR.INS_STU
  AFTER INSERT ON EWR.STUDENT
  REFERENCING NEW AS NEW
  FOR EACH ROW
BEGIN
  IF ( NEW.ROLL_NO > 60 ) THEN
    INSERT INTO EWR.STUDENT_DIV VALUES ( NEW.ROLL_NO, 'P' );
  END IF;
  IF ( NEW.ROLL_NO < 60 ) THEN
    INSERT INTO EWR.STUDENT_DIV VALUES ( NEW.ROLL_NO, 'F' );
  END IF;
END !

db2 -td! -f trigger.sql
DB20000I  The SQL command completed successfully.

I hope this solve your problem.

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.