10

I'm trying create seuqunce trigger but error occurs when I executing the trigger create sql in SQL Developer. I don't understand, actually everything looks fine. I share the details below, please help me, thanks.

My trigger sql:

CREATE OR REPLACE TRIGGER "TRIGGER1" BEFORE INSERT ON ACCOUNTS
FOR EACH ROW
 WHEN (new."ID" IS NULL) 
BEGIN
  SELECT ACCOUNTS_SEQ.NEXTVAL 
  INTO :new."ID" 
  FROM dual;
END;
/

And error description:

Error starting at line : 5 in command -
CREATE OR REPLACE TRIGGER "TRIGGER1" BEFORE INSERT ON ACCOUNTS
FOR EACH ROW
 WHEN (new."ID" IS NULL) 
BEGIN
  SELECT ACCOUNTS_SEQ.NEXTVAL 
  INTO :new."ID" 
  FROM dual
Error report -
SQL Command: trıgger "TRIGGER1"
Failed: Warning: completed with warning

Error starting at line : 12 in command -
END
Error report -
Unknown Command
2
  • 1
    Get rid off those double quotes and recompile. Commented Sep 4, 2014 at 8:38
  • ID needs to be the actual field in the ACCOUNTS table, too. Commented Sep 21, 2016 at 15:44

2 Answers 2

24

Try this:

CREATE OR REPLACE TRIGGER TRIGGER1
BEFORE INSERT ON ACCOUNTS
FOR EACH ROW
  WHEN (new.ID IS NULL)
BEGIN
  :new.ID := ACCOUNTS_SEQ.NEXTVAL;
END;
/

Your sequence should look like this:

CREATE SEQUENCE ACCOUNTS_SEQ 
  START WITH 1 
  INCREMENT BY 1;
Sign up to request clarification or add additional context in comments.

11 Comments

Unfortunately, trigger was created bu I can't compile it. Error(8,11): PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following: ;
Now trigger sql looks like: CREATE OR REPLACE TRIGGER TRIGGER1 BEFORE INSERT ON ACCOUNTS FOR EACH ROW WHEN (new.ID IS NULL) BEGIN SELECT ACCOUNTS_SEQ.NEXTVAL INTO :new.ID FROM dual
Make sure you are using / after the block.
I'm sure but same result
and also after from dual you need to add END;
|
2
CREATE OR REPLACE TRIGGER TRIGGER1
BEFORE INSERT ON ACCOUNTS
FOR EACH ROW
BEGIN
IF :new.ID IS NULL THEN 
SELECT ACCOUNTS_SEQ.NEXTVAL 
INTO :new.ID
FROM dual;
END IF;
END;

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.