Back to Exposed

batchInsert

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

1.5.09.5 KB
Original Source

batchInsert

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. Certain drivers, like for MySQL and PostgreSQL, may automatically rewrite a batched statement to use the optimized multi-row values constructor syntax. This behavior is usually dependent on the value set on specific connection configuration parameters. Regardless, 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.

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.tests.shared.dml.InsertTests.testBatchInsert01


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.

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


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. Certain drivers, like for MySQL and PostgreSQL, may automatically rewrite a batched statement to use the optimized multi-row values constructor syntax. This behavior is usually dependent on the value set on specific connection configuration parameters. Regardless, 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.

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.tests.shared.dml.InsertTests.testBatchInsertWithSequence


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.

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