Skip to main content

SET

Description

This statement is used to set variables.

Syntax

SET [SESSION | GLOBAL] var_and_val_list;

var_and_val_list:
var_and_val [,var_and_val ...]

var_and_val:
var_value {TO | =} set_expr_or_default
| var_value = (simple_select)

Parameters

ParameterDescription
SESSION | GLOBALSpecifies whether to set a session-level or global variable. The default is session-level.
var_valueThe name of the variable.

Examples

  • Set the user variable a.

    SET @a = 1;
  • Set the global system variable secure_file_priv, which specifies the path for importing or exporting files.

    SET GLOBAL secure_file_priv = '';
  • Set the user variable proxy_route_policy, which is used to configure a routing policy.

    SET @proxy_route_policy = 'follower_first';
  • Set the variable value by using the SELECT statement.

    CREATE TABLE tbl1(col INT);
    Query OK, 0 rows affected (0.049 sec)

    INSERT into tbl1 VALUES('1'),('2'),('3'),('4'),('5');
    Query OK, 5 rows affected (0.037 sec)
    Records: 5 Duplicates: 0 Warnings: 0

    SET @var1=(SELECT COUNT(*) FROM tbl1);
    Query OK, 0 rows affected (0.069 sec)