
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can we handle a result set inside MySQL stored procedure?
We can use a cursor to handle a result set inside a stored procedure. Basically a cursor allows us to iterate a set of rows returned by a query and process each row accordingly.
To demonstrate the use of CURSOR within MySQL stored procedure, we are creating the following stored procedure which is based on the values, as shown below, of the table named ‘student_info’ −
mysql> Select * from student_info; +-----+---------+----------+------------+ | id | Name | Address | Subject | +-----+---------+----------+------------+ | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Jaipur | Literature | | 125 | Raman | Shimla | Computers | +-----+---------+----------+------------+ 3 rows in set (0.00 sec)
The following query will create a procedure named ‘list_address’ which returns the list of all addresses stored in the table −
mysql> Delimiter // mysql> CREATE PROCEDURE list_address (INOUT address_list varchar(255)) -> BEGIN -> DECLARE value_finished INTEGER DEFAULT 0; -> DECLARE value_address varchar(100) DEFAULT ""; -> DEClARE address_cursor CURSOR FOR -> SELECT address FROM student_info; -> DECLARE CONTINUE HANDLER -> FOR NOT FOUND SET value_finished = 1; -> OPEN address_cursor; -> get_address: LOOP -> FETCH address_cursor INTO value_address; -> IF value_finished = 1 THEN -> LEAVE get_address; -> END IF; -> SET address_list = CONCAT(value_address,";",address_list); -> END LOOP get_address; -> CLOSE address_cursor; -> END // Query OK, 0 rows affected (0.00 sec)
Now, we can see the result below when we invoke this procedure −
mysql> DELIMITER ; mysql> Set @address_list = ""; Query OK, 0 rows affected (0.00 sec) mysql> CALL list_address(@address_list); Query OK, 0 rows affected (0.00 sec) mysql> Select @address_list; +-------------------------+ | @address_list | +-------------------------+ | Shimla;Jaipur;Amritsar; | +-------------------------+ 1 row in set (0.00 sec)
- Related Articles
- How can we perform ROLLBACK transactions inside a MySQL stored procedure?
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- How can we perform START transactions inside MySQL stored procedure?
- How can we perform COMMIT transactions inside MySQL stored procedure?
- How can we alter a MySQL stored procedure?
- How can we drop a MySQL stored procedure?
- How can we invoke MySQL stored procedure?
- How can we write MySQL handler in a stored procedure?
- What is stored procedure and how can we create MySQL stored procedures?
- How can we create MySQL stored procedure to calculate the factorial?
- Set conditions in a MySQL stored procedure
- Implement Dynamic SQL query inside a MySQL stored procedure?
- How can we see the source code of a particular MySQL stored procedure?
- How can we use prepared statements in a stored procedure?
- How can we handle NULL values stored in a MySQL table by using PHP script?

Advertisements