Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Friday, 24 October 2014

Assign sequence number to variable in Oracle

In Oracle, Upto 10G, We need to SELECT the sequence next value from DUAL table. But in Oracle 11G onwards, We can directly assign a sequence next value to a variable which will avoid round trip to the Oracle server. Let's see how it is:-

First create a sequence in Oracle:-

CREATE SEQUENCE TEST_SEQ
START WITH 1
MIN VALUE 1
INCREMENT BY 1
MAX VALUE 9999999999999999
NO RECYCLE
NO CACHE;

Now, in your block, declare a variable in DECLARE section and assign value to that variable in EXECUTABLE section:-

DECLARE
SEQ_VALUE NUMBER;
BEGIN
SEQ_VALUE :=TEST_SEQ.NEXTVAL;
END;

Above code will directly assign the sequence number to variable without selecting the value from DUAL table.

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.