java 如何在 Apache Derby 中使用 SEQUENCE?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5729063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use SEQUENCE in Apache Derby?
提问by Tomasz B?achowicz
I'd like to use SEQUENCE support in Apache Derby 10.7. I've created the sequence with the following statement:
我想在 Apache Derby 10.7 中使用 SEQUENCE 支持。我使用以下语句创建了序列:
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?
如何从 中选择下一个/当前值SAMPLE_SEQ
?你能帮我解决这个问题吗?
回答by Andreas Dolk
Apache Derby Doc says: Use a NEXT VALUE FOR expression
Apache Derby Doc 说:使用 NEXT VALUE FOR 表达式
Should be something like
应该是这样的
SELECT NEXT VALUE FOR SAMPLE_SEQ;
回答by a_horse_with_no_name
Use NEXT VALUE FOR as documented in the manual:
按照手册中的说明使用 NEXT VALUE FOR:
http://db.apache.org/derby/docs/10.7/ref/rrefsqljnextvaluefor.html#rrefsqljnextvaluefor
http://db.apache.org/derby/docs/10.7/ref/rrefsqljnextvaluefor.html#rrefsqljnextvaluefor
回答by user611544
To get the current value of the sequence the following SQL should be executed:
要获取序列的当前值,应执行以下 SQL:
SELECT CURRENTVALUE FROM SYS.SYSSEQUENCES WHERE SEQUENCENAME='SAMPLE_SEQ'
回答by sola
In the SQL command prompt, you can query the next value with this statement:
在 SQL 命令提示符下,您可以使用以下语句查询下一个值:
values NEXT VALUE FOR <sequence_name>
This will work as an expression embedded into an INSERT statement. E.g.:
这将用作嵌入到 INSERT 语句中的表达式。例如:
INSERT INTO <table_name> (IDFIELD) VALUES (NEXT VALUE FOR <sequence_name>)
回答by ankurrc
In case you want to fetch the 'current value' from the 'sequence':
如果您想从“序列”中获取“当前值”:
- values ( next value for <sequence> )
- 值(<序列>的下一个值)
Same in Java using JDBC:
在 Java 中使用 JDBC 也是如此:
ResultSet rs = conn.prepareStatement("values (next value for <sequence>)").executeQuery();
rs.next();
int seqValue = rs.getInt(1);
Source: Derby-user-mailing list archive
来源: Derby 用户邮件列表存档