Method: projects.instances.databases.sessions.executeBatchDml

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 order, sequentially. ExecuteBatchDmlResponse will contain a ResultSet for each DML statement that has successfully executed. If a statement fails, its error status will be returned as part of the ExecuteBatchDmlResponse. Execution will stop at the first failed statement; the remaining statements will not run.

sessions.executeBatchDml is expected to return an OK status with a response even if there was an error while processing one of the DML statements. Clients must inspect response.status to determine if there were any errors while processing the request.

See more details in ExecuteBatchDmlRequest and ExecuteBatchDmlResponse.

HTTP request

POST https://spanner.googleapis.com/v1/{session=projects/*/instances/*/databases/*/sessions/*}:executeBatchDml

The URL uses gRPC Transcoding syntax.

Path parameters

Parameters
session

string

Required. The session in which the DML statements should be performed.

Authorization requires the following Google IAM permission on the specified resource session:

  • spanner.databases.write

Request body

The request body contains data with the following structure:

JSON representation
{
  "transaction": {
    object (TransactionSelector)
  },
  "statements": [
    {
      object (Statement)
    }
  ],
  "seqno": string
}
Fields
transaction

object (TransactionSelector)

The transaction to use. A ReadWrite transaction is required. Single-use transactions are not supported (to avoid replay). The caller must either supply an existing transaction ID or begin a new transaction.

statements[]

object (Statement)

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 will stop at the first failed statement; the remaining statements will not run.

REQUIRES: statements_size() > 0.

seqno

string (int64 format)

A per-transaction sequence number used to identify this request. This is used in the same space as the seqno in ExecuteSqlRequest. See more details in ExecuteSqlRequest.

Response body

If successful, the response body contains data with the following structure:

The response for sessions.executeBatchDml. Contains a list of ResultSet, one for each DML statement that has successfully executed. If a statement fails, the error is returned as part of the response payload. Clients can determine whether all DML statements have run successfully, or if a statement failed, using one of the following approaches:

  1. Check if 'status' field is OkStatus.
  2. Check if result_sets_size() equals the number of statements in ExecuteBatchDmlRequest.

Example 1: A request with 5 DML statements, all executed successfully.

Result: A response with 5 ResultSets, one for each statement in the same order, and an OkStatus.

Example 2: A request with 5 DML statements. The 3rd statement has a syntax error.

Result: A response with 2 ResultSets, for the first 2 statements that run successfully, and a syntax error (INVALID_ARGUMENT) status. From result_set_size() client can determine that the 3rd statement has failed.

JSON representation
{
  "resultSets": [
    {
      object (ResultSet)
    }
  ],
  "status": {
    object (Status)
  }
}
Fields
resultSets[]

object (ResultSet)

ResultSets, one for each statement in the request that ran successfully, in the same order as the statements in the request. Each ResultSet will not contain any rows. The ResultSetStats in each ResultSet will contain the number of rows modified by the statement.

Only the first ResultSet in the response contains a valid ResultSetMetadata.

status

object (Status)

If all DML statements are executed successfully, status will be OK. Otherwise, the error status of the first failed statement.

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(Type)
    },
    ...
  }
}
Fields
sql

string

Required. The DML string.

params

object (Struct format)

The DML string can contain parameter placeholders. A parameter placeholder consists of '@' followed by the parameter name. Parameter names consist of any combination of 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 is an error to execute an SQL statement with unbound parameters.

Parameter values are specified using params, which is a JSON object whose keys are parameter names, and whose values are the corresponding parameter values.

paramTypes

map (key: string, value: object (Type))

It is not 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.

An object containing a list of "key": value pairs. Example: { "name": "wrench", "mass": "1.3kg", "count": "3" }.

Try it!

Оцените, насколько информация на этой странице была вам полезна:

Оставить отзыв о...

Текущей странице
Cloud Spanner Documentation