The following article mainly introduces the usage notes of MySQL cursors, which can be used in SQL statements of stored procedures. The main types are as follows. The following is a detailed introduction to them. I believe that if you master this technology, you will be It will be of great help in future study or work.
1. Statements that do not return results, such as: INSERT, UPDATE, DROP, DELETE, etc.
2. The select statement returns a single-line variable and can be passed to a local variable (select ..into)
3. A select statement that returns a multi-row result set and can be processed using a MySQL cursor loop
Note that the multi-row result set returned by the stored procedure can be received by the client program (such as php), but it is impossible to receive the result set of another stored procedure in one stored procedure. The general solution is to store a temporary Tables are shared by other processes
4. prepare statement
The following mainly describes the cursor and prepare part
cursor definition
DECLARE cursor_name CURSOR FOR SELECT_statement;
Cursor operation OPEN open the cursor
OPEN cursor_name;
FETCH Gets the record of the cursor's current pointer and passes it to the specified variable list. Note that the number of variables must be the same as the number of fields returned by the MySQL cursor. To obtain multiple rows of data, use a loop statement to execute FETCH
FETCH cursor_name INTO variable list;
CLOSE closes the cursor
CLOSE cursor_name ;
Note: MySQL's cursor is read-only forward, that is, you can only read the result set sequentially from the beginning to the back, not from the back to the front, and you cannot directly jump to the middle record.
A complete example:
define local variables
DECLARE o varchar(128);
define cursor
DECLARE ordernumbers CURSOR FOR SELECT callee_name FROM account_tbl where acct_timeduration=10800; DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_departments=1; SET no_more_departments=0;
open cursor
OPEN order numbers;
loop over all rows
REPEAT -- Get order number FETCH ordernumbers INTO o; update account set allMoneyallMoney=allMoney+72,lastMonthConsumelastMonthConsume=lastMonthConsume-72 where NumTg=@o;
end of loop
UNTIL no_more_departments END REPEAT;
close cursor
CLOSE ordernumbers;
Variable definitions:
declare variable_name [,variable_name...] datatype [default value];
Among them, datatype is the data type of mysql, such as: int, float, date, varchar(length)
example:
declare l_int int unsigned default 4000000; declare l_numeric numeric(8,2) default 9.95; declare l_date date default '1999-12-31'; declare l_datetime datetime default '1999-12-31 23:59:59'; declare l_varchar varchar(255) default 'this will not be padded';
variable assignment
set variable_name = expression_value[,variable_name = expression ...]
parameter
The parameters of the mysql stored procedure are used in the definition of the stored procedure, there are three types of parameters, in, out, inout
create procedure|function([[in |out |inout ] parameter name data type...])
in input parameter
Indicates that the value of the parameter must be specified when calling the stored procedure, and the value of the parameter modified in the stored procedure cannot be returned, which is the default value
out output parameter
The value can be changed inside the stored procedure and can be returned
inout input and output parameters
specified at call time and can be changed and returned
Create a stored procedure:
grammar:
create procedure p()
begin
end
create procedure productpricing()
begin
select avg(pro_price) as priceaverage
from products;
end;
#Begin...end is the body definition of the stored procedure
# The delimiter of mysql is a semicolon (;)
The way to call the stored procedure is:
# call plus the procedure name and a parenthesis
# For example, calling the stored procedure defined above
call productpricing();
/*初始化*/ drop procedure if exists useCursor // /*Create a stored procedure create */ CREATE PROCEDURE useCursor() BEGIN /* Definition of local variables declare*/ declare tempName varchar(50) DEFAULT ''; /*The definition life length of the local variable sets the default value declare*/ declare allName VARCHAR(4000) DEFAULT ''; /*Create cursor*/ declare cur1 CURSOR for select username from trade_01.f_user; /* Set cursor exception catch catch exception stop cursor * and set the loop using the variable tmpname to null to break out of the loop. */ DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET tempName = NULL; /* start cursor */ OPEN cur1; /* Cursor goes one step down */ FETCH cur1 INTO tempName; /*loop cursor*/ WHILE (tempName is not null) DO set tempName=CONCAT(tempName,";"); /* Splice name */ set allName= CONCAT(allName,tempName); FETCH cur1 INTO tempName; END WHILE; /* close the cursor */ CLOSE cur1; /*output splice name*/ select allName; END /* call stored procedure */ call useCursor()//