Support returning ID of inserted row right from the INSERT INTO statement

491 views
Skip to first unread message

Elemér Zágoni

unread,
Sep 14, 2023, 4:24:35 AM9/14/23
to H2 Database
Hi Guys,

I bumped into the following problem:
I have the following code which Works correctly with MSSQL 2019:
Java part:
....
final Integer insertedRowId = this.jdbcTemplate.queryForObject(sqlInsertNew), args, argTypes, Integer.class)

final String sqlInsertNew = "INSERT INTO MyTable ( Name) OUTPUT INSERTED.Id VALUES (?);"

the part:  OUTPUT INSERTED.Id  is NOt recognized by H2 :(

MyTable is Defined like below:

CREATE TABLE [dbo].[MyTable](
  [Id] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
  [Name] [nvarchar](100) NOT NULL
) ON [PRIMARY];

Question:

I desperately need a way to create a working H2 counterpart of the above query,
because I am NOT allowed to change the Java code :(
Can I somehow return the freshly inserted Id in H2, by modifying the Sql statement only?

Thanks for your help,

Elemér.

Evgenij Ryazanov

unread,
Sep 14, 2023, 4:56:43 AM9/14/23
to H2 Database
Hello!

H2 supports standard data change delta tables:
Reply all
Reply to author
Forward
0 new messages