Executes a batch of SQL DML statements. This method allows many statements to be run with lower latency than submitting them sequentially with sessions.executeSql.
Statements are executed in sequential order. A request can succeed even if a statement fails. The ExecuteBatchDmlResponse.status field in the response provides information about the statement that failed. Clients must inspect this field to determine whether an error occurred.
Execution stops after the first failed statement; the remaining statements are not executed.
Required. The transaction to use. Must be a read-write transaction.
To protect against replays, single-use transactions are not supported. The caller must either supply an existing transaction ID or begin a new transaction.
Required. The list of statements to execute in this batch. Statements are executed serially, such that the effects of statement i are visible to statement i+1. Each statement must be a DML statement. Execution stops at the first failed statement; the remaining statements are not executed.
Required. A per-transaction sequence number used to identify this request. This field makes each request idempotent such that if the request is received multiple times, at most one succeeds.
The sequence number must be monotonically increasing within the transaction. If a request arrives for the first time with an out-of-order sequence number, the transaction might be aborted. Replays of previously handled requests yield the same response as the first execution.
Optional. If set to true, this request marks the end of the transaction. After these statements execute, you must commit or abort the transaction. Attempts to execute any other requests against this transaction (including reads and queries) are rejected.
Setting this option might cause some error reporting to be deferred until commit time (for example, validation of unique constraints). Given this, successful execution of statements shouldn't be assumed until a subsequent sessions.commit call completes successfully.
Response body
The response for sessions.executeBatchDml. Contains a list of ResultSet messages, one for each DML statement that has successfully executed, in the same order as the statements in the request. If a statement fails, the status in the response body identifies the cause of the failure.
To check for DML statements that failed, use the following approach:
Check the status in the response message. The google.rpc.Code enum value OK indicates that all statements were executed successfully.
If the status was not OK, check the number of result sets in the response. If the response contains NResultSet messages, then statement N+1 in the request failed.
Example 1:
Request: 5 DML statements, all executed successfully.
Response: 5 ResultSet messages, with the status OK.
Example 2:
Request: 5 DML statements. The third statement has a syntax error.
Response: 2 ResultSet messages, and a syntax error (INVALID_ARGUMENT) status. The number of ResultSet messages indicates that the third statement failed, and the fourth and fifth statements were not executed.
If successful, the response body contains data with the following structure:
One ResultSet for each statement in the request that ran successfully, in the same order as the statements in the request. Each ResultSet does not contain any rows. The ResultSetStats in each ResultSet contain the number of rows modified by the statement.
Optional. A precommit token is included if the read-write transaction is on a multiplexed session. Pass the precommit token with the highest sequence number from this transaction attempt should be passed to the sessions.commit request for this transaction.
Parameter names and values that bind to placeholders in the DML string.
A parameter placeholder consists of the @ character followed by the parameter name (for example, @firstName). Parameter names can contain letters, numbers, and underscores.
Parameters can appear anywhere that a literal value is expected. The same parameter name can be used more than once, for example:
"WHERE id > @msg_id AND id < @msg_id + 100"
It's an error to execute a SQL statement with unbound parameters.
It isn't always possible for Cloud Spanner to infer the right SQL type from a JSON value. For example, values of type BYTES and values of type STRING both appear in params as JSON strings.
In these cases, paramTypes can be used to specify the exact SQL type for some or all of the SQL statement parameters. See the definition of Type for more information about SQL types.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-06-18 UTC."],[],[],null,["# Method: projects.instances.databases.sessions.executeBatchDml\n\n- [HTTP request](#body.HTTP_TEMPLATE)\n- [Path parameters](#body.PATH_PARAMETERS)\n- [Request body](#body.request_body)\n - [JSON representation](#body.request_body.SCHEMA_REPRESENTATION)\n- [Response body](#body.response_body)\n - [JSON representation](#body.ExecuteBatchDmlResponse.SCHEMA_REPRESENTATION)\n- [Authorization scopes](#body.aspect)\n- [Statement](#Statement)\n - [JSON representation](#Statement.SCHEMA_REPRESENTATION)\n- [Try it!](#try-it)\n\nExecutes a batch of SQL DML statements. This method allows many statements to be run with lower latency than submitting them sequentially with [sessions.executeSql](/spanner/docs/reference/rest/v1/projects.instances.databases.sessions/executeSql#google.spanner.v1.Spanner.ExecuteSql).\n\nStatements are executed in sequential order. A request can succeed even if a statement fails. The [ExecuteBatchDmlResponse.status](/spanner/docs/reference/rest/v1/projects.instances.databases.sessions/executeBatchDml#body.ExecuteBatchDmlResponse.FIELDS.status) field in the response provides information about the statement that failed. Clients must inspect this field to determine whether an error occurred.\n\nExecution stops after the first failed statement; the remaining statements are not executed.\n\n### HTTP request\n\nChoose a location: \nglobal europe-west8 me-central2 us-central1 us-central2 us-east1 us-east4 us-east5 us-south1 us-west1 us-west2 us-west3 us-west4 us-west8 us-east7 \n\n\u003cbr /\u003e\n\nThe URLs use [gRPC Transcoding](https://google.aip.dev/127) syntax.\n\n### Path parameters\n\n### Request body\n\nThe request body contains data with the following structure:\n\n### Response body\n\nThe response for [sessions.executeBatchDml](/spanner/docs/reference/rest/v1/projects.instances.databases.sessions/executeBatchDml#google.spanner.v1.Spanner.ExecuteBatchDml). Contains a list of [ResultSet](/spanner/docs/reference/rest/v1/ResultSet) messages, one for each DML statement that has successfully executed, in the same order as the statements in the request. If a statement fails, the status in the response body identifies the cause of the failure.\n\nTo check for DML statements that failed, use the following approach:\n\n1. Check the status in the response message. The [google.rpc.Code](/spanner/docs/reference/rest/v1/Code) enum value `OK` indicates that all statements were executed successfully.\n2. If the status was not `OK`, check the number of result sets in the response. If the response contains `N` [ResultSet](/spanner/docs/reference/rest/v1/ResultSet) messages, then statement `N+1` in the request failed.\n\nExample 1:\n\n- Request: 5 DML statements, all executed successfully.\n- Response: 5 [ResultSet](/spanner/docs/reference/rest/v1/ResultSet) messages, with the status `OK`.\n\nExample 2:\n\n- Request: 5 DML statements. The third statement has a syntax error.\n- Response: 2 [ResultSet](/spanner/docs/reference/rest/v1/ResultSet) messages, and a syntax error (`INVALID_ARGUMENT`) status. The number of [ResultSet](/spanner/docs/reference/rest/v1/ResultSet) messages indicates that the third statement failed, and the fourth and fifth statements were not executed.\n\nIf successful, the response body contains data with the following structure:\n\n### Authorization scopes\n\nRequires one of the following OAuth scopes:\n\n- `https://www.googleapis.com/auth/spanner.data`\n- `https://www.googleapis.com/auth/cloud-platform`\n\nFor more information, see the [Authentication Overview](/docs/authentication#authorization-gcp).\n\nStatement\n---------\n\nA single DML statement."]]