简体   繁体   English

如何使用 H2 嵌入式数据库获取序列中的下一个值?

[英]How to get nextvalue in a sequence using H2 Embedded Database?

Background背景

I'm using Oralce as database and H2 Embedded in-memory database for integration tests.我使用 Oralce 作为数据库和 H2 Embedded 内存数据库进行集成测试。

I've created some tables in H2 and was able to get some inserted data.我在 H2 中创建了一些表,并且能够获取一些插入的数据。 However, I could not retrieve the current sequence value for the sequence I've created in H2.但是,我无法检索在 H2 中创建的序列的当前序列值。

I'm aware that Oracle and H2 are not the same and use similar but different syntax.我知道 Oracle 和 H2 不一样,使用相似但不同的语法。 I'm also aware that you can define some alias in H2 in-memory database and embed a java code in-lieu of Oracle SQL functions.我也知道您可以在 H2 内存数据库中定义一些别名并嵌入 java 代码来代替 Oracle Z9778840A0100CB30C982876741B0B5A2 函数。 This gives me a hint that there must be a workaround in retrieving the sequence value using Oracle's syntax over H2 database.这给了我一个提示,在 H2 数据库上使用 Oracle 的语法检索序列值时必须有一种解决方法。

The question问题

How do I make Oracle's syntax for selecting the current value of a sequence work on H2?如何使 Oracle 用于选择序列当前值的语法在 H2 上工作? Do I need to create alias and write embedded java code in-lieu of Oracle's syntax?我是否需要创建别名并编写嵌入式 java 代码来代替 Oracle 的语法? What are my options?我有哪些选择?

The code under test uses the following hypothetical but similar SQL被测代码使用以下假设但类似的 SQL

select myschema.mysequence.nextval from dual

But I'm getting error like the following但我收到如下错误

org.h2.jdbc.JdbcSQLSyntaxErrorException: Column "nextval" not found [42122-199]

It's probably obvious that that isn't going to work because of the difference in syntax.很明显,由于语法的不同,这不会起作用。 I'm looking for a workaround without having to change the code being tested which uses Oracle's syntax.我正在寻找一种解决方法,而不必更改正在测试的使用 Oracle 语法的代码。

Updates更新

I'm using Spring JDBC's EmbeddedDatabaseBuilder which means I don't connect to a separate independent H2 database instance but to an instance created on-the-fly in which I include DDL scripts to create the DB objects.我正在使用 Spring JDBC 的 EmbeddedDatabaseBuilder,这意味着我不连接到单独的独立 H2 数据库实例,而是连接到动态创建的实例,其中我包含 DDL 脚本来创建数据库对象。

The following post along with the accepted answer helped solved it.以下帖子以及接受的答案帮助解决了这个问题。

Does Spring embedded database support different SQL dialects? Spring 嵌入式数据库是否支持不同的 SQL 方言?

Such Oracle-style expression is actually supported by H2, including the version 1.4.199:这种 Oracle 风格的表达其实是 H2 支持的,包括 1.4.199 版本:

set mode Oracle;
create schema myschema;
create sequence myschema.mysequence;
select myschema.mysequence.nextval from dual;
> 1
select myschema.mysequence.nextval from dual;
> 2

You can use syntax from the SQL Standard in H2, if you wish, but it isn't supported by Oracle:如果您愿意,可以使用 H2 中 SQL 标准中的语法,但 Oracle 不支持:

VALUES NEXT VALUE FOR myschema.mysequence;

I have no idea how you got Column "nextval" not found with your SQL;我不知道您是如何在 SQL Column "nextval" not found的; if schema or sequence doesn't exist, the exception will be different.如果架构或序列不存在,则异常会有所不同。

You need to update your question with your real query or post it in a separate question, because this question already contains an answer in the question inself: your own sample query is valid for H2.您需要使用您的真实查询更新您的问题或将其发布在一个单独的问题中,因为此问题本身已经包含问题的答案:您自己的示例查询对 H2 有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM