MySQL
  Home arrow MySQL arrow Delving Deeper into MySQL 5.0
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 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

Delving Deeper into MySQL 5.0
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 8
    2006-05-04

    Table of Contents:
  • Delving Deeper into MySQL 5.0
  • REPEAT
  • Cursors
  • Cursor Example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    The Web Buyer's Guide is your best source for white papers on a wide range of IT products and services. This Week's Featured White Papers: Taming the Threat Landscape by Symantec

    Delving Deeper into MySQL 5.0
    (Page 1 of 4 )

    MySQL 5.0 gives the developer access to features that earlier versions of MySQL do not support. These include stored procedures and stored functions. This article, the third in a series, continues our exploration of the potential of these two features. It is excerpted from chapter eight of Beginning MySQL Database Design and Optimization: From Novice to Professional, written by Jon Stephens and Chad Russell (Apress, ISBN: 1590593324).

    Looping in Procedures

    One major enhancement that stored procedures and stored functions provide to you in your SQL programming is that they give you the ability to execute code in loops. MySQL 5.0 supports three looping constructs: LOOP , REPEAT , and WHILE . They are largely equivalent, and which one you use depends on both the situation you’re in and your own style or preferences. Each looping construct allows you to perform an action or set of actions a specified number of times, while a condition is true, or until a desired condition has been met. We’ll examine these and provide you with some working examples in the next three sections of this chapter.


    NOTE  
    While there is no FOR or FOREACH loop in MySQL procedures, controlling loops by means of a counter is not difficult to accomplish using any of the three looping constructs that are available, as you’ll see from some of the examples in the following sections.

    LOOP, LEAVE, ITERATE

    The most basic looping construct of the three offered in MySQL 5.0 and newer is LOOP, which, together with a set of labels, identifies a block of SQL code that is to be repeated one or more times. The labels must appear at both the beginning and end of the loop, and must match. A beginning label must be followed with a colon ( : ) and precedes the LOOP keyword; an ending label follows the END LOOP keywords but precedes the final semicolon ( ; ) marking the end of the loop. A label can be any valid MySQL identifier.


    TIP
    A LEAVE statement (see the text that follows) won’t function without a label. Therefore, you should label every LOOP construct that you use in a stored procedure or stored function. Labels aren’t quite so necessary with REPEAT and WHILE (see the next two sections), but using them in longer procedures will help make your code easier to read and maintain.

    The syntax for a loop with a label is shown here:

    label: LOOP
    END LOOP label;

    Once begun, a LOOP will repeat indefinitely until something happens to cause execution to pass out of it. In order for it to be useful, MySQL provides a statement for doing just that: the LEAVE statement:

    LEAVE label;

    If you’re familiar with the break keyword in programming languages such as Java, Perl, C, or PHP, then you’ll find that LEAVE works in much the same fashion; it terminates the loop at the point where LEAVE is encountered.

    There’s also a statement that causes execution to pass back to the beginning of a loop. This is the ITERATE statement, which, like LEAVE , also uses a label:

    ITERATE label;

    In this example, we create a stored function using LOOP , LEAVE , and ITERATE . This function, which we’ve named loop_example, is basically a reimplementation of the integer division ( DIV ) operator that became available in MySQL 4.1; it takes two integers as arguments, and returns the number of times that the first one will go into the second without regard to any fraction or remainder:

    Here’s the loop_example function in use:

    Let’s look more closely at how the LOOP construct in this function works, by repeating the code for it with some comments:

    myloop:  # marks the beginning of the loop "myloop"
    LOO P
      SET temp = temp - start;   # perform subtraction
      SET counter = counter + 1; # increment counter
     
    IF temp >= start # is the new value greater than the input value?
        THEN ITERATE myloop;  # if it is, go back to the "myloop" label
      END IF;
     
    LEAVE myloop;  # (otherwise) break out of the loop
    END LOOP myloop; # marks the end of the loop "myloop"

    We could also have written this loop as

    myloop:
    LOOP
      SET temp = temp - start;
      SET counter = counter + 1;
     
    IF temp < start
        THEN LEAVE myloop;
      END IF;
    END LOOP myloop;

    In this instance, the ITERATE wasn’t really necessary; however, in more complex loops, you may want to return to the beginning of the loop from one of several dif ferent points, depending on the circumstances; ITERATE makes this possible.

    More MySQL Articles
    More By Apress Publishing


       · This article is an excerpt from the book "Beginning MySQL Database Design and...
     

    Buy this book now. This article is excerpted from chapter eight of Beginning MySQL Database Design and Optimization: From Novice to Professional, written by Jon Stephens and Chad Russell (Apress, ISBN: 1590593324). Check it out today at your favorite bookstore. Buy this book now.

       

    MYSQL ARTICLES

    - 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...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...
    - Creating the Blog Script for a PHP/MySQL Blo...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway