2

I'm using a sequence and trigger to essentially auto increment a column in a table, however I'm getting an error - ORA-24344: success with compilation error.

I was using this post: How to create id with AUTO_INCREMENT on Oracle? and it worked successfully for two other tables w/ auto increment I made, but there must be something in here I'm not familiar with causing an error.

More edits: Thanks to Polppan we've established that this likely isn't an Oracle issue, rather an OCI with PHP issue. I'm using:

oci_execute($sql);

And as mentioned here (again, thanks Polppan for that link), there's a bit of an issue between EOL characters and oci_execute. It was 11 years ago, so I don't know if that's been patched or not, and I did try his solution but it didn't help. Does anyone know if there are other issues with oci_execute and creating triggers?

Creating the table: (works)

CREATE TABLE RT_documents (
  documentID INT NOT NULL, 
  reviewID varchar2(20) NOT NULL, 
  file_location CLOB NOT NULL,
  version NUMBER(*,3) NOT NULL,
  CONSTRAINT RT_documents_pk PRIMARY KEY (documentID)
)

Creating the sequence: (works)

CREATE SEQUENCE rt_documents_seq

Creating/replacing trigger: (doesn't work)

CREATE OR REPLACE TRIGGER rt_documents_bir
BEFORE INSERT ON RT_documents
FOR EACH ROW

BEGIN
    SELECT RT_documents_seq.NEXTVAL
    INTO :new.documentID
    FROM dual;
END;

EDIT: Exact error message as requested - (Note, I'm executing these query-by-query using OCI/Oracle in PHP. PHP tag added just in case, but pretty sure this is an oracle syntax error or something).

Error:

Notice: oci_execute(): OCI_SUCCESS_WITH_INFO: ORA-24344: success with compilation error in (...)

-I can successfully execute the first two queries, and double checked and the table is there so it worked properly.

Community
  • 1
  • 1
Corey Thompson
  • 398
  • 6
  • 18
  • @Polppan the Identity function is new to 12c, but triggers and sequences used here are for databases pre-12c. (Your link even specifies this a few times) – Corey Thompson Aug 26 '14 at 05:16
  • I tried your code in 10g and it didn't work, complaining about `id` doesn't exist. – Jacob Aug 26 '14 at 05:17
  • As you mentioned, it needed to be documentID (which I've changed to that now) but I'm still getting the same error :\ – Corey Thompson Aug 26 '14 at 05:22
  • I do not think this as a duplicate question as it more of an issue with execution of trigger code from PHP. So kindly re-open this question. – Jacob Aug 26 '14 at 06:26

2 Answers2

2

Trigger doesn't understand new.id as id doesn't exist in RT_documents table.

Your trigger should be

CREATE OR REPLACE TRIGGER rt_documents_bir
   BEFORE INSERT
   ON RT_documents
   FOR EACH ROW
BEGIN
   SELECT RT_documents_seq.NEXTVAL INTO :new.documentID FROM DUAL;
END;

Update

SELECT * FROM v$version;

Oracle Database 10g Enterprise Edition

CREATE TABLE RT_documents
(
   documentID      INT NOT NULL,
   reviewID        VARCHAR2 (20) NOT NULL,
   file_location   CLOB NOT NULL,
   version         NUMBER (*, 3) NOT NULL,
   CONSTRAINT RT_documents_pk PRIMARY KEY (documentID)
);

Table created.

CREATE SEQUENCE rt_documents_seq;

Sequence created.

CREATE OR REPLACE TRIGGER rt_documents_bir
   BEFORE INSERT
   ON RT_documents
   FOR EACH ROW
BEGIN
   SELECT RT_documents_seq.NEXTVAL INTO :new.documentID FROM DUAL;
END;
/

Trigger created.

INSERT INTO RT_documents (reviewID, file_location, version)
 VALUES ('test', 'test', 1);

1 row created.


SELECT * FROM RT_documents;

DOCUMENTID REVIEWID             FILE_LOCATION
                                    VERSION
---------- -------------------- -------------------------------------------
-------------------------------- ----------
         1 test                 test
                                          1
Jacob
  • 14,463
  • 65
  • 207
  • 320
  • I changed it to that and unfortunately I'm still getting the same error. In fact, in a previous table creation, I managed to get it to work with :new.id even though I didn't have an ID column. Presumably since it's a trigger there may be an error when I try inserting into that table, but I'll go back and fix that one after I can get this one working. – Corey Thompson Aug 26 '14 at 05:07
  • @CoreyThompson Could you provide the exact error message? – Jacob Aug 26 '14 at 05:08
  • Just want to note for future readers that my original post had `INTO :new.id` but I edited it to show `INTO :new.documentID` as it wasn't causing the error. – Corey Thompson Aug 26 '14 at 05:27
  • @CoreyThompson I presume that you are trying to execute sql statements using PHP code, rather than from sqlplus or from any other database tool. – Jacob Aug 26 '14 at 05:34
  • I'm (essentially) using `oci_execute($statement)` (with of course a proper set up connection and everything - as I mentioned, I can create the table and the sequence. I checked your updated answer and I have it word-for-word and I can't for the life of me figure out why the trigger won't create. – Corey Thompson Aug 26 '14 at 05:41
  • Is there a way I can check what triggers/sequences are already existing? Maybe there's a trigger already in place preventing this one from being created? (PS. I checked, I'm running the same version as you - 10g) – Corey Thompson Aug 26 '14 at 05:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59980/discussion-between-polppan-and-corey-thompson). – Jacob Aug 26 '14 at 05:45
  • Unfortunately I'm unable to access Chat, our network automatically blocked it. – Corey Thompson Aug 26 '14 at 05:47
  • @CoreyThompson Try SELECT * FROM user_objects WHERE object_type = 'SEQUENCE' However if sequence does exist, then it would complain that it already exist with same name. For triggers it is CREATE or REPLACE, so it wouldn't complain about already existing triggers – Jacob Aug 26 '14 at 05:48
  • @CoreyThompson See this [helps](https://community.oracle.com/thread/418995?start=0&tstart=0) – Jacob Aug 26 '14 at 05:49
  • Would it complain about two separately named triggers that are both supposed to execute prior to inserting a row? Some logic-error like not knowing which to execute first, so it wouldn't allow a second one? – Corey Thompson Aug 26 '14 at 05:50
  • I'm able to access the chat now if you want to continue in there? – Corey Thompson Aug 26 '14 at 05:52
2

Thanks to Polppan.

The solution was removing the EOL characters. (I did try this but had, without realising, removed the semi-colons, which caused the same error code)

This was a PHP error after all. Using oci_execute, you must remove EOL characters in triggers:

$sql = "CREATE OR REPLACE TRIGGER ......."; //shortened for easy reading
$sql = str_replace(chr(13),'',$sql);
$sql = str_replace(chr(10),'',$sql);
oci_execute($sql);
Corey Thompson
  • 398
  • 6
  • 18