I’m adding new features to a legacy application written in PHP that uses an Oracle 9i database through the ODBC functions. I’ve created a table that has a sequence and a trigger to generate auto-incrementing IDs.
Now, I’m struggling to find a way to make an insert and obtain the generated ID afterwards:
- The ODBC library does not appear to have a dedicated
lastInsertIdmethod. - Running a query with
RETURNINGclause triggers:ORA-00439: feature not enabled: RETURNING clause from this client type. - I’m able to run a
RETURNINGclause if I enclose it in aBEGIN...ENDblock but it’s of little help: OUT parameters are apparently not supported by the Unified ODBC driver PHP uses.
Do I need to hard-code the sequence name and use SEQ_NAME.CURRVAL in the rest of the transaction? What do I need to do to ensure I get the right value even if there’re concurrent accesses?
Update: added third point to failed attemps
Best bet might be to code this insert as a PL/SQL API on the database side which will return you the ID to use.
e.g.)
Then you could call this as a prepared statement to get the out param and use the ID for the rest of your transaction.
Whoops – just saw the “No OUT params comment….”
OK, then I guess you need to manage it all from the UI.
Drop the trigger from the table, do a
to get the next available key value, store it locally, and then do all of the work yourself using the retrieved ID to insert into the ID field in the table. The sequence will ensure that you have transactional security on the ID.
What a pain in the butt implementation of ODBC!