mysql cursor with example

Upload: nizar-achmad

Post on 02-Jun-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 MySQL Cursor With Example

    1/7

    Search this website

    MySQL Cursor

    Summary: in this tutorial, you will learn how to useMySQL

    cursorin stored procedures to iterate througha result set

    returned by a SELECT statement.

    Introduction to MySQL cursor

    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.

    MySQL cursor is read only, non-scrollable and asensitive.

    Read only: you cannot update data in the underlying

    table through the cursor.

    MySQL Tutorial

    Newsletter

    Sign up to receive updates

    about MySQL Tutorials and

    find out what's new in

    MySQL.

    Enter your email address...

    Home Basic MySQL MySQL Stored Procedures MySQL Triggers MySQL Views

    MySQL Functions MySQL Administration MySQL Tips

    http://www.mysqltutorial.org/mysql-functions.aspxhttp://www.mysqltutorial.org/mysql-functions.aspxhttp://www.mysqltutorial.org/mysql-administration.aspxhttp://www.mysqltutorial.org/mysqltips.aspxhttp://www.mysqltutorial.org/http://www.mysqltutorial.org/http://www.mysqltutorial.org/basic-mysql-tutorial.aspxhttp://www.mysqltutorial.org/mysql-stored-procedure-tutorial.aspxhttp://www.mysqltutorial.org/mysql-triggers.aspxhttp://www.mysqltutorial.org/http://www.mysqltutorial.org/http://www.mysqltutorial.org/http://www.mysqltutorial.org/http://www.mysqltutorial.org/http://www.mysqltutorial.org/mysql-views-tutorial.aspxhttp://www.mysqltutorial.org/mysql-stored-procedure-tutorial.aspxhttp://www.mysqltutorial.org/mysqltips.aspxhttp://www.mysqltutorial.org/mysql-administration.aspxhttp://www.mysqltutorial.org/mysql-functions.aspxhttp://www.mysqltutorial.org/mysql-views-tutorial.aspxhttp://www.mysqltutorial.org/mysql-triggers.aspxhttp://www.mysqltutorial.org/mysql-stored-procedure-tutorial.aspxhttp://www.mysqltutorial.org/basic-mysql-tutorial.aspxhttp://www.mysqltutorial.org/http://www.mysqltutorial.org/stored-procedures-loop.aspxhttp://www.mysqltutorial.org/mysql-stored-procedure-tutorial.aspxhttp://www.mysqltutorial.org/
  • 8/10/2019 MySQL Cursor With Example

    2/7

    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 dont update the data that is

    being used by an asensitive cursor. MySQL cursor is

    asensitive.

    You can use MySQL cursors in stored procedures, stored

    functions and triggers.

    Working with MySQL cursor

    First, you have to declare a cursor by using the DECLARE

    statement:

    The cursor declaration must be after any variable declaration. If

    you declare a cursor before variables declaration, MySQL will

    issue an error. A cursor must always be associated with a

    SELECT statement.

    Next, you open the cursor by using the OPEN statement. The

    OPEN statement initializes the result set for the cursor

    therefore you must call the OPEN statement before fetching

    rows from the result set.

    MySQL Quick Start

    Install MySQL Database

    Server

    Download MySQL Sample

    Database

    Load Sample Database

    MySQL Programming

    Interfaces

    PHP MySQL Tutorial

    Perl MySQL Tutorial

    Other Tutorials

    MySQL Full-Text Search

    MySQL Cheat Sheet

    MySQL Books

    MySQL Hosting

    MySQL Resources

    1 DECLAREcursor_nameCURSORFORSELECT_statement;

    1 OPENcursor_name;

    http://www.mysqltutorial.org/mysql-resources.aspxhttp://www.mysqltutorial.org/mysql-hosting.aspxhttp://www.mysqltutorial.org/mysqlbooks.aspxhttp://www.mysqltutorial.org/mysql-cheat-sheet.aspxhttp://www.mysqltutorial.org/mysql-full-text-search.aspxhttp://www.mysqltutorial.org/perl-mysql/http://www.mysqltutorial.org/php-mysql/http://www.mysqltutorial.org/how-to-load-sample-database-into-mysql-database-server.aspxhttp://www.mysqltutorial.org/mysql-sample-database.aspxhttp://www.mysqltutorial.org/install-mysql/http://www.mysqltutorial.org/variables-in-stored-procedures.aspxhttp://www.mysqltutorial.org/mysql-triggers.aspxhttp://www.mysqltutorial.org/mysql-stored-procedure-tutorial.aspxhttp://www.mysqltutorial.org/mysql-select-statement-query-data.aspx
  • 8/10/2019 MySQL Cursor With Example

    3/7

    Then, you use the FETCH statement to retrieve the next row

    pointed by the cursor and move the cursor to the next row in the

    result set.

    After that, you can check to see if there is any row available

    before fetching it.

    Finally, you call the CLOSE statement to deactivate the cursor

    and release the memory associated with it as follows:

    When the cursor is no longer used, you should close it.

    When working with MySQL cursor, you must also declare a NOT

    FOUND handler to handle the situation when the cursor could

    not find any row. Because each time you call the FETCH

    statement, the cursor attempts to read the next row in the result

    set. When the cursor reaches the end of the result set, it will not

    be able to get the data, and a condition is raised. The handler isused to handle this condition.

    To declare a NOT FOUND handler, you use the following

    syntax:

    Where finished is a variable to indicate that the cursor hasreached the end of the result set. Notice that the handler

    declaration must appear after variable and cursor declaration

    inside the stored procedures.

    The following diagram illustrates how MySQL cursor works.

    1 FETCHcursor_nameINTOvariableslist;

    1 CLOSEcursor_name;

    1 DECLARECONTINUEHANDLERFORNOTFOUNDSETfinished= 1;

  • 8/10/2019 MySQL Cursor With Example

    4/7

    MySQL Cursor Example

    We are going to develop a stored procedure that builds an emaillistof all employees in the employees table in the MySQL

    sample database.

    First, we declare some variables, a cursor for looping over the

    emails of employees, and a NOT FOUND handler:

    Next, we open the email_cursor by using the OPEN

    statement:

    Then, we iterate the email list, and concatenate all emails where

    each email is separated by a semicolon(;):

    After that, inside the loop we used the v_finished variable to

    checkif there is any email in the list to terminate the loop.

    1

    23

    4

    5

    6

    7

    8

    9

    10

    DECLAREfinishedINTEGERDEFAULT0;

    DECLAREemailvarchar(255)DEFAULT"";

    -- declare cursor for employee email

    DEClAREemail_cursorCURSORFOR

    SELECTemailFROMemployees;

    -- declare NOT FOUND handler

    DECLARECONTINUEHANDLER

    FORNOTFOUNDSETfinished= 1;

    1 OPENemail_cursor;

    1

    23

    4

    5

    6

    7

    8

    get_email: LOOP

    FETCHemail_cursorINTOv_email;IFv_finished= 1THEN

    LEAVEget_email;

    ENDIF;

    -- build email list

    SETemail_list= CONCAT(v_email,";",email_list);

    ENDLOOPget_email;

    http://www.mysqltutorial.org/mysql-sample-database.aspx
  • 8/10/2019 MySQL Cursor With Example

    5/7

    Finally, we close the cursor using the CLOSE statement:

    The build_email_list stored procedure is as follows:

    You can test the build_email_list stored procedure using

    the following script:

    In this tutorial, we have shown you how to use MySQL cursor to

    1 CLOSEemail_cursor;

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    1314

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    2627

    28

    29

    30

    31

    32

    33

    34

    35

    36

    DELIMITER$$

    CREATEPROCEDUREbuild_email_list (INOUTemail_listvarchar(

    BEGIN

    DECLAREv_finishedINTEGERDEFAULT0;

    DECLAREv_emailvarchar(100)DEFAULT"";

    -- declare cursor for employee email

    DEClAREemail_cursorCURSORFOR

    SELECTemailFROMemployees;

    -- declare NOT FOUND handlerDECLARECONTINUEHANDLER

    FORNOTFOUNDSETv_finished= 1;

    OPENemail_cursor;

    get_email: LOOP

    FETCHemail_cursorINTOv_email;

    IFv_finished= 1THEN

    LEAVEget_email;

    ENDIF;

    -- build email list

    SETemail_list= CONCAT(v_email,";",email_list);

    ENDLOOPget_email;

    CLOSEemail_cursor;

    END$$

    DELIMITER;

    1

    2

    3

    SET@email_list= "";

    CALLbuild_email_list(@email_list);

    SELECT@email_list;

  • 8/10/2019 MySQL Cursor With Example

    6/7

    Bagikan 0 24Like

    iterate a result set and process each row accordingly.

    Related Tutorials

    Getting Started with MySQL Stored Procedures

    MySQL Stored Procedure Variables

    Introduction to MySQL Stored Procedures

    Share this:

    About MySQL Tutorial

    Website

    MySQLTutorial.org is a

    website dedicated to MySQL

    database. We regularly

    publish useful MySQL

    tutorials to help web

    developers and database

    administrators learn MySQL

    fast and use MySQL

    effectively.

    Our MySQL tutorials are

    practical and easy-to-follow,

    with SQL script and

    screenshots available.More

    About Us

    MySQL is trademark of

    Oracle Corp.

    Recent MySQL

    Tutorials

    MySQL Export Table to CSV

    Raising Error Conditions with

    SIGNAL / RESIGNAL

    Statements

    MySQL Error Handling in

    Stored Procedures

    MySQL Stored Function

    Import CSV File Into MySQL

    Table

    MySQL Natural Sorting with

    ORDER BY Clause

    Modifying MySQL Events

    Working with MySQL

    Scheduled Event

    PHP MySQL BLOB

    PHP MySQL Transaction

    Site Links

    About Us

    Contact Us

    Request a Tutorial

    Privacy Policy

    Previous Tutorial:

    Loop in Stored ProceduresNext Tutorial:

    Listing Stored Procedures in a MySQL

    Database

    http://www.mysqltutorial.org/listing-stored-procedures-in-mysql-database.aspxhttp://www.mysqltutorial.org/stored-procedures-loop.aspxhttp://www.mysqltutorial.org/privacy-policy/http://www.mysqltutorial.org/request-mysql-tutorial/http://www.mysqltutorial.org/contact-us/http://www.mysqltutorial.org/about-us/http://www.mysqltutorial.org/php-mysql-transaction/http://www.mysqltutorial.org/php-mysql-blob/http://www.mysqltutorial.org/mysql-triggers/working-mysql-scheduled-event/http://www.mysqltutorial.org/mysql-triggers/modifying-mysql-events/http://www.mysqltutorial.org/mysql-natural-sorting/http://www.mysqltutorial.org/import-csv-file-mysql-table/http://www.mysqltutorial.org/mysql-stored-function/http://www.mysqltutorial.org/mysql-error-handling-in-stored-procedures/http://www.mysqltutorial.org/mysql-signal-resignal/http://www.mysqltutorial.org/mysql-export-table-to-csv/http://www.mysqltutorial.org/about-us/http://www.mysqltutorial.org/introduction-to-sql-stored-procedures.aspxhttp://www.mysqltutorial.org/variables-in-stored-procedures.aspxhttp://www.mysqltutorial.org/getting-started-with-mysql-stored-procedures.aspx
  • 8/10/2019 MySQL Cursor With Example

    7/7

    Copyright 2014 by www.mysqltutorial.org. All Rights Reserved.