How to use SEQUENCE in Apache Derby?

試著忘記壹切 提交于 2019-12-06 23:00:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!