Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to get sequence value

New RootsApr 30 2016 — edited Apr 4 2017

Hi Experts,

Below question came across while practicing sequence . I have a sequence which can be referenced from multiple session. 

--------------------------------------------------------

--  DDL for Table TEST and TEST_CP

--------------------------------------------------------

CREATE TABLE TEST

  ( ID_PK NUMBER, NAME VARCHAR2(20)

  );

CREATE TABLE TEST_CP

  ( ID_PK NUMBER, NAME VARCHAR2(20),INSERT_DATE DATE

  );

CREATE sequence sno;

-- Session 1

INSERT INTO test VALUES

  (sno.nextval,'Oracle'

  );

--Session 2

INSERT INTO test VALUES

  (sno.nextval,'Oracle'

  );

 

 

--Session 1

INSERT INTO test_cp VALUES

  (sno.currval,'Oracle',sysdate  -- here sequence value has been update by session 2. But I have to insert value which was generated from session1(  value 1 instead of 2)

  );

Is there any way to get sequence value specific to particular session in pure sql without using PL/SQL and variables.

This can be achieved through PLSQL code. but I would like to know is there any way can achieve through SQL.

DECLARE

  V_SEQ_VALUE NUMBER;

BEGIN

  V_SEQ_VALUE := SNO.NEXTVAL;

  INSERT INTO test VALUES

    (V_SEQ_VALUE,'Oracle'

    );

  INSERT INTO test_cp VALUES

    (V_SEQ_VALUE,'Oracle',sysdate

    ) ;

END;

This post has been answered by CarlosDLG on Apr 30 2016
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 30 2016
Added on Apr 30 2016
12 comments
1,694 views