Usage notes
- User-defined variables are variables, which can be created by the user and exist in the session. This means that no one can access user-defined variables that have been declared by another user, and when the session is closed these variables expire.
- The database does not support using the SHOW statement to display existing user-defined variables.
- The following types of values cannot be declared as user-defined variables: BITMAP, HLL, and PERCENTILE. User-defined variables of the JSON type are converted to the STRING type for storage.
- From v3.2.7 onwards, the database supports the ARRAY type as a user-defined variable.
Declare user-defined variables
Syntax
NOTE
- All variables must be preceded by a single at sign (@).
- Multiple variables can be declared in the same SET statement and need to be separated with commas (
,).- You can declare the same variable multiple times. The newly declared value overwrites the original value.
- If an undeclared variable is used, the value of the variable is
NULLby default, and the NULL type is STRING.
Parameters
Examples
Example 1: Declare a number as a user-defined variable.Use user-defined variables in SQL
-
Simplifies the writing of SQL statements. For example, when you execute the following SELECT statement, the database parses
@varas1. -
Avoids duplicate computation. For example, when you execute the following SELECT statement, the database parses
@varas the result returned by theselect sum(c1) from tblcommand.