Learn how to Fetch Sequence Values with jOOQ – Java, SQL and jOOQ.

A whole lot of RDBMS assist customary SQL sequences of some kind. The usual SQL syntax to create a sequence is:

The next is how you possibly can 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 can even embed the S.nextval() area expression in another location the place a Subject<?> may be positioned, together with fetching the worth from inside a SELECT:

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

Or, if it’s worthwhile 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)

(Visited 42 times, 1 visits today)

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
Ask ChatGPT
Set ChatGPT API key
Find your Secret API key in your ChatGPT User settings and paste it here to connect ChatGPT with your Tutor LMS website.
0
Would love your thoughts, please comment.x
()
x