SET statement for setting variables

更新时间:
复制 MD 格式

Use the SET statement to assign values to user-defined variables, session variables, and global variables.

Syntax

SET variable = expr [, variable = expr] ...

variable: {
    user_var_name
  | {GLOBAL | @@GLOBAL.} system_var_name
  | [SESSION | @@SESSION. | @@] system_var_name
}

You can assign multiple variables in a single SET statement by separating each assignment with a comma.

Variable scopes

ScopeSyntaxEffect
User-defined@var_nameScoped to the current session
SessionSESSION, @@SESSION., or @@Applies to the current session only
GlobalGLOBAL or @@GLOBAL.Applies to all connections; persisted across restarts

Global variable behavior

When you run SET GLOBAL, PolarDB-X persists the value. The configuration takes effect after the instance is restarted. After the global variable is set, the configuration takes effect for all existing connections.

Examples

Set a user-defined variable

SET @foo = 'bar';
Query OK, 0 rows affected (0.00 sec)
SELECT @foo;
+------+
| @foo |
+------+
| bar  |
+------+
1 row in set (0.01 sec)

Set a session variable

SET @@time_zone = '+09:00';
Query OK, 0 rows affected (0.01 sec)
SELECT @@time_zone;
+-------------+
| @@time_zone |
+-------------+
| +09:00      |
+-------------+
1 row in set (0.00 sec)

Set a global variable

SET GLOBAL time_zone = '+09:00';
Query OK, 0 rows affected (0.04 sec)
SELECT @@GLOBAL.time_zone;
+--------------------+
| @@global.time_zone |
+--------------------+
| +09:00             |
+--------------------+
1 row in set (0.02 sec)