How to assign values to variables in mysql stored procedures: 1. Use the DECLARE keyword, the syntax "DECLARE variable name type DEFAULT default value;"; 2. Use the SET keyword, the syntax "SET variable name = assignment expression Mode;".
The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.
Variables can be defined and used in mysql stored procedures and functions.
Users can use the DECLARE keyword to define variables, and they can be assigned values after definition. The scope of these variables is in the BEGIN...END program section.
1. Assign a value when defining a variable
You can use the DECLARE keyword in MySQL to define variables and set default values, syntax:
DECLARE 变量名 类型 DEFAULT 默认值;
Note: The DEFAULT clause is used to set the default value of the variable. If the DEFAULT clause is omitted, the default value is NULL.
Example:
Define the variable my_sql, the data type is INT type, and the default value is 10. The SQL statement is as follows:
DECLARE my_sql INT DEFAULT 10;
2. Assign a value after defining the variable
You can use the SET keyword in MySQL to assign a value to the variable
Grammar:
SET 变量名 = 赋值表达式;
Note: A SET statement can assign values to multiple variables at the same time, and the assignment statements for each variable are separated by commas.
Example: Assign a value of 30 to the variable my_sql. The SQL statement is as follows:
SET my_sql=30;
[Related recommendations: mysql video tutorial]
The above is the detailed content of How to assign values to variables in mysql stored procedures. For more information, please follow other related articles on the PHP Chinese website!