MySQL - Cursor CLOSE Statement



A cursor in database cursor is a construct which allows you to iterate/traversal the records of a table. In MySQL you can use cursors with in a stored program such as procedures, functions etc.

In other words, you can iterate though the records of a table from a MySQL stored program using the cursors. The cursors provided by MySQL are embedded cursors. They are −

  • READ ONLY − Using these cursors you cannot update any table.

  • Non-Scrollable − Using these cursors you can retrieve records from a table in one direction i.e., from top to bottom.

  • Asensitive − These cursors are insensitive to the changes that are made in the table i.e. the modifications done in the table are not reflected in the cursor.

    Which means if we have created a cursor holding all the records in a table and, meanwhile if we add some more records to the table, these recent changes will not be reflected in the cursor we previously obtained.

While Declaring cursors in a stored program you need to make sure these (cursor declarations) always follow the variable and condition declarations.

To use a cursor, you need to follow the steps given below (in the same order)

  • Declare the cursor using the DECLARE Statement.
  • Declare variables and conditions.
  • Open the declared cursor using the OPEN Statement.
  • Retrieve the desired records from a table using the FETCH Statement.
  • Finally close the cursor using the CLOSEstatement.
CLOSE

The CLOSE statement

This statement is used close the cursor opened earlier. If you try to close a cursor that is not yet opened an error is generated. If you haven’t closed the cursor explicitly it will be closed at the end of the BEGIN ... END block. The CLOSE statement to deactivates the cursor and releases the memory associated with it.

Syntax

Following is the syntax of the MySQL Cursor CLOSE Statement −

CLOSE cursor_name;

Example

Assume we have created a table with name mobile_sales in MySQL database using CREATE statement as shown below −

CREATE TABLE mobile_sales (
   SNO INT,
   mobile_brand VARCHAR(255),
   unit_sale INT
);

Now, we will insert 11 records in mobile_sales table using INSERT statements −

Insert into mobile_sales values
(1, 'Iphone', 3000),
(2, 'Samsung', 4000),
(3, 'Nokia', 5000),
(4, 'Vivo', 1500),
(5, 'Oppo', 900),
(6, 'MI', 6400),
(7, 'MotoG', 4360),
(8, 'Lenovo', 4100),
(9, 'RedMI', 4000),
(10, 'MotoG', 4360);

Following procedure retrieves the sales of the mobile model with serial number −

DELIMITER //
CREATE PROCEDURE cursorExample(num INT)
   BEGIN
      DECLARE number_of_units varchar(255);
      DECLARE cur1 CURSOR for SELECT unit_sale FROM 
	  mobile_sales WHERE SNO=num;
      OPEN cur1;
         FETCH cur1 into number_of_units;
         SELECT number_of_units;
      CLOSE cur1;
   END //
DELIMITER ;

You can call the above procedure as shown below −

CALL cursorExample(5);

Output

Following is the output of the above query −

number_of_units
900
Advertisements