Cursors in SQL
Cursors in SQL
A Cursor is a temporary memory that is allocated by the database server at the time of performing
the Data Manipulation Language operations on a table, such as INSERT, UPDATE and DELETE etc. It is
used to retrieve and manipulate data stored in the SQL table.
In MySQL, you cannot declare a cursor directly outside of a stored procedure or function. Cursors are
generally declared within stored procedures, functions, or blocks of SQL code in MySQL database.
Using cursors, we can perform multiple operations on each row of a result set, with or without
returning the original data.
Properties of Cursors
READ ONLY − We cannot update or modify any records in the table using the MySQL cursors.
We can just fetch and process data from a table.
Non-Scrollable − We can retrieve records from a table in a single direction, i.e. from the first
record or the last. We cannot move backward or jump to a specific position within the result
set.
Asensitive Cursor − An asensitive cursor operates directly on the actual data in the database,
it does not create a copy of the data. If any change is made to the data by other connections,
it can affect the data that the cursor is working with.
In addition to the Asensitive cursor there is another type known as Insensitive Cursor. An insensitive
cursor uses a temporary copy of the data. Therefore, these cursors are insensitive (not affected) to
the changes that are made in the table.
There are four steps to manage these cursors. Following diagram illustrates the lifecycle of an SQL
cursor −
Now, let us discuss the phases of life cycle of the cursor one-by-one.
In MySQL we can declare a cursor using the DECLARE statement and associate it with a SELECT
statement to retrieve records from a database table.
However, this SELECT statement associated with a cursor does not use the INTO clause, as it's
purpose is to fetch and process rows rather than assigning values to variables.
Syntax
After declaring a cursor in MySQL, the next step is to open the cursor using the OPEN statement. It
initializes the result-set, allowing us to fetch and process rows from the associated SELECT statement
in the cursor.
Syntax
OPEN cursor_name;
Then, we can use the FETCH statement to retrieve the current row pointed by the cursor, and with
each FETCH, the cursor moves to the next row in the result set. This allows us to process each row
one by one.
Syntax
Once all the rows are fetched, we must close the cursor to release the memory associated with it.
We can do this using the CLOSE statement.
Syntax
CLOSE cursor_name;
Example
In this example, let us see how to manage a cursor in a stored procedure.
Assume we have created a table with the name CUSTOMERS using the CREATE TABLE statement as
follows −
);
Now, let us insert some records into the CUSTOMERS table using the INSERT statement as follows −
Now, we will create a backup table named 'CUSTOMERS_BACKUP' to store customer data −
);
Here, we are creating a stored procedure named FetchCustomers to fetch customer names from
the CUSTOMERS table and inserting them one by one into the BACKUP table. We are using a cursor
to iterate through the rows and a handler to detect the end of the result-set, ensuring all names are
processed −
DELIMITER //
BEGIN
-- Declare cursor
-- Open cursor
OPEN MY_CURSOR;
read_loop: LOOP
IF done = 1 THEN
LEAVE read_loop;
END IF;
END LOOP;
-- Close cursor
CLOSE MY_CURSOR;
END //
DELIMITER ;
Once we create the procedure successfully, we can execute it using the CALL statement as shown
below −
CALL FetchCustomers();
Verification
You can verify the contents of the CUSTOMERS_BACKUP table using the SELECT statement as shown
below −
ID NAME
1 Ramesh
2 Khilan
3 Kaushik
4 Chaitali