API SQL scripts

Updated at:

This document explains how to write SQL scripts for query-type and operation-type APIs.

Get/List (query type)

  • Supported query features:

    • You can perform single-table queries, multi-table joins (JOIN), and nested queries (subqueries) within the same data source.

    • You can use advanced SQL mode (MyBatis style).

    • You can use parameters in SELECT statements. For example: SELECT id_card, SUM(CASE WHEN id_card LIKE ${id_card} THEN 1 ELSE 0 END) AS proj_score FROM table WHERE c LIKE ${id_card} GROUP BY id_card.

  • Unsupported syntax and usage:

    • Multiple SQL query statements: You cannot execute multiple SQL statements in a single API, such as SELECT ...; SELECT ...;.

    • Non-DQL statements: In List or Get APIs, you cannot use non-DQL statements such as INSERT, UPDATE, CREATE, and DELETE.

    • SELECT *: Do not use SELECT * queries. You must explicitly specify the columns to query.

    • Column name prefixes: If a column name includes a table prefix (for example, t.name), you must use an alias as the response parameter name, such as SELECT t.name AS name FROM table.

    • Aggregate functions: When you use an aggregate function such as SUM(), COUNT(), MAX(), or MIN() , you must use an alias as the response parameter name. For example: SELECT SUM(num) AS total_num FROM table.

    • Do not use a pagination statement in the SQL script. Instead, implement pagination by using pagination parameters.

      Note
      • If the data source is SAP HANA, use pagination statements in SQL and define pagination parameters to implement pagination.

      • For TDengine data sources, you must use a pagination statement in the SQL script. If pagination parameters such as PageStart and PageSize are not provided, all data is returned. If they are provided, data is paginated according to the specified values.

  • In advanced SQL mode, dynamic field queries allow you to dynamically specify response fields by using parameters.

    • Parameter format: The parameter name must start with var_cols_ and follow the format var_cols_xxx, such as var_cols_args. For example: SELECT id, ${var_cols_args} FROM table1.

    • Response parameters: You must declare all supported dynamic query fields as response parameters.

    • Calling an API: When you call the API, pass the fields that you want to query in the dynamic parameters. Any fields that are not passed will have a null value in the response. For example: var_cols_args=name,age,dept.

    • Actual execution: The resulting SQL statement is: SELECT id, name, age, dept FROM table1.

  • In advanced SQL mode, comparison operators in MyBatis SQL must be escaped.

    Original operator

    Escaped operator

    <

    &lt;

    >

    &gt;

    <=

    &lt;=

    >=

    &gt;=

  • Data source-specific limitations:

    Data source

    Limitations

    TDengine

    • You must use LIMIT/OFFSET in SQL to implement pagination.

    • Optional parameters are not supported.

    SAP HANA

    • Pagination is not supported. The PageStart and PageSize parameters have no effect.

    • Optional parameters are not supported.

    Elasticsearch

    • Elasticsearch uses ScrollId for deep pagination. Without a ScrollId, you can only retrieve the first 10,000 records using the PageStart and PageSize parameters. Attempting to retrieve records beyond this limit will cause the API call to fail.

      For example, if PageStart is set to 9998, the maximum value for PageSize is 2.

    • To query data beyond the first 10,000 records, you must specify a scrollId in the WHERE clause. You can define a scrollId request parameter when developing the API and pass the corresponding scrollId value in your request.

      Note

      When querying with a scrollId, you cannot use standard pagination. Do not pass values for PageStart and PageSize, or the call will fail.

    • The WHERE clause only supports scrollId as a condition. Other fields in the clause are ignored.

      For example: select a from table where scrollId=${scrollId}.

    • Subqueries are not supported.

    PostgreSQL

    Cross-schema queries are not supported. You can only query data within the current connection's schema.

    Impala

    advanced SQL mode (MyBatis style) is not supported.

    Lindorm

    JOIN statements are not supported.

    DolphinDB

    The native DolphinDB mode is not supported. To use DolphinDB, you must create a data source and set its SQL compatibility mode to either Oracle or MySQL.

    Important

    Modifying the compatibility mode of a data source may cause unpredictable errors in existing APIs. Adjust this setting based on your business requirements.

    GBase 8a

    The QUALIFY clause is not supported in basic SQL mode.

    OpenSearch

    • Simple subqueries are supported, but multi-level nested IN subqueries may not execute correctly.

      For example: SELECT ... WHERE age IN (SELECT age FROM ... WHERE age IN (18,39));.

    • When you run a query by using JDBC, a subquery that contains JOIN may cause the query result to be empty.

      For example: SELECT ... FROM (SELECT ... FROM t1 LEFT JOIN t2 ON ...) e1 WHERE ...;.

    • The WITH syntax for common table expressions is not supported.

      For example: WITH test_with AS (...) SELECT ...;.

    • UNION and UNION ALL statements are not supported, even if they are included in a subquery.

      For example: SELECT id FROM (SELECT id FROM t1 UNION SELECT id FROM t2) t;.

    • The COALESCE syntax has compatibility issues with the current JDBC driver. We recommend that you use the CASE WHEN syntax as an alternative.

      Replace SELECT COALESCE(id, 0) FROM ...; with SELECT CASE WHEN id IS NULL THEN 0 ELSE id END FROM ...;.

    • When you use a CASE WHEN expression with a JOIN clause, field parsing may fail or columns may not be found. We recommend that you verify syntax compatibility before you build complex queries.

      For example: SELECT CASE WHEN a.id= ... END FROM t1 a JOIN t2 b ON ...;.

Create/Update/Delete (operation type)

  • Supported SQL modes:

    • Basic SQL mode: Supports standard INSERT, UPDATE, and DELETE statements. This mode uses the ${param} placeholder to pass parameters and is suitable for simple scenarios.

    • Advanced SQL mode (MyBatis style): Use this mode for complex logic such as conditional statements, dynamic fields, and batch operations. The following tags are supported:

      <if>, <choose>, <when>, <otherwise>, <trim>, <foreach>
  • Batch operations in advanced SQL mode process records individually, which can cause poor performance. Therefore, avoid using this mode for batch create, update, or delete APIs whenever possible.

  • If an SQL statement contains an in condition parameter, each record is executed separately, which results in poor performance. We recommend that you avoid using the in condition parameter.

  • Only PostgreSQL supports the RETURNING clause.