SQL syntax guide

Updated at:

Unified Service supports standard ANSI SQL for Data Manipulation Language (DML). It also provides custom extensions for specific API development needs, such as parameter declaration, dynamic SQL, and range parameter generation. This topic describes the supported SQL syntax.

Limits

  • Only SELECT operations are supported.

  • Transactions are not supported.

  • Table and column names are case-insensitive by default.

Basic syntax

[ WITH with_subquery [, ...] ]
SELECT
[ [ ALL | DISTINCT ] | select_expr [ AS output_name ] [, ...] ] 
[ FROM table_reference [, ...] ]
[ WHERE condition ]
[ GROUP BY expression [, ...] ]
[ HAVING condition ]
[ { UNION | ALL | INTERSECT | EXCEPT | MINUS } query ]
[ ORDER BY expression
[ ASC | DESC ]
[ LIMIT { number | ALL } ]

The preceding code block shows the basic syntax and supported operations. The specific syntax that you can use depends on the data source. For more information, see the SQL syntax documentation for your target data source.

Parameter declaration

In addition to basic SQL syntax, Unified Service lets you declare parameters for calls using the triplet format `${<parameter_name>,<type>,<default_value>}`.

Example: The following SQL statement defines an API that declares a numeric parameter `uid` and a string array parameter `unArr`. The default value of `uid` is set to `1`.

SELECT name, addr, tel
FROM table_name
WHERE user_id = ${uid,Number,1} 
and user_name in ${unArr,Array,a,b};

Supported parameter types

Type

Description

Corresponding database types

Default value (optional)

Example

Number

Number

int, Integer, Double, BigInt, etc.

Single value

${uid,Number,1}

String

String

Char, Varchar, Text, String, etc.

Single value. Quotation marks are not required.

${tel,String,177723_test}

Array

String array

Char, Varchar, Text, String, etc.

Separate multiple values with commas.

${names,Array,a,b}

Array_Num

Numeric array

int, Integer, Double, BigInt, etc.

Separate multiple values with commas.

${uids,Array_Num,1,2,3}

date

Date type. The caller is responsible for ensuring the correct date format.

Date, DateTime, TimeStamp, etc.

Single value. Quotation marks are not required.

${date,Date,2022-12-30 12:00:00}

Dynamic SQL

Unified Service supports dynamic SQL with the built-in user-defined function `NotRequired()`. You can use this function to define optional parameters with the following syntax: `NotRequired(<field> <operator> ${<parameter_name>,<type>,<default_value>})`.

Example: The following SQL statement defines an API with a numeric parameter `uid` and an optional string array parameter `unArr`. The default value of `uid` is `1`. If the request does not include the `unArr` parameter, the `user_name in ${unArr,Array,}` condition is ignored.

SELECT name, addr, tel
FROM table_name
WHERE user_id = ${uid,Number,1} 
and NotRequired(user_name in ${unArr,Array,});