警告:使用编译错误创建触发器。我对创建触发器感到困惑

时间:2017-10-11 10:31:01

标签: sql oracle

创建一个名为SEQ_CUSTOMER的序列和一个名为TR_CUSTOMER_ID的触发器,它将自动为C_ID列生成一系列数字。此序列应从10,000开始并递增1。

CREATE SEQUENCE "SEQ_CUSTOMER" 
   MINVALUE 50 MAXVALUE 999999999990
   INCREMENT BY 1 
   START WITH 10000; 

CREATE OR REPLACE TRIGGER "TR_CUSTOMER_ID" 
BEFORE INSERT ON "CUSTOMERS" 
FOR EACH ROW 
BEGIN
  SELECT "SEQ_CUSTOMER". NEXTVAL INTO :NEW.C_ID FROM DUAL; 
END; 

警告:使用编译错误创建触发器。

SQL> SHOW ERRORS;
Errors for TRIGGER "TR_CUSTOMER_ID":

LINE/COL ERROR
-------- -----------------------------------------------------------------
2/1      PL/SQL: SQL Statement ignored
2/9      PL/SQL: ORA-02289: sequence does not exist
SQL>

如何解决问题,谢谢

2 个答案:

答案 0 :(得分:1)

是第一个

DROP SEQUENCE SEQ_CUSTOMER;
CREATE SEQUENCE SEQ_CUSTOMER START WITH 10000;

是第二个

CREATE OR REPLACE TRIGGER TR_CUSTOMER_ID
BEFORE INSERT
ON CUSTOMERS
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
BEGIN
  :new.C_ID := SEQ_CUSTOMER.nextval;
END TR_CUSTOMER_ID;

答案 1 :(得分:0)

从触发器中删除空格,也可以在引号之间写入。

CREATE SEQUENCE "SEQ_CUSTOMER" 
   MINVALUE 50 MAXVALUE 999999999990
   INCREMENT BY 1 
   START WITH 10000
/

CREATE OR REPLACE TRIGGER "TR_CUSTOMER_ID" 
BEFORE INSERT ON "CUSTOMERS" 
FOR EACH ROW 
BEGIN
    SELECT SEQ_CUSTOMER.NEXTVAL
  INTO   :new.ID
  FROM   dual;
END;
/