问题
I'd like to use SEQUENCE support in Apache Derby 10.7. I've created the sequence with the following statement:
CREATE SEQUENCE SAMPLE_SEQ AS INT MAXVALUE 999999 CYCLE;
How can I select next/current value from the SAMPLE_SEQ
? Could you please help me out with the query?
回答1:
Apache Derby Doc says: Use a NEXT VALUE FOR expression
Should be something like
SELECT NEXT VALUE FOR SAMPLE_SEQ;
回答2:
Use NEXT VALUE FOR as documented in the manual:
http://db.apache.org/derby/docs/10.7/ref/rrefsqljnextvaluefor.html#rrefsqljnextvaluefor
回答3:
To get the current value of the sequence the following SQL should be executed:
SELECT CURRENTVALUE FROM SYS.SYSSEQUENCES WHERE SEQUENCENAME='SAMPLE_SEQ'
回答4:
In the SQL command prompt, you can query the next value with this statement:
values NEXT VALUE FOR <sequence_name>
This will work as an expression embedded into an INSERT statement. E.g.:
INSERT INTO <table_name> (IDFIELD) VALUES (NEXT VALUE FOR <sequence_name>)
回答5:
In case you want to fetch the 'current value' from the 'sequence':
- values ( next value for <sequence> )
Same in Java using JDBC:
ResultSet rs = conn.prepareStatement("values (next value for <sequence>)").executeQuery();
rs.next();
int seqValue = rs.getInt(1);
Source: Derby-user-mailing list archive
来源:https://stackoverflow.com/questions/5729063/how-to-use-sequence-in-apache-derby