Академический Документы
Профессиональный Документы
Культура Документы
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.
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 an 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.
A database cursor is a control structure that enables traversal over the records in a database. Cursors are used by
database programmers to process individual rows returned by database system queries. Cursors enable manipulation of
whole result sets at once. In this scenario, a cursor enables the rows in a result set to be processed sequentially. In SQL
procedures, a cursor makes it possible to define a result set (a set of data rows) and perform complex logic on a row by
row basis. By using the same mechanics, an SQL procedure can also define a result set and return it directly to the caller
of the SQL procedure or to a client application.
The procedure starts with three variable declarations. Incidentally, the order is important. First, declare variables. Then
declare conditions. Then declare cursors. Then, declare handlers. If you put them in the wrong order, you will get an
error message.
DELIMITER $$
CREATE PROCEDURE my_procedure_cursors(INOUT return_val INT)
BEGIN
DECLARE a,b INT;
DECLARE cur_1 CURSOR FOR
SELECT max_salary FROM jobs;
DECLARE CONTINUE HANDLER FOR NOT FOUNDSET b = 1;
OPEN cur_1;
REPEAT FETCH cur_1 INTO a;
UNTIL b = 1
END
REPEAT;
CLOSE cur_1;
SET return_val = a;
END;
$$
mysql>
CALL my_procedure_cursors(@R);
Query OK, 0 rows affected (0.00 sec)
This MySQL tutorial explains how to declare a cursor in MySQL with syntax and examples.
Description
A cursor is a SELECT statement that is defined within the declaration section of your stored program in MySQL.
Syntax
cursor_name
The name to assign to the cursor.
select_statement
The SELECT statement associated with the cursor.
Example
For example:
DELIMITER //
BEGIN
OPEN c1;
FETCH c1 INTO siteID;
CLOSE c1;
RETURN siteID;
END; //
DELIMITER ;
You could then call your new function (that contains the cursor) as follows:
Description
Once you've declared your cursor in MySQL, the next step is to use the OPEN statement to open the cursor.
Syntax
The syntax to open a cursor using the OPEN statement in MySQL is:
OPEN cursor_name;
Description
The purpose of using a cursor, in most cases, is to retrieve the rows from your cursor so that some type of operation can
be performed on the data. After declaring and opening your cursor, the next step is to use the FETCH statement to fetch
rows from your cursor.
Syntax
cursor_name
The name of the cursor that you wish to fetch rows.
variable_list
The list of variables, comma separated, that you wish to store the cursor result set in.
Description
If you try to fetch data from a cursor and there are no rows, MySQL will raise a NO DATA error. You can set up a
handler for the NOT FOUND condition so that your stored program does not terminate with an error.
Syntax
The syntax to set up a handler for the NOT FOUND condition for a cursor in MySQL is:
set_condition
The condition to set when the NOT FOUND condition is encountered by the cursor.
Example
Let's look at how to set up a handler for the NOT FOUND condition for a cursor in MySQL.
First, we need to declare a variable that will be set when the NO DATA error occurs.
Description
Asensitive cursors
Insensitive cursors.
Asensitive cursor focus on genuine data.
Asensitive cursor work faster than the insensitive cursor.
Asensitive cursor doesn’t need any duplicate copy of data.
Any alteration done in the data through dead end should effect the data contain in asensitive cursors.
An insensitive cursor uses duplicate copy data.
Insensitive cursors work very slower.
Insensitive cursors contain duplicate data.
Description
In read only data, there is only permission to read the data. And it does not contain any permissions to update the data or
alter the data.
Description
MySQL cursor moves only in one direction. Data fetching can be done in only one direction, and cannot fetch data in
opposite direction.
Description
Creating MySQL Cursors is done by creating cursor with all the specifications.
Syntax
By viewing the below example, the concept of create cursors can be easily understood.
mysql> delimiter $$
mysql> create procedure curdemo(id int)
-> begin
-> declare name varchar(255);
-> declare cur1 cursor for select stu_name from student where stu_id=id;
-> open cur1;
-> fetch cur1 into name;
-> select name;
-> close cur1;
-> end $$
Query OK, 0 rows affected (0.13 sec)
mysql> delimiter ;
mysql> call curdemo(2);
+------+
| name |
+------+
| shah |
+------+
1 row in set (0.39 sec)
In the above example, curdemo is a input parameter which accept input values. cur1 is the attribute used in the cursor.
open() is used to open the cursor, fetch is used to fetch the row values into names and close cursor will remove the open
cursor for same minutes. Then the call cursor will execute the curdemo and return the records(i.e, call cursor will call
the stu_id(2) and it will return the name(shah)).
Description
Close Cursor is used to close the Cursor upto some extent, when the open cursor is not required. The keyword close in
the cursor will discharge the present records to close and then open the open cursor.
Conceptual
figure
By viewing the below example, the concept of close cursors can be easily understood.
mysql> delimiter $$
mysql> create procedure close_cursor(id int)
-> begin
-> declare name varchar(255);
-> declare cur1 cursor for select stu_name from student where stu_id=id;
open cur1;
-> fetch cur1 into name;
-> select name;
-> close cur1;
-> end $$
Query OK, 0 rows affected (0.02 sec)
mysql> delimiter ;
mysql> call close_cursor(4);
+------+
| name |
+------+
| maze |
+------+
1 row in set (0.39 sec)
In the above example, close_cursor is a input parameter which accept input values. cur1 is the attribute used in the
cursor. open() is used to open the cursor, fetch is used to fetch the record values from cursor, select name will return the
name of the record and close cursor is used to close the cur1. Then the call cursor will execute the cur1 and return the
records(i.e, call cursor will call the stu_id(4) and it will return the name(maze)).
Key Points
MySQL Cursors are database objects used in the database application used to manipulate the data.
Read only will only read the data.
Asensitive cursor focuses to genuine information.
Insensitive cursor utilizes duplicate information.