&quot&quot&quot在Postgres工作,但在H2 Dabatabse中工作

发布于 2025-01-23 08:11:52 字数 628 浏览 3 评论 0 原文

我正在编写一个查询,以使用“ with as”将数据插入2个表中。该查询在Postgres上工作正常,但是在H2数据库上,它正在抛出语法错误。

我有2张桌子。 表1具有2列 - 主键 table1_id table1_value 列。 表2有3列 - pk table2_id table2_value table1_id 作为外键。

查询是这样的:

WITH ins as (
    INSERT INTO table_1 (table1_value) VALUES ("table1_value")
    RETURNING table1_ID as t1_id
   )
   INSERT INTO table_2 (table2_value, tab1_id) VALUES ("table2_value", (SELECT t1_id FROM ins));

此查询在Postgres上正常工作,但是在H2 dB上它会引发语法错误,并带有消息

“;预期”(,与“从”; SQL语句

I am writing a single query to insert data into 2 tables using "WITH AS". The query works fine on Postgres but on H2 database it is throwing syntax error.

I have 2 tables.
Table 1 has 2 columns -- a Primary Key table1_ID and a table1_value column.
Table 2 has 3 columns -- a PK table2_Id and table2_value and table1_id as Foreign key.

The query is like this:

WITH ins as (
    INSERT INTO table_1 (table1_value) VALUES ("table1_value")
    RETURNING table1_ID as t1_id
   )
   INSERT INTO table_2 (table2_value, tab1_id) VALUES ("table2_value", (SELECT t1_id FROM ins));

This query works fine on Postgres but on H2 DB it throws syntax error with a message

"; expected "(, WITH, SELECT, FROM"; SQL statement

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可是我不能没有你 2025-01-30 08:11:52

hadatabase参考链接:


请参阅 compatibility e节:https://www.postgresql.org/docs/current/sql-insert.html

插入符合SQL标准,但返回子句除外
postgresql扩展,与插入物一起使用的能力也是如此
以及与冲突中指定替代行动的能力。
另外,省略了列名列表的情况,但并非所有
列是从值子句或查询中填写的,不允许
标准。

hadatabase reference link:
http://www.h2database.com/html/advanced.html#recursive_queries
http://www.h2database.com/html/commands.html?highlight=insert&search=insert#firstFound


see Compatibility section: https://www.postgresql.org/docs/current/sql-insert.html

INSERT conforms to the SQL standard, except that the RETURNING clause
is a PostgreSQL extension, as is the ability to use WITH with INSERT,
and the ability to specify an alternative action with ON CONFLICT.
Also, the case in which a column name list is omitted, but not all the
columns are filled from the VALUES clause or query, is disallowed by
the standard.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文