Home > Database > Mysql Tutorial > body text

How to define variables in mysql stored procedure

青灯夜游
Release: 2022-01-24 17:02:41
Original
7566 people have browsed it

In the mysql stored procedure, you can use the DECLARE keyword to define variables. The syntax is "DECLARE variable name [,...] type [DEFAULT default value]"; the parameter "type" is used to specify the variable Type, "DEFAULT" clause is used to set a default value for the variable.

How to define variables in mysql stored procedure

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

Variables are the most basic elements in expression statements and can be used to temporarily store data. Variables can be defined and used in both stored procedures and functions. Users can use the DECLARE keyword to define variables and assign values ​​to the variables after definition. The scope of these variables is in the BEGIN...END program section.

Define variables

You can use the DECLARE keyword to define variables in MySQL. The basic syntax is as follows:

DECLARE var_name[,...] type [DEFAULT value]
Copy after login

where :

  • DECLARE keyword is used to declare variables;

  • var_name parameters are The name of the variable. Multiple variables can be defined here at the same time;

  • type The parameter is used to specify the type of the variable;

  • DEFAULT value clause sets the default value of the variable to value. When the DEFAULT clause is not used, the default value is NULL.

Example

The variable my_sql is defined below, 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

[Related recommendations: mysql video tutorial]

The above is the detailed content of How to define variables in mysql stored procedure. 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!