Home  >  Article  >  Database  >  How to assign values ​​to variables in mysql stored procedures

How to assign values ​​to variables in mysql stored procedures

青灯夜游
青灯夜游Original
2022-01-24 17:14:3711432browse

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;".

How to assign values ​​to variables in mysql stored procedures

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn