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.
Yesterday we talked about how to create, modify, and delete the stored procedure in the mysql tutorial. The following is a tutorial aboutDefinition assignment of variables in mysql stored proceduresOh.
I,Variable definition
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]
View a variable definition instance
Declare last date;
II,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.
Set var_name = [, var_name expr]...
The method for assigning values to the above last variable is as follows:
Set last = date_sub (current_date (), interval 1 month );
The following describes how to assign a value to a variable through a query. The result returned by the query must be a row. The specific operation is as follows:
Select col into var_name [,...] table_expr
Query to assign values to v_pay.
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 aboutMyql Stored Procedure.
Indicate, this site Original article reprinted indicate the source http://www.bKjia. c0m/database/database.html