API SQL scripts
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
SELECTstatements. 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
ListorGetAPIs, you cannot use non-DQL statements such asINSERT,UPDATE,CREATE, andDELETE.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 asSELECT t.name AS name FROM table.Aggregate functions: When you use an aggregate function such as
SUM(),COUNT(),MAX(), orMIN(), 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.
NoteIf 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
PageStartandPageSizeare 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 formatvar_cols_xxx, such asvar_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
<<>><=<=>=>=Data source-specific limitations:
Data source
Limitations
TDengine
You must use
LIMIT/OFFSETin SQL to implement pagination.Optional parameters are not supported.
SAP HANA
Pagination is not supported. The
PageStartandPageSizeparameters have no effect.Optional parameters are not supported.
Elasticsearch
Elasticsearch uses
ScrollIdfor deep pagination. Without aScrollId, you can only retrieve the first 10,000 records using thePageStartandPageSizeparameters. Attempting to retrieve records beyond this limit will cause the API call to fail.For example, if
PageStartis set to9998, the maximum value forPageSizeis2.To query data beyond the first 10,000 records, you must specify a
scrollIdin theWHEREclause. You can define ascrollIdrequest parameter when developing the API and pass the correspondingscrollIdvalue in your request.NoteWhen querying with a
scrollId, you cannot use standard pagination. Do not pass values forPageStartandPageSize, or the call will fail.The
WHEREclause only supportsscrollIdas 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
JOINstatements 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.
ImportantModifying 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
QUALIFYclause is not supported in basic SQL mode.OpenSearch
Simple subqueries are supported, but multi-level nested
INsubqueries 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
JOINmay cause the query result to be empty.For example:
SELECT ... FROM (SELECT ... FROM t1 LEFT JOIN t2 ON ...) e1 WHERE ...;.The
WITHsyntax for common table expressions is not supported.For example:
WITH test_with AS (...) SELECT ...;.UNIONandUNION ALLstatements 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
COALESCEsyntax has compatibility issues with the current JDBC driver. We recommend that you use theCASE WHENsyntax as an alternative.Replace
SELECT COALESCE(id, 0) FROM ...;withSELECT CASE WHEN id IS NULL THEN 0 ELSE id END FROM ...;.When you use a
CASE WHENexpression with aJOINclause, 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, andDELETEstatements. 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
incondition parameter, each record is executed separately, which results in poor performance. We recommend that you avoid using theincondition parameter.Only PostgreSQL supports the
RETURNINGclause.