Lecture 62: Definition and Application of Variables in MySQL Stored Procedures

There are three types of variables in the MySQL database, namely system variables, user-defined variables, and local variables.

1. System variables

System variables are system variables provided by the MySQL database and belong to system variables at the service level. System variables are divided into two levels when used:

  • Global variables (global): Global variables take effect for all sessions, but after restarting MySQL, the modified system variable value will become invalid. If you want to permanently modify the value of the variable, you can declare it in the MySQL configuration file.

  • Local variables (session): Session variables are only valid for a single session, and will be invalid for another session window.

There are two levels of use of system variables. You can specify which level to use when using variables. If you do not specify the level of system variables explicitly, the default is session.

1.1. View the syntax format of system variables

1) View all system variables

SHOW [ SESSION | GLOBAL ] VARIABLES 

2) Find a certain system variable

SHOW [ SESSION | GLOBAL ] VARIABLES LIKE '变量名'

3) View the value of the specified variable

SELECT @@[SESSION | GLOBAL].变量名

1.2. Set the syntax format of the system variable value

There are two ways to set the value of a variable.

SET [ SESSION | GLOBAL ] 系统变量名 =

SET @@[SESSION | GLOBAL].系统变量名 =

1.3. System variable use cases

1) View all system variables

mysql> show variables;

2) View system variables whose names contain auto

On the left is the variable name and on the right is the variable value.

mysql> show variables like '%auto%';
+----------------------------------------------+-------+
| Variable_name                                | Value |
+----------------------------------------------+-------+
| auto_generate_certs                          | ON    |
| auto_increment_increment                     | 1     |
| auto_increment_offset                        | 1     |
| autocommit                                   | ON    |
| automatic_sp_privileges                      | ON    |
| caching_sha2_password_auto_generate_rsa_keys | ON    |
| innodb_autoextend_increment                  | 64    |
| innodb_autoinc_lock_mode                     | 2     |
| innodb_stats_auto_recalc                     | ON    |
| sha256_password_auto_generate_rsa_keys       | ON    |
| sql_auto_is_null                             | OFF   |
+----------------------------------------------+-------+
11 rows in set (0.00 sec)

3) View the variable value of the specified variable

mysql> select @@session.autocommit;
+----------------------+
| @@session.autocommit |
+----------------------+
|                    1 |
+----------------------+
1 row in set (0.00 sec)

4) Modify the value of the system variable

mysql> set @@session.autocommit = 0;
Query OK, 0 rows affected (0.02 sec)

或者

mysql> set session autocommit = 0;
Query OK, 0 rows affected (0.00 sec)

2. User-defined variables

In the MySQL database, it also supports user-defined environment variables according to their own needs. User-defined variables do not need to be declared in advance, and can be directly assigned and used. User-defined variables are only applied to the current session connection.

User-defined variables do not need to be declared or initialized. If the user-defined variable name is used directly without assignment, the variable value will be NULL.

2.1. Syntax format of user-defined variables

1) Define variables and assign values

Method 1, use SET to directly define variables and assign values.

SET @变量名1 = 变量值1
SET @变量名1 = 变量值1 , @变量名2 = 变量值2

SET @变量名1 := 变量值1 
SET @变量名1 := 变量值1 , @变量名2 := 变量值2 

建议使用:=的方式为变量赋值,因为在变量运算时=符号也是运算符号。

Method 2, since user-defined variables do not need to be declared, you can use SELECT to directly define variables and assign values.

SELECT @变量名1 := 变量值1 , @变量名2 := 变量值2 

Method 3, assigning the SQL query result to a variable.

SELECT 字段名 INTO @变量名 FROM 表名

将某个表的某个字段的结果赋值给变量。

2) Using variables

SELECT @变量名1

2.2. Cases of user-defined variables

1) Use set to define variables and assign values

1.一次性定义一个变量
mysql> set @name = 'jiangxl';
或者
mysql> set @username :=  'wang';

2.一次性定义多个变量
mysql> set @username = 'wang' , @userid = '1';
或者
mysql> set @age := '19' , @book := 'linux';

2) Use select to define variables and assign values

After using select to define the variable and assign the value, you can see the value of the variable.

1.一次性定义一个变量
mysql> select @myzhiwu := 'devops_linux';
+----------------------------+
| @myzhiwu := 'devops_linux' |
+----------------------------+
| devops_linux               |
+----------------------------+

2.一次性定义多个变量
mysql> select @myzhiwu := 'devops_linux' , @myage := '25';
+----------------------------+----------------+
| @myzhiwu := 'devops_linux' | @myage := '25' |
+----------------------------+----------------+
| devops_linux               | 25             |
+----------------------------+----------------+

3) Assign the total number of ryxxb to the variable

mysql> select count(*) into @mycount from ryxxb;

4) View the value of the variable

Query the value corresponding to each defined variable.

mysql> select @name,@username,@userid,@age,@book,@myzhiwu,@myage;
+---------+-----------+---------+------+-------+--------------+--------+
| @name   | @username | @userid | @age | @book | @myzhiwu     | @myage |
+---------+-----------+---------+------+-------+--------------+--------+
| jiangxl | wang      | 1       | 19   | linux | devops_linux | 25     |
+---------+-----------+---------+------+-------+--------------+--------+

3. Local variables that can be defined in stored procedures

Local variables are declared in stored procedures, and only apply to the scope of BEGIN...END of stored procedures. Local variables need to be declared with the DECLARE keyword. After the declaration is completed, assign values ​​​​by SET or SELECT. Local variables and user-defined variables The method of assignment is exactly the same.

3.1. Grammatical format of local variables

1) Declare variables

DECLARE 变量名 变量值的类型 [DEFAULT 默认值]

变量值的类型就是数据库字段的类型,例如:INT BIGINT CHAR VARCHAR DATE TIME等等。

2) Variable assignment

SET 变量名 = 变量值
SET 变量名 := 变量值
SELECT 字段名 INTO 变量名 FROM 表名;

3.2. Application cases of local variables

Create a stored procedure, define two local variables in it, use SET and SELECT to assign values, and finally check the contents of the variables.

1.创建视图
create procedure proc_2()
begin
	declare ryxxb_count int default 0;			#声明ryxxb表总数的变量,类型为int,默认值为0
	declare username varchar(10);				#声明username变量,类型为varchar
	set username := 'jiangxl';						#赋值username变量
	select count(*) into ryxxb_count from ryxxb;		#将ryxxb表的总数据量赋值为ryxxb_count变量
	select ryxxb_count,username;						#查询变量的值
end;

2.调用视图
mysql> call proc_2();
+-------------+----------+
| ryxxb_count | username |
+-------------+----------+
|          22 | jiangxl  |
+-------------+----------+
1 row in set (0.00 sec)

Guess you like

Origin blog.csdn.net/weixin_44953658/article/details/130552384