简体   繁体   中英

Compilation errors with 12c Trigger statements?

First post and it's a biggie. I've been stuck on this for a while and it's holding up the rest of my project - it's been a long time since I've done much in the way of database development. The last time I used Oracle it was on 11g but the syntax I'm trying to reuse from an old project doesn't seem to work now and I'm left scratching my head. I'd be incredibly grateful if anyone can point out the (probably) obvious thing I'm missing?

Not sure if I've posted too much here so just say if so.


Here's the table CREATE, and TRIGGER statements

 1. CREATE
 2.   TABLE employees
 3.     (
 4.       Employee_id       NUMBER   (4)   NOT NULL ,
 5.       firstname         VARCHAR2 (64)  NOT NULL ,
 6.       surname           VARCHAR2 (64)  NOT NULL ,
 7.       email             VARCHAR2 (256) NOT NULL ,
 8.       telephone         VARCHAR2 (20) ,
 9.       emp_password      VARCHAR2 (32)  NOT NULL ,
 10.      startdate         DATE           NOT NULL ,
 11.      organisation_id   NUMBER   (3)   NOT NULL ,
 12.      department_id     NUMBER   (2)   NOT NULL ,
 13.      jobtitle_id       NUMBER   (3)   NOT NULL ,
 14.      user_level        NUMBER   (1)   NOT NULL ,
 15.       Emp_added_by      NUMBER   (4)   NOT NULL ,   
 16.       Emp_added_on      TIMESTAMP      NOT NULL ,
 17.       Emp_updated_by    NUMBER   (4)   NOT NULL ,
 18.       Emp_updated_on    TIMESTAMP      NOT NULL
 19.     ) ;

 20.   ALTER TABLE employees ADD CONSTRAINT employees_PK 
 21.     PRIMARY KEY ( employee_id ) ;

And the Sequence and statement;

22. CREATE SEQUENCE "seq_employee_id" MINVALUE 1 MAXVALUE 9999 INCREMENT BY 1 
23. START WITH 1 NOCACHE ORDER NOCYCLE ;

And finally the trigger that's giving me shit;

24. create or replace 
25. trigger trg_employees 
26. BEFORE 
27.  INSERT OR 
28.  UPDATE OF employee_id 
29.  ON employees FOR EACH ROW 
30. BEGIN 
31.   IF INSERTING THEN IF 
32.    :NEW.employee_id = NULL THEN
33.      SELECT seq_employee_id.NEXTVAL
34.      INTO :NEW.employee_id
35.      FROM sys.dual;
36.    END IF;
37.  END IF;
38.  /*format firstname to have initial capital letter*/
39.  :NEW.firstname := INITCAP(:NEW.firstname);
40.  /*format surname to have initial capital letter*/
41.  :NEW.surname := INITCAP(:NEW.surname);
42.  /*replace any characters other than digits with an empty string*/
43.  :NEW.telephone := REGEXP_REPLACE(:NEW.telephone, '[^[:digit:]]', '');
44.  /*adjust to (99999) 999999 format */
45.  :NEW.telephone := REGEXP_REPLACE(:NEW.telephone,
46.            '([[:digit:]]{5})([[:digit:]](6)', '(\1) \2');
47. END;

48. ELSIF UPDATING THEN
49.   SELECT SYSDATE INTO :NEW.employee_updated_on FROM sys.dual;
50. END IF;
51. END;

/*This trigger automatically inserts a timestamp into updated rows to show audit
details.*/
52. CREATE OR REPLACE TRIGGER trg_employee_update 
53. BEFORE UPDATE ON employees FOR EACH ROW 
54. BEGIN
  /* Update "employee_updated_on" to current system date*/
55.  :NEW.employee_updated_on := sysdate;
56. END;

I can create the table, constraints, and sequence with no trouble; but when I run the trigger statement it compiles with the following errors

4,7 PL/SQL: SQL Statement ignored 4,14 PL/SQL: ORA-02289: sequence does not exist

Now obviously the sequence does exist, but I have no idea at all why it can't recognise it as I can view the sequence fine in my database. I don't know if I've got a syntax error at line 10, SELECT seq_employee_id.NEXTVAL , perhaps?

The sequence you're referencing in your trigger doesn't exist.

If you use double-quotes around the identifier in your CREATE SEQUENCE statement

CREATE SEQUENCE "seq_employee_id" MINVALUE 1 MAXVALUE 9999 INCREMENT BY 1 
START WITH 1 NOCACHE ORDER NOCYCLE ;

then you are creating a case-sensitive identifier. If you do that, you would need to refer to the identifier using double quotes and with the correct case every time you refer to it. That's generally rather annoying which is why I'd never suggest creating case-sensitive identifiers.

SELECT "seq_employee_id".NEXTVAL
  INTO :NEW.employee_id
  FROM sys.dual;

Starting with 11g, you can simplify the statement by doing a direct assignment of the sequence

:new.employee_id := "seq_employee_id".nextval;

Realistically, I'd suggest recreating the sequence without the double-quotes and using the direct assignment to the :new.employee_id .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM