Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

get Last Inserted ID from Oracle

Contributor ,
Jul 19, 2011 Jul 19, 2011

Hello I know in SQL Server, I can within the cfquery tag do stuff like select @@identity as "newid" to get the just inserted ID of a record.

How do I do this in Oracle 10 G.

The Sequence that creates the unique key is called from a BEFORE INSERT OR UPDATE Trgger. How do I return the just inserted ID in Coldfusion?

TOPICS
Database access
12.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Jul 19, 2011 Jul 19, 2011

The simple answer is SEQUENCE_NAME.CURRVAL.

However.

In order for CURRVAL to be set, you need to be within a session. I'm afraid to say I don't know whether a trigger runs within the context of your session or not, that would have to be tested.

I use Oracle, and if I ever need to get back the sequence value reliably, I select it out upfront then pass it to my insert statements. That way there can be an ambiguity or weirdness involved, no matter how CF handles its connections to the database.

Translate
Guide ,
Jul 19, 2011 Jul 19, 2011

The simple answer is SEQUENCE_NAME.CURRVAL.

However.

In order for CURRVAL to be set, you need to be within a session. I'm afraid to say I don't know whether a trigger runs within the context of your session or not, that would have to be tested.

I use Oracle, and if I ever need to get back the sequence value reliably, I select it out upfront then pass it to my insert statements. That way there can be an ambiguity or weirdness involved, no matter how CF handles its connections to the database.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 19, 2011 Jul 19, 2011
LATEST

Hmm, looks like you might be alright actually, but do make sure you wrap it all up inside a cftransaction block. Testing:

CREATE TABLE TestTable (

  ID NUMBER(2) PRIMARY KEY,

  Name VARCHAR2(20) NOT NULL

) ;

CREATE SEQUENCE Test_seq ;

CREATE OR REPLACE TRIGGER TestTable_Inserted_Trg

BEFORE INSERT ON TestTable FOR EACH ROW

DECLARE

BEGIN

:new.Id := Test_seq.NEXTVAL ;

END ;

/

INSERT INTO TestTable ( Name ) VALUES ( 'Dave' ) ;

SQL> select Test_seq.currval

  2  from dual ;

   CURRVAL

----------

         1

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources