MySQL variable definition and assignment are common problems. The following describes how to define and assign values to MySQL variables during storage, I hope you can learn more about MySQL variable definition.
I. MySQL variable definition
Mysql variable definition uses declare to define a local variable. The definition and value assignment operations of variables in mysql stored procedures can only be performed in begin... used in the end block, the variable must be defined at the beginning of the composite statement, and prior to other statements, expr is the value or return value expression, so that any MySQL-supported Return Value expression can be used. You can assign values to multiple variables at a time, or declare multiple variables at a time. If you want to assign a default value, you can use default.
The syntax for defining a MySQL variable is as follows:
Declare var_name [,...] type [default value] to 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 directly assign values to set variables. constants or expressions are supported as follows:
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 operations are as follows:
Select col into var_name [,...] table_expr.
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 complete.
Detailed description of MySQL injection using variables
Usage of MySQL user Variables
Difference between NULL and MySQL NULL strings
In-depth study of MySQL result strings
Top 10 most noteworthy MySQL Variables