To use stored procedures in a database, you must first understand how to use variables and custom functions in the database. The brute force introduction begins.
The difference between system variables and user-defined variables is that user-defined variables use a
@when viewed
, while system variables use Two@
system variables
Introduction: Users cannot define the system Variables, but can be modified and used
Usage: Use select to get the value of the variable, but because select will query all strings as fields of a table by default, if it is a variable You need to use the @@
symbol to access
View all system built-in variables command:
show variables;
View a certain System variable command:select @@variable name;
, such asselect @@version
Modify variable (local modification) command:set variable name = value
, such asset autocommit = 3;
Custom variable
Custom variable syntax :
set @name = value;
, such as`set @name = 'saboran';
View the custom variable value:select @name;
Variable scope
Externally defined variables are called global variables. Global variables are the same as global variables in js and can be used in Used inside the function;
Local variables: declare variable data type
Local variables cannot be accessed outside the function.
Syntax
create function 函数名(参数列表) returns 数据类型 begin // 函数体 // 返回值 end
Give me an example
delimiter $$ create function avg(first int) returns int begin declare value ; set value = first; return value; end $$
To use stored procedures in the database, you must first understand how to use variables and custom functions in the database. The brute force introduction begins.
The difference between system variables and user-defined variables is that user-defined variables use a
@when viewed
, while system variables use Two@
system variables
Introduction: Users cannot define the system Variables, but can be modified and used
Usage: Use select to get the value of the variable, but because select will query all strings as fields of a table by default, if it is a variable You need to use the @@
symbol to access
View all system built-in variables command:
show variables;
View a certain System variable command:select @@variable name;
, such asselect @@version
Modify variable (local modification) command:set variable name = value
, such asset autocommit = 3;
Custom variable
Custom variable syntax :
set @name = value;
, such as`set @name = 'saboran';
View the custom variable value:select @name;
Variable scope
Externally defined variables are called global variables. Global variables are the same as global variables in js and can be used in Used inside the function;
Local variables: declare variable data type
Local variables cannot be accessed outside the function.
Syntax
create function 函数名(参数列表) returns 数据类型 begin // 函数体 // 返回值 end
Give me an example
delimiter $$ create function avg(first int) returns int begin declare value ; set value = first; return value; end $$
The above is the detailed content of Quick Start with MySQL Stored Procedures. For more information, please follow other related articles on the PHP Chinese website!