Home > Database > Mysql Tutorial > body text

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

青灯夜游
Release: 2022-01-24 18:07:13
Original
11425 people have browsed it

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 默认值;
Copy after login

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;
Copy after login

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 变量名 = 赋值表达式;
Copy after login

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;
Copy after login

[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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!