创建插入触发错误

时间:2010-01-24 11:21:12

标签: oracle triggers

我正在尝试创建一个触发器,用于从预定义的序列设置id。

CREATE SEQUENCE seq_list_status 
    START WITH 1 
    INCREMENT BY 1 

;

CREATE OR REPLACE TRIGGER trg_list_status_insert 
    BEFORE INSERT ON list_status 
    FOR EACH ROW 
    select seq_list_status.nextval into :new.id from dual; 
/

我在创建触发器时收到以下错误

Error starting at line 1 in command:
CREATE OR REPLACE TRIGGER trg_list_status_insert 
    BEFORE INSERT ON list_status 
    FOR EACH ROW 
    select seq_list_status.nextval into :new.id from dual
Error at Command Line:4 Column:4
Error report:
SQL Error: ORA-04079: invalid trigger specification
04079. 00000 -  "invalid trigger specification"
*Cause:    The create TRIGGER statement is invalid.
*Action:   Check the statement for correct syntax.
我用谷歌搜索了它,但似乎一切都好。什么可能是错的?

2 个答案:

答案 0 :(得分:6)

触发器是程序单元。因此,我们必须将代码体包装在BEGIN和END中。试试这个

CREATE OR REPLACE TRIGGER trg_list_status_insert
    BEFORE INSERT ON list_status
    FOR EACH ROW
BEGIN
    select seq_list_status.nextval into :new.id from dual;
END;
/

不幸的是the examples in the SQL Reference没有我们想要的那么有用。但它确实链接到其他有用的文档,例如the App Developers Guide

答案 1 :(得分:5)

您错过了begin end

CREATE OR REPLACE TRIGGER trg_list_status_insert 
BEFORE INSERT ON list_status 
FOR EACH ROW 
BEGIN
select seq_list_status.nextval into :new.id from dual; 
END;
/