All Products
Search
Document Center

Tablestore:Query data by executing SQL statements

Last Updated:May 26, 2025

This topic describes how to use Tablestore SDK for .NET to query data by executing SQL statements.

Usage notes

The SQL query feature is supported by Tablestore SDK for .NET V5.0.0 and later. When you use the SQL query feature, make sure that a supported version of Tablestore SDK for .NET is installed.

For more information, see Version history of Tablestore SDK for .NET.

Prerequisites

Example

The following example shows how to use the select pk0,pk1,col0,col1,date_col,geo_col from test_table limit 20 statement to query data in the test_table table and return a maximum of 20 rows of data. The system returns the request type of the query statement, the schema of the return value, and the returned results.

/// <summary>
/// Query data.
/// </summary>
/// <param name="otsClient"></param>
public static void QueryData(OTSClient otsClient)
{
    SQLQueryRequest sqlQueryRequest = new SQLQueryRequest("select pk0,pk1,col0,col1,date_col,geo_col from test_table limit 20");

    SQLQueryResponse sqlQueryResponse = otsClient.SQLQuery(sqlQueryRequest);

    SQLTableMeta sqlTableMeta = sqlQueryResponse.GetSQLResultSet().GetSQLTableMeta();
    Console.WriteLine(JsonConvert.SerializeObject(sqlTableMeta.GetSchema()));

    ISQLResultSet resultSet = sqlQueryResponse.GetSQLResultSet();
    while (resultSet.HasNext())
    {
        ISQLRow row = resultSet.Next();
        Console.WriteLine(row.GetString("pk0") + " , " + row.GetLong("pk1") + " , " + row.GetString("col0") + " , " +
                          row.GetLong("col1") + " , " + row.GetString("date_col") + " , " + row.GetString("geo_col"));
    }
}

FAQ

References