创建Oracle序列触发器

时间:2014-09-04 08:06:35

标签: oracle triggers sequence

我尝试创建seuqunce触发器,但是当我在SQL Developer中执行触发器创建sql时发生错误。我不明白,其实一切都很好。我在下面分享详细信息,请帮助我,谢谢。

我的触发器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;
/

错误说明:

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 个答案:

答案 0 :(得分:14)

试试这个:

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

您的序列应如下所示:

CREATE SEQUENCE ACCOUNTS_SEQ 
  START WITH 1 
  INCREMENT BY 1;

答案 1 :(得分:1)

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;