Update data with SQL

Updated at:

Tablestore SDK for Go executes an UPDATE statement to update one row in a data table that does not have local transactions enabled.

Prerequisites

  • Install Tablestore SDK for Go and initialize a client. SQL DML requires version 1.9.2 or later. We recommend that you use the latest version.

  • For the data table that you want to access, create a mapping table, and make sure that local transactions are not enabled.

  • If you use a RAM user, grant the SQL_DML permission to the RAM user.

Description

Call the SQLQuery method to execute an UPDATE statement and update one row. The WHERE clause must specify all primary key columns of the row. Primary key columns cannot be updated.

func (client *TableStoreClient) SQLQuery(request *SQLQueryRequest) (*SQLQueryResponse, error)

The following example increments the score column of a row in example_table by 5.

request := &tablestore.SQLQueryRequest{Query: `
UPDATE example_table
SET score = score + 5
WHERE tenant_id = 1 AND pk = 'row1'`}

response, err := client.SQLQuery(request)
if err != nil {
    log.Fatalf("SQLQuery failed: %v", err)
}
fmt.Println("AffectedRows:", response.AffectedRows)

Parameters

SQLQueryRequest contains the following parameters.

Name

Type

Description

Query (required)

string

The UPDATE statement to execute. The statement specifies the mapping table, attribute columns and values to update, and the complete primary key of the target row.

Response

SQLQueryResponse contains the following fields that are directly related to the operation result.

Field

Type

Description

AffectedRows

int64

The number of affected rows.

StmtType

SQLStatementType

The SQL statement type. This operation returns SQL_UPDATE.