API SQL script editing
This topic explains how to edit API SQL scripts for query operations (Get/List) and data manipulation operations (Create/Update/Delete).
Get/List (query operations)
Supported query modes:
Supports single-table queries, multi-table joins (JOIN), and subqueries within the same data source.
Supports advanced SQL mode (MyBatis style).
You can configure parameters in the
SELECTclause. 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 statements: You cannot execute multiple SQL statements in a single API, such as
SELECT ...; SELECT ...;.Non-DQL statements: In APIs for Get/List operations, non-Data Query Language (DQL) statements such as
INSERT,UPDATE,CREATE, andDELETEare not supported.SELECT *: The
SELECT *query is not supported. You must explicitly specify the columns to query.Column prefixes: If a column name includes a table prefix, such as
t.name, you must use an alias for the response parameter. For example:SELECT 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 for the response parameter. For example:SELECT SUM(num) AS total_num FROM table.Pagination statements: Do not use pagination statements in SQL. Use pagination parameters instead.
NoteFor an SAP HANA data source, pagination is not supported, and passing pagination parameters has no effect.
For a TDengine data source, you must use a pagination statement in the SQL. If you do not pass pagination parameters (PageStart and PageSize), the API returns all data. If you pass pagination parameters, the API returns data based on the defined pagination parameters.
Dynamic column queries (in advanced SQL mode) let you dynamically specify response columns by including parameters in the query statement.
Parameter format: The parameter must start with
var_cols, in the formatvar_cols_xxx(for example,var_cols_args). For example:SELECT id, ${var_cols_args} FROM table1.Response parameters: You must add all supported dynamic columns as response parameters.
Call the API: When calling the API, pass the fields that you want to query in the dynamic parameters. Fields that are not passed will have a null value in the returned result. For example:
var_cols_args=name,age,dept.Executed SQL: The actual SQL executed is:
SELECT id, name, age, dept FROM table1.
When using advanced SQL mode, you must use escaped characters for comparison operators in MyBatis SQL.
Original operator
Escaped character
<<>><=<=>=>=Data source-specific limitations:
Data source
Limitations
TDengine
You must use
LIMIT/OFFSETin the SQL statement to implement pagination.Optional parameters are not supported.
SAP HANA
Pagination is not supported. Passing
PageStartandPageSizeparameters has no effect.Optional parameters are not supported.
Elasticsearch
Pagination is implemented by using
ScrollId. If you do not specify aScrollId, you can query a maximum of 10,000 records. When you set thePageStartandPageSizepagination parameters, ensure that the query does not exceed 10,000 records. Otherwise, the call fails.For example, if
PageStartis set to 9998, the maximum value forPageSizeis 2.To query records beyond the first 10,000, you must use a
WHEREclause to specify ascrollId. You can define ascrollIdrequest parameter when you create the API and pass the correspondingscrollIdvalue when you run a query.NoteWhen you specify a
scrollIdfor a query, you cannot use pagination. Do not pass values forPageStartandPageSize. Otherwise, the call fails.In the
WHEREclause, only thescrollIdfield is supported as a query condition. Other fields are ignored.For example:
select a from table where scrollId=${scrollId}.Subqueries are not supported.
HBase
Only single
rowkeyqueries are supported. Therowkeyis a required request parameter. The query syntax is:select info1.id as name from table where rowkey = ${rowkey}The
ORDER BYclause is not supported.Pagination is not supported. Passing
PageStartandPageSizeparameters has no effect.The
JOINstatement is not supported.
PostgreSQL
Cross-schema queries are not supported. You can only query data within the schema of the current connection.
Impala
Advanced SQL mode (MyBatis style) is not supported.
Lindorm
The
JOINstatement is not supported.DolphinDB
DolphinDB supports only the Oracle and MySQL SQL Compatibility Modes. The native DolphinDB mode is not supported. When you create a data source, you must set the SQL Compatibility Mode to Oracle or MySQL to use it for API creation.
ImportantModifying the compatibility mode of the data source may cause unpredictable errors in existing APIs. Adjust this setting based on your business requirements.
GBase 8a
The
QUALIFYstatement is not supported in basic SQL mode.OpenSearch
Simple subqueries are supported. Multi-level nested
INsubqueries may not execute correctly.For example:
SELECT ... WHERE age IN (SELECT age FROM ... WHERE age IN (18,39));.When you query data by using JDBC, a subquery that contains a
JOINmay return an empty result set.For example:
SELECT ... FROM (SELECT ... FROM t1 LEFT JOIN t2 ON ...) e1 WHERE ...;.The
WITHclause, used for common table expressions, is not supported.For example:
WITH test_with AS (...) SELECT ...;.The
UNIONandUNION ALLstatements are not supported, even within subqueries.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 using theCASE WHENsyntax as an alternative.Replace
SELECT COALESCE(id, 0) FROM ...;withSELECT CASE WHEN id IS NULL THEN 0 ELSE id END FROM ...;.Using a
CASE WHENexpression with aJOINstatement may cause field parsing errors or "column not found" errors. Verify syntax compatibility before you build complex queries.For example:
SELECT CASE WHEN a.id=... END FROM t1 a JOIN t2 b ON ...;.
Hive
If a Hive table is a data lake table, for example, with a format like Iceberg or Hudi, queries use the Spark engine. If it is a standard Hive table, for example, with a file format like ORC, Parquet, or TextFile, queries use the Hive engine.
Create/Update/Delete (data manipulation operations)
Supported SQL modes:
basic SQL mode: Supports standard
INSERT,UPDATE, andDELETEstatements. Use the${param}placeholder to pass parameters.Advanced SQL mode (MyBatis style): You can use this mode for complex logic, such as conditional statements, dynamic fields, and batch operations. The supported tags are as follows:
<if>, <choose>, <when>, <otherwise>, <trim>, <foreach>
When you perform batch operations in advanced SQL mode, each record is processed individually, which can lead to poor performance. Therefore, avoid using advanced SQL mode for APIs that perform batch inserts, updates, or deletes.
If an SQL statement contains an
incondition, each record is executed individually, resulting in poor performance. Avoid usinginconditions whenever possible.Only PostgreSQL supports the
RETURNINGclause.