Home / MySQL DBMS / Pointer in Procedure

Cursor in SPocedure

In this lesson, we will learn how to use database cursors to iterate over a query result set.

MySQL supports cursors for resident procedures, functions, and triggers. The cursor is used to iterate over a set of rows belonging to the query result. Use cursors to process calculations on each line. For version 5.x and later, cursors in MySQL have the following properties:

  • Read only: means we cannot change the value.
  • Non-scrollable (cannot go back): the cursor only goes in one direction, cannot skip or return to previously traversed lines in the result set.
  • Asensitive: avoid updating a data table while the cursor is open on the data table itself. If not, unexpected consequences may occur.

Process of declaring and using pointers

Step 1: Declare the pointer with the following syntax:

DECLARE cursor_name CURSOR FOR SELECT_statement;

Step 2: open the cursor for use

OPEN cursor_name;

Step 3: Take out a line to process and move the cursor to the next line

FETCH cursor_name INTO variable list;

Step 4: close the cursor, freeing the memory occupied by the cursor.

CLOSE cursor_name;

Note: The important point to note when manipulating cursors is to use NOT FOUND error checking to avoid cases where there are no more data lines to process.

Here is an example of using cursors in a resident procedure

For example:

DELIMITER $$

 DROP PROCEDURE IF EXISTS CursorProc$$

 CREATE PROCEDURE CursorProc()

 BEGIN

        DECLARE  no_more_products, quantity_in_stock INT DEFAULT 0;

        DECLARE  prd_code VARCHAR(255);

        DECLARE  cur_product CURSOR FOR

               SELECT  productCode FROM products;

        DECLARE  CONTINUE HANDLER FOR NOT FOUND

        SET  no_more_products = 1;



        /* for  logging information */

         CREATE  TABLE infologs (

                Id int(11) NOT NULL AUTO_INCREMENT,

               Msg varchar(255) NOT NULL,

               PRIMARY KEY (Id)

        );

        OPEN  cur_product;



        FETCH  cur_product INTO prd_code;

        REPEAT

               SELECT  quantityInStock INTO quantity_in_stock

               FROM  products

               WHERE  productCode = prd_code;



               IF  quantity_in_stock < 100 THEN

                       INSERT  INTO infologs(msg)

                       VALUES  (prd_code);

               END  IF;

               FETCH  cur_product INTO prd_code;

        UNTIL  no_more_products = 1

        END REPEAT;

        CLOSE  cur_product;

        SELECT *  FROM infologs;

        DROP TABLE  infologs;

 END$$

 DELIMITER;

Explanation: above is a simple example and can be done by building SQL queries. However, it is an easy-to-understand example for us to learn how pointers work.

We use a cursor on the products table and iterate over the query result set on this table. If the product's total inventory < 100 then we put it in the clipboard. At the end of the iteration process, we will have a list of products with inventory less than 100.

Note: must declare the pointer before the NOT FOUND error checking declaration.

Did you find this article helpful?

Support the author so they can write more quality articles!

Tư vấn lộ trình CNTT 🤖