2

I am trying to batch insert some records using the batch-Insert functionality provided by QueryDSL. The same piece of code works when I use just execute() but then have to separately write a select query to get the last inserted records.

databaseAccessor.runInNewTransaction(databaseConnection -> {
            SQLInsertClause batchInsert = databaseConnection.insert(TEST);
            for (int i = 0; i < 5; i++) {
                batchInsert.set(TEST.NAME, "label" + i).set(TEST.PROJECT_ID, 1).addBatch();
            }
            return batchInsert.executeWithKeys(TEST.ID);
        }, OnRollback.NOOP);

This piece of code works for MySQL and PostgreSQL but fails for ORACLE and SQL Server, Is there a way I can do this and also get the id of the inserted records.

The exception I get is as follows:

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: ****The statement must be executed before any results can be obtained.****

1 Answers1

0

It's not possible to get the generated IDs created by a batch insert back from a Microsoft SQL Server.

It's not supported by their JDBC driver. The following page goes into detail on why this can't be achieved in the foreseeable future: https://github.com/microsoft/mssql-jdbc/issues/245