Back to Exposed

batchInsert

docs/api/exposed-r2dbc/org.jetbrains.exposed.v1.r2dbc/batch-insert.html

1.5.011.5 KB
Original Source

batchInsert

suspend fun <T : Table, E> T.batchInsert(data: Iterable<E>, ignore: Boolean = false, shouldReturnGeneratedValues: Boolean = true, body: BatchInsertStatement.(E) -> Unit): List<ResultRow>

Represents the SQL statement that batch inserts new rows into a table.

A single INSERT statement will be prepared and parameterized with new bindings for each row in data, relying entirely on the underlying driver's statement-level batching mechanisms. The Exposed logger will still represent this batching operation by logging a new INSERT SQL line per row.

Alternatively, a single INSERT statement that uses multi-row values constructor for batch inserting can be created by Exposed by setting useMultiRowValues = true.

Note: On most databases, a batch with ignore enabled that inserts only some of its rows returns only the rows that were successfully inserted. However, on H2 and MariaDB, such a batch returns a row for every value in data rather than only the inserted ones, and pairs the values the database generated with the wrong rows. This occurs because their drivers report no update count per statement, leaving nothing to tell the skipped rows apart by. Any batch that inserts no rows at all returns an empty list on every database.

Return

A list of ResultRow representing data from each newly inserted row.

Parameters

data

Collection of values to use in the batch insert.

ignore

Whether to ignore errors or not. Note ignore is not supported by all vendors. Please check the documentation.

shouldReturnGeneratedValues

Specifies whether newly generated values (for example, auto-incremented IDs) should be returned. See Batch Insert for more details.

Samples

org.jetbrains.exposed.v1.r2dbc.sql.tests.shared.dml.InsertTests.testBatchInsert01


suspend fun <T : Table, E> T.batchInsert(data: Iterable<E>, useMultiRowValues: Boolean, ignore: Boolean = false, shouldReturnGeneratedValues: Boolean = true, body: BatchInsertStatement.(E) -> Unit): List<ResultRow>

Represents the SQL statement that batch inserts new rows into a table, either by using a single multi-row INSERT ... VALUES (...), (...), ... statement, or by executing one bound statement per row.

Relying on this specific INSERT syntax instead of the driver's statement-level batching mechanisms may be recommended by certain drivers for improved performance optimization. Some databases, like Oracle and MySQL, may not support returning multiple generated key values when this insert syntax is used.

Note: On most databases, a batch with ignore enabled that inserts only some of its rows returns only the rows that were successfully inserted. However, on H2 and MariaDB, such a batch returns a row for every value in data rather than only the inserted ones, and pairs the values the database generated with the wrong rows. This occurs because their drivers report no update count per statement, leaving nothing to tell the skipped rows apart by. Any batch that inserts no rows at all returns an empty list on every database.

Return

A list of ResultRow representing data from each newly inserted row.

Parameters

data

Collection of values to use in the batch insert.

useMultiRowValues

Whether to return a single INSERT statement that uses multi-row values constructor, like INSERT ... VALUES (...), (...), ...; if false, a regular statement will be prepared for driver batching.

ignore

Whether to ignore errors or not. Note ignore is not supported by all vendors. Please check the documentation.

shouldReturnGeneratedValues

Specifies whether newly generated values (for example, auto-incremented IDs) should be returned. See Batch Insert for more details.

Samples

org.jetbrains.exposed.v1.r2dbc.sql.tests.shared.dml.InsertTests.testBatchInsert01


suspend fun <T : Table, E> T.batchInsert(data: Sequence<E>, ignore: Boolean = false, shouldReturnGeneratedValues: Boolean = true, body: BatchInsertStatement.(E) -> Unit): List<ResultRow>

Represents the SQL statement that batch inserts new rows into a table.

A single INSERT statement will be prepared and parameterized with new bindings for each row in data, relying entirely on the underlying driver's statement-level batching mechanisms. The Exposed logger will still represent this batching operation by logging a new INSERT SQL line per row.

Alternatively, a single INSERT statement that uses multi-row values constructor for batch inserting can be created by Exposed by setting useMultiRowValues = true.

Note: On most databases, a batch with ignore enabled that inserts only some of its rows returns only the rows that were successfully inserted. However, on H2 and MariaDB, such a batch returns a row for every value in data rather than only the inserted ones, and pairs the values the database generated with the wrong rows. This occurs because their drivers report no update count per statement, leaving nothing to tell the skipped rows apart by. Any batch that inserts no rows at all returns an empty list on every database.

Return

A list of ResultRow representing data from each newly inserted row.

Parameters

data

Sequence of values to use in the batch insert.

ignore

Whether to ignore errors or not. Note ignore is not supported by all vendors. Please check the documentation.

shouldReturnGeneratedValues

Specifies whether newly generated values (for example, auto-incremented IDs) should be returned. See Batch Insert for more details.

Samples

org.jetbrains.exposed.v1.r2dbc.sql.tests.shared.dml.InsertTests.testBatchInsertWithSequence


suspend fun <T : Table, E> T.batchInsert(data: Sequence<E>, useMultiRowValues: Boolean, ignore: Boolean = false, shouldReturnGeneratedValues: Boolean = true, body: BatchInsertStatement.(E) -> Unit): List<ResultRow>

Represents the SQL statement that batch inserts new rows into a table, either by using a single multi-row INSERT ... VALUES (...), (...), ... statement, or by executing one bound statement per row.

Relying on this specific INSERT syntax instead of the driver's statement-level batching mechanisms may be recommended by certain drivers for improved performance optimization. Some databases, like Oracle and MySQL, may not support returning multiple generated key values when this insert syntax is used.

Note: On most databases, a batch with ignore enabled that inserts only some of its rows returns only the rows that were successfully inserted. However, on H2 and MariaDB, such a batch returns a row for every value in data rather than only the inserted ones, and pairs the values the database generated with the wrong rows. This occurs because their drivers report no update count per statement, leaving nothing to tell the skipped rows apart by. Any batch that inserts no rows at all returns an empty list on every database.

Return

A list of ResultRow representing data from each newly inserted row.

Parameters

data

Sequence of values to use in the batch insert.

useMultiRowValues

Whether to return a single INSERT statement that uses multi-row values constructor, like INSERT ... VALUES (...), (...), ...; if false, a regular statement will be prepared for driver batching.

ignore

Whether to ignore errors or not. Note ignore is not supported by all vendors. Please check the documentation.

shouldReturnGeneratedValues

Specifies whether newly generated values (for example, auto-incremented IDs) should be returned. See Batch Insert for more details.

Samples

org.jetbrains.exposed.v1.r2dbc.sql.tests.shared.dml.InsertTests.testBatchInsert01

Generated by Dokka © 2026 Copyright