Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I request the next value of a sequence via Java? #237

Open
Radenixlol opened this issue Feb 14, 2025 · 8 comments
Open

Can I request the next value of a sequence via Java? #237

Radenixlol opened this issue Feb 14, 2025 · 8 comments

Comments

@Radenixlol
Copy link

Hello, I wanted to know if there is a way to request the value of a sequence through a query, since I tried "values nextval for name_seq" as I would do through the interface and it does not recover the value, and the documentation only mentions methods to embed it within an INSERT, and I have a use case in which I need to bring the value to my program to do something with it beforehand.

@wimjongman
Copy link

Can you provide a code snippet that explains your problem better?

@Radenixlol
Copy link
Author

Radenixlol commented Feb 17, 2025

String sql = "values nextval for ORDER_SEQ"
Query query = em.createNativeQuery(sql);
query.getFirstResult();

I just want to retrieve the next value in the sequence

and I only find examples like this in the IBM documentation:

INSERT INTO ORDERS (ORDERNO, CUSTNO)
VALUES (NEXT VALUE FOR ORDER_SEQ, 12)

which doesn't work for me

Note: ORDER_SEQ is de name of sequence

@Radenixlol
Copy link
Author

I would also like to know if there is a query in SQL that allows me to verify the existence of a sequence in the system. I have not found any way to do so other than through the database itself.

@wimjongman
Copy link

wimjongman commented Feb 18, 2025

  1. When you ask for the next sequence it automatically increments
  2. The sequence is implemented as a data area in the schema

Create the sequence

set schema mylib

CREATE SEQUENCE ORDER_SEQ    
START WITH 500               
INCREMENT BY 1               
MAXVALUE 10000               
NOCYCLE                      
CACHE 24      

Increment the sequence

select (next value for order_seq) from qcllesrc limit 1 -- any table

Get the last used increment

select (previous value for order_seq) from qcllesrc limit 1 -- any table

@Radenixlol
Copy link
Author

@wimjongman Thank you very much

Another question, do you know where IBM I keeps track of this sequence? Is there a table that can be consulted?

@wimjongman
Copy link

2. The sequence is implemented as a data area in the schema

@Radenixlol
Copy link
Author

I guess it's not queryable via SQL then, I've been doing this for a short time and I'm really not sure what implications a data area has with respect to a file, thank you very much anyway

@wimjongman
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants