Saturday, November 23, 2024
HomeProgrammingEasy methods to Fetch Sequence Values with jOOQ – Java, SQL and...

Easy methods to Fetch Sequence Values with jOOQ – Java, SQL and jOOQ.


A number of RDBMS help normal SQL sequences of some kind. The usual SQL syntax to create a sequence is:

The next is how you might fetch a worth from this sequence, utilizing jOOQ, assuming you’re utilizing the code generator:

// import static com.instance.generated.Sequences.*;

System.out.println(ctx.fetchValue(S.nextval()));

The sequence expression interprets to quite a lot of dialects:

-- CockroachDB, PostgreSQL, YugabyteDB
nextval('s')

-- Db2, HANA, Informix, Oracle, Snowflake, Sybase SQL Wherever, Vertica
s.nextval

-- Derby, Firebird, H2, HSQLDB, MariaDB, SQL Server
NEXT VALUE FOR s

You may as well embed the S.nextval() area expression in some other location the place a Area<?> may be situated, together with fetching the worth from inside a SELECT:

ctx.choose(S.nextval()).fetch();

Or, if you’ll want to fetch a number of sequence values in a single go, use nextvals():

System.out.println(ctx.fetchValues(S.nextvals(10)));

This prints one thing like:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

And the question being executed makes use of jOOQ’s GENERATE_SERIES emulation to fetch all of the values in a single go, e.g. for H2:

SELECT NEXT VALUE FOR s
FROM system_range(1, 10)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments