Home >Database >Mysql Tutorial >How to define variables in mysql stored procedure

How to define variables in mysql stored procedure

青灯夜游
青灯夜游Original
2022-01-24 17:02:417662browse

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]

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;

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

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