MySQL
  Home arrow MySQL arrow Page 2 - Error Handling
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Error Handling
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2007-08-30


    Table of Contents:
  • Error Handling
  • Handling Last Row Conditions
  • Condition Handlers
  • Handler Conditions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Error Handling - Handling Last Row Conditions
    ( Page 2 of 4 )

    One of the most common operations in a MySQL stored program involves fetching one or more rows of data. You can do this in a stored program through the use of a cursor (explained in Chapter 5). However, MySQL (and the ANSI standard) considers an attempt to fetch past the last row of the cursor an error. Therefore, you almost always need to catch that particular error when looping through the results from a cursor.

    Consider the simple cursor loop shown in Example 6-4. At first glance, you might worry that we might inadvertently have created an infinite loop, since we have not coded any way to leave the dept_loop loop.

    Example 6-4. Cursor loop without a NOT FOUND handler

    CREATE PROCEDURE sp_fetch_forever()
     
    READS SQL DATA
    BEGIN
       
    DECLARE l_dept_id INT;
       
    DECLARE c_dept CURSOR FOR
               
    SELECT department_id
                 
    FROM departments;

        OPEN c_dept;
       
    dept_cursor: LOOP
           
    FETCH c_dept INTO l_dept_id;
       
    END LOOP dept_cursor;
       
    CLOSE c_dept;
    END

    Bravely, we run this program and find that the seemingly infinite loop fails as soon as we attempt to fetch beyond the final row in the result set:

      mysql> CALL sp_fetch_forever();
     
    ERROR 1329 (02000): No data to FETCH

    Since we likely want to do something with the data after we’ve fetched it, we cannot let this exception propagate out of the procedure unhandled. So we will add a declaration for a CONTINUE HANDLER in the procedure, setting a flag to indicate that the last row has been fetched. This technique is shown in Example 6-5.

    Example 6-5. Cursor loop with a NOT FOUND handler

    1 CREATE PROCEDURE sp_not_found( )
    2     READS SQL DATA
    3 BEGIN
    4     DECLARE l_last_row INT DEFAULT 0;
    5     DECLARE l_dept_id INT;
    6     DECLARE c_dept CURSOR FOR
    7           SELECT department_id
    8             FROM departments;
    9    /* handler to set l_last_row=1 if a cursor returns no more rows */
    10   DECLARE CONTINUE HANDLER FOR NOT FOUND SET l_last_row=1;
    11
    12   OPEN c_dept ;
    13   dept_cursor: LOOP
    14        FETCH c_dept INTO l_dept_id;
    15         IF (l_last_row=1) THEN
    16             LEAVE dept_cursor;
    17         END IF;
    18         /* Do something with the data*/
    1 9
    20   END LOOP dept_cursor ;
    21   CLOSE c_dept;
    2 2
    23 END ;

    In plain English, the handler on line 10 says “When a fetch from a cursor returns no more rows, continue execution, but set the variable l_last_row to 1.” After retriev ing each row, we check the l_last_row variable and exit from the cursor loop if the last row is returned. Without this handler, our cursor loop will fetch too many times and raise an exception.

    Now that you have seen two simple examples of declaring handlers for error situations that you can anticipate, let’s explore this functionality in more detail.



     
     
    >>> More MySQL Articles          >>> More By O'Reilly Media
     

       

    MYSQL ARTICLES

    - MySQL Security Tips
    - Designing a MySQL Database: Tips and Techniq...
    - The Three Most Important MySQL Queries
    - Null and Empty Strings
    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT