"WITH AS" 使用 Postgres 但不使用 H2 数据库
"WITH AS" Working in Postgres but not in H2 dabatabse
我正在编写一个查询以使用“WITH AS”将数据插入到 2 个表中。该查询在 Postgres 上运行良好,但在 H2 数据库上它抛出语法错误。
我有2张桌子。
Table 1 有 2 列——主键 table1_ID 和 table1_value 列。
Table 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 上它会抛出语法错误并显示一条消息
"; expected "(, WITH, SELECT, FROM"; SQL statement
ha数据库引用link:
http://www.h2database.com/html/advanced.html#recursive_queries
http://www.h2database.com/html/commands.html?highlight=insert&search=insert#firstFound
参见兼容性部分: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.
我正在编写一个查询以使用“WITH AS”将数据插入到 2 个表中。该查询在 Postgres 上运行良好,但在 H2 数据库上它抛出语法错误。
我有2张桌子。 Table 1 有 2 列——主键 table1_ID 和 table1_value 列。 Table 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 上它会抛出语法错误并显示一条消息
"; expected "(, WITH, SELECT, FROM"; SQL statement
ha数据库引用link:
http://www.h2database.com/html/advanced.html#recursive_queries
http://www.h2database.com/html/commands.html?highlight=insert&search=insert#firstFound
参见兼容性部分: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.