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.
HTTP request
POST https://spanner.googleapis.com/v1/{session=projects/*/instances/*/databases/*/sessions/*}:executeBatchDml
The URL uses gRPC Transcoding syntax.
Path parameters
Parameters | |
---|---|
session |
Required. The session in which the DML statements should be performed. Authorization requires the following IAM permission on the specified resource
|
Request body
The request body contains data with the following structure:
JSON representation |
---|
{ "transaction": { object ( |
Fields | |
---|---|
transaction |
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. |
statements[] |
Required. The list of statements to execute in this batch. Statements are executed serially, such that the effects of statement Callers must provide at least one statement. |
seqno |
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 will succeed. 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 may be aborted. Replays of previously handled requests will yield the same response as the first execution. |
request |
Common options for this request. |
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 valueOK
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 containsN
ResultSet
messages, then statementN+1
in the request failed.
Example 1:
- Request: 5 DML statements, all executed successfully.
- Response: 5
ResultSet
messages, with the statusOK
.
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 ofResultSet
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:
JSON representation |
---|
{ "resultSets": [ { object ( |
Fields | |
---|---|
result |
One Only the first |
status |
If all DML statements are executed successfully, the status is |
Authorization scopes
Requires one of the following OAuth scopes:
https://www.googleapis.com/auth/spanner.data
https://www.googleapis.com/auth/cloud-platform
For more information, see the Authentication Overview.
Statement
A single DML statement.
JSON representation |
---|
{
"sql": string,
"params": {
object
},
"paramTypes": {
string: {
object ( |
Fields | |
---|---|
sql |
Required. The DML string. |
params |
Parameter names and values that bind to placeholders in the DML string. A parameter placeholder consists of the Parameters can appear anywhere that a literal value is expected. The same parameter name can be used more than once, for example:
It is an error to execute a SQL statement with unbound parameters. |
param |
It is not always possible for Cloud Spanner to infer the right SQL type from a JSON value. For example, values of type In these cases, |