MySQL Cursor, mysqlcursor
MySQL Cursor
Summary: in this tutorial, you will learn how to use MySQL cursor in stored procedures to iterate through a result set returned by a SELECT statement.
Introduction to MySQL cursor
To handle a result set inside a stored procedure, you use a cursor. A cursor allows you to iterate a set of rows returned by a query and process each row accordingly.
MySQL cursor is read-only, non-scrollable and asensitive.
- Read only: you cannot update data in the underlying table through the cursor.
- Non-scrollable: you can only fetch rows in the order determined by the SELECT statement. you cannot fetch rows in the reversed order. in addition, you cannot skip rows or jump to a specific row in the result set.
- Asensitive: there are two kinds of cursors: asensitive cursor and insensitive cursor. an asensitive cursor points to the actual data, whereas an insensitive cursor uses a temporary copy of the data. an asensitive cursor performs faster than insensitive cursor because it does not have to make a temporary copy of data. however, any change that made to the data from other connections will affect the data that is being used by an asensitive cursor, therefore, it is safer if you don't update the data that is being used by an asensitive cursor. mySQL cursor is asensitive.
You can use MySQL cursors in stored procedures, stored functions, and triggers.
Working with MySQL cursor
First, you have to declare a cursor by usingDECLAREStatement:
| 1 |
DECLARE cursor_name cursor for SELECT_statement; |
The cursor declaration must be after any variable declaration. If you declare a cursor before variables declaration, MySQL will issue an error. A cursor must always be associated withSELECTStatement.
Next, you open the cursor by usingOPENStatement.OPENStatement initializes the result set for the cursor, therefore, you must callOPENStatement before fetching rows from the result set.
Then, you useFETCHStatement to retrieve the next row pointed by the cursor and move the cursor to the next row in the result set.
| 1 |
FETCH cursor_name INTO variables list; |
After that, you can check to see if there is any row available before fetching it.
Finally, you callCLOSEStatement to deactivate the cursor and release the memory associated with it as follows:
When the cursor is no longer used, you should close it.
When working with MySQL cursor, you must also declareNOT FOUNDHandler to handle the situation when the cursor cocould not find any row. Because each time you callFETCHStatement, the cursor attempts to read the next row in the result set. when the cursor reaches the end of the result set, it will not be able to get the data, and a condition is raised. the handler is used to handle this condition.
To declareNOT FOUNDHandler, you use the following syntax:
| 1 |
Declare continue handler for not found set finished = 1; |
WherefinishedIs a variable to indicate that the cursor has reached the end of the result set. Notice that the handler declaration must appear after variable and cursor declaration inside the stored procedures.
The following dimo-strates how MySQL cursor works.
MySQL Cursor Example
We are going to develop a stored procedure that builds an email list of all employees inemployeesTable in the MySQL sample database.
First, we declare some variables, a cursor for looping over the emails of employees, andNOT FOUNDHandler:
| 12345678910 |
DECLARE finished integer default 0; DECLARE email varchar (255) DEFAULT ""; -- declare cursor for employee emailDEClARE email_cursor cursor for select email FROM employees; -- declare not found handlerDECLARE continue handlerfor not found set finished = 1; |
Next, we openemail_cursorBy usingOPENStatement:
Then, we iterate the email list, and concatenate all emails where each email is separated by a semicolon (;):
| 12345678 |
Get_email: loop fetch email_cursor INTO v_email; IF v_finished = 1 then leave get_email; end if; -- build email list SET email_list = CONCAT (v_email, ";", email_list ); end loop get_email; |
After that, inside the loop we usedv_finishedVariable to check if there is any email in the list to terminate the loop.
Finally, we close the cursor usingCLOSEStatement:
Thebuild_email_listStored procedure is as follows:
| 123456789101112131415161718192021222324252627282930313233343536 |
DELIMITER $ create procedure build_email_list (INOUT email_list varchar (4000) begin declare v_finished integer default 0; DECLARE v_email varchar (100) DEFAULT ""; -- declare cursor for employee email DEClARE email_cursor for select email FROM employees; -- declare not found handler declare continue handler for not found set v_finished = 1; OPEN email_cursor; get_email: loop fetch email_cursor INTO v_email; IF v_finished = 1 then leave get_email; end if; -- build email list SET email_list = CONCAT (v_email, ";", email_list); end loop get_email; CLOSE email_cursor; END $ DELIMITER; |
You can test build_email_listStored procedure using the following script:
| 123 |
SET @ email_list = ""; CALL build_email_list (@ email_list); SELECT @ email_list; |
In this tutorial, we have shown you how to use MySQL cursor to iterate a result set and process each row accordingly.