MySQL Cursors
-
Upload
reggie-niccolo-santos -
Category
Technology
-
view
535 -
download
19
description
Transcript of MySQL Cursors
![Page 1: MySQL Cursors](https://reader033.fdocuments.us/reader033/viewer/2022061505/55497841b4c905d8558b5a76/html5/thumbnails/1.jpg)
MySQL CursorsReggie Niccolo Santos
UP ITDC
![Page 2: MySQL Cursors](https://reader033.fdocuments.us/reader033/viewer/2022061505/55497841b4c905d8558b5a76/html5/thumbnails/2.jpg)
Outline
What is a cursor? Additional characteristics Syntax Example
![Page 3: MySQL Cursors](https://reader033.fdocuments.us/reader033/viewer/2022061505/55497841b4c905d8558b5a76/html5/thumbnails/3.jpg)
What is a cursor?
Allows you to iterate through a result set returned by a SELECT statement
Allows you to iterate over a set of rows returned by a query and process each row accordingly
![Page 4: MySQL Cursors](https://reader033.fdocuments.us/reader033/viewer/2022061505/55497841b4c905d8558b5a76/html5/thumbnails/4.jpg)
MySQL Cursor Characteristics
Read-only You cannot update data in the
underlying table through the cursor
![Page 5: MySQL Cursors](https://reader033.fdocuments.us/reader033/viewer/2022061505/55497841b4c905d8558b5a76/html5/thumbnails/5.jpg)
MySQL Cursor Characteristics
Non-scrollable You can only fetch rows in the
order determined by the SELECT statement
You cannot fetch rows in the reversed order
You cannot skip rows or jump to a specific row in the result set
![Page 6: MySQL Cursors](https://reader033.fdocuments.us/reader033/viewer/2022061505/55497841b4c905d8558b5a76/html5/thumbnails/6.jpg)
MySQL Cursor Characteristics
Asensitive Points to the actual data,
whereas an insensitive cursor uses a temporary copy of the data
Faster than an insensitive cursor
![Page 7: MySQL Cursors](https://reader033.fdocuments.us/reader033/viewer/2022061505/55497841b4c905d8558b5a76/html5/thumbnails/7.jpg)
Syntax
DECLARE cursor_name CURSOR FOR select_statement
OPEN cursor_name
FETCH [[NEXT] FROM] cursor_name INTO var_name [, var_name] ...
CLOSE cursor_name
![Page 8: MySQL Cursors](https://reader033.fdocuments.us/reader033/viewer/2022061505/55497841b4c905d8558b5a76/html5/thumbnails/8.jpg)
Things to Note
The SELECT statement cannot have an INTO clause
Cursor declarations must appear before handler declarations and after variable and condition declarations
![Page 9: MySQL Cursors](https://reader033.fdocuments.us/reader033/viewer/2022061505/55497841b4c905d8558b5a76/html5/thumbnails/9.jpg)
Things to Note
If no more rows are available, a No Data condition occurs with a SQLSTATE value '02000'. To detect this condition, you can setup a handler for it (or for a NOT FOUND condition)
![Page 10: MySQL Cursors](https://reader033.fdocuments.us/reader033/viewer/2022061505/55497841b4c905d8558b5a76/html5/thumbnails/10.jpg)
Example
DECLARE v_finished INTEGER DEFAULT 0;DECLARE v_email VARCHAR(255) DEFAULT "";DECLARE email_list VARCHAR(1000) DEFAULT ""; # declare cursor for student emailDECLARE email_cursor CURSOR FOR SELECT email FROM student_record.students;
# declare NOT FOUND handlerDECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1;
![Page 11: MySQL Cursors](https://reader033.fdocuments.us/reader033/viewer/2022061505/55497841b4c905d8558b5a76/html5/thumbnails/11.jpg)
Example
OPEN email_cursor;
get_email: LOOP FETCH email_cursor INTO v_email; IF v_finished = 1 THEN LEAVE get_email; END IF; -- build email list SET email_list = CONCAT(v_email, ";", email_list);
END LOOP get_email;
CLOSE email_cursor;
![Page 12: MySQL Cursors](https://reader033.fdocuments.us/reader033/viewer/2022061505/55497841b4c905d8558b5a76/html5/thumbnails/12.jpg)
References
Http://www.mysqltutorial.org/mysql-cursor/ Http://www.brainbell.com/tutorials/MySQL/Working_With_Cursors.htm