0

I am trying to create a db2 trigger so that a transaction history is maintained in a separate table. But I am getting below error and unable to proceed.

SQL Error [42601]: An unexpected token "CREATE TRIGGER HISTORY_TRIGGER AFTER" was found following "BEGIN-OF-STATEMENT". Expected tokens may include: "".. SQLCODE=-104, SQLSTATE=42601, DRIVER= 4.22.29

I am executing the sql through dbeaver.

I have already tried An unexpected token "CREATE TRIGGER and it doesn't help.

CREATE TRIGGER HISTORY_TRIGGER AFTER INSERT OR UPDATE ON MY_SCHEMA.TRANSACTIONS_TABLE
REFERENCING OLD ROW AS OLD
            NEW ROW AS NEW
BEGIN
    INSERT INTO MY_SCHEMA.HISTORY_TABLE
    (ID, EMAIL, REASON, ROLE, TIMESTAMP)
    VALUES(NEW.ID, NEW.EMAIL, NEW.REASON, NEW.ROLE, CURRENT_TIMESTAMP);
END

Please help me to find out what is wrong with my sql trigger.

2
  • Possible duplicate of An unexpected token "CREATE TRIGGER Commented Dec 28, 2018 at 15:23
  • @mustaccio as I have mentioned in the question itself i have already tried that. But seems like I had to change the default delimiter in dbeaver like Mark has mentioned below. Commented Dec 29, 2018 at 15:34

1 Answer 1

2

Window->Preferences->General->Editors->SQL Editor->SQL Processing->Statement delimiter: @

CREATE OR REPLACE TRIGGER HISTORY_TRIGGER 
AFTER INSERT OR UPDATE ON MY_SCHEMA.TRANSACTIONS_TABLE
REFERENCING OLD AS OLD NEW AS NEW
FOR EACH ROW
BEGIN
    INSERT INTO MY_SCHEMA.HISTORY_TABLE (ID, EMAIL, REASON, ROLE, TIMESTAMP)
    VALUES(NEW.ID, NEW.EMAIL, NEW.REASON, NEW.ROLE, CURRENT_TIMESTAMP);
END@
Sign up to request clarification or add additional context in comments.

1 Comment

Before someone says to insert the old values into history. I want to say Its much easier to query history knowing the last value is the most recent in the table that is being tracked.

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.