I. Definition of Variables
MySQL uses declare to define a local variable. The scope of use of this variable can only be in begin... used in the end block, the variable must be defined at the beginning of the composite statement, and can declare multiple variables at the same time before other statements. If necessary, you can use default to assign the default value.
The syntax for defining a variable is as follows:
Declare var_name [,...] type [default value] to view a variable definition instance
Declare last date; 2. MySQL Stored Procedure variable assignment
You can directly assign values to variables or assign values to queries. You can use set to directly assign values. It can be a constant or expression.
CopyCode The Code is as follows: Set var_name = [, var_name expr]... the method for assigning values to the last variable above is as follows:
Set last = date_sub (current_date (), interval 1 month); The following describes how to assign values to the variables through the query. The results returned by the query must be one row. The specific operations are as follows:
Select Col into var_name [,...] table_expr we will assign values to v_pay through the query.
Create Function get _ cost (p_custid int, p_eff datetime)
Return decimal (5, 2)
Deterministic
Reads SQL data
Begin
Declare v_pay decimail (5, 2 );
Select ifnull (sum (pay. amount), 0) into vpay from payment where pay. payd <= p_eff and pay. custid = PID
Reutrn v_rent + v_over-v_pay;
End $
Now, this simple tutorial on variable definition assignment in the stored procedure is here. Next we will talk about the definition and processing of conditions in the myql stored procedure.
The following is a supplement from other netizens:
You can use a variable in the MySQL stored procedure to save the values in the processing procedure.
Use the declare statement to define variables. The syntax format is as follows:
Declare var_name [,...] type [default value]
Var_name is the variable name and type is any data type supported by MySQL. The optional [default value] specifies the default value for the variable. Multiple variables of the same type can be defined at a time. Variable names are separated by commas.
Pay attention to the following points when defining and using variables:
◆ The declare statement must be used in degin... The end statement block must appear in the degin... The beginning of the end statement block, that is, before other statements.
◆ The scope of the variables defined by declare is limited to the degin... Other degin within the end block and nested in the block... End Block.
◆ Variable names in stored procedures are case-insensitive.
The Defined variables are assigned values using the set statement. The syntax format is as follows:
Set var_name = expr [, var_name = expr]...
Here, var_name is the variable name, expr is the expression of the value or return value, which can be any expression supported by MySQL. You can assign values to multiple variables at a time. Multiple "variable name = value" pairs are separated by commas.
For example:Copy codeThe Code is as follows: Begin
Declare No varchar (20 );
Declare title varchar (30 );
Set NO = '000000', Title = 'variables and assignments defined in Stored Procedures ';
End
Tip: All keywords in the stored procedure are case-insensitive. For example, begin can be written.