MySQL
  Home arrow MySQL arrow Page 2 - Delving Deeper into MySQL 5.0
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? 
Google.com  
MYSQL

Delving Deeper into MySQL 5.0
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    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:
      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


    Delving Deeper into MySQL 5.0 - REPEAT
    ( Page 2 of 4 )

     

    REPEAT is another useful looping construct. It’s somewhat easier to use in some circumstances than LOOP , because it has a terminating condition built into its syntax, as shown here:

    [label:] REPEAT
    statement-block
    UNTIL condition
    END REPEAT [label];

    The block of statements appearing between the REPEAT and UNTIL keywords is executed repeatedly until the condition indicated by UNTIL is met. Here’s an example of a stored procedure (named repeat_sum) that gets the sum of an integer and all positive integers less than itself using a REPEAT ... UNTIL loop:

     

    In this procedure, we start by declaring a counter equal to 0, then, each time we go through the REPEAT loop, we increment it, adding it to the output parameter output, and then checking to see if it’s yet equal to the original value. When counter and value are equal, we exit the loop. We can test it as shown here:


    CAUTION
      When writing REPEAT loops, make sure that the condition following UNTIL will be met at some point. Otherwise you’ll have an endless loop, which means you’ll quite likely have to kill the thread and start over.

    WHILE

    The WHILE loop in some ways can be regarded as the inverse of a REPEAT loop. With a REPEAT loop, statements are executed until some condition is met. In a WHILE loop, execution of the statement block following the DO keyword continues only so long as the specified condition is true.

    Here’s the formal syntax for WHILE:

    [label:] WHILE condition DO
    statement-block
    END WHILE [label];

    In the next example, we use a WHILE loop in a stored function to obtain the factorial of an integer:

    The factorial of an integer is defined as the product of that integer and all the positive integers less than itself and greater than 1, so the factorial of 5 (often written as 5!) is 5 X 4 X 3 X 2 = 120, as you can see from our test of the factorial function above. 0! (zero factorial) is defined as 1 and the factorial of a negative number is undefined. With these additional conditions in mind, you could write a somewhat improved version of the stored function. This better_factorial function uses a CASE block to distinguish the cases where the input value is equal to or less than zero, and to act accordingly as shown here:

    CREATE FUNCTION better_factorial(value INT ) RETURNS INT
    BEGIN
     
    DECLARE temp INT;
      DECLARE output INT DEFAULT 1;
     
    SET temp = value;
     
    CASE
        WHEN value < 0 THEN SET output = 0; 
        WHEN value = 0 OR value = 1 THEN SET output = 1;
        ELSE
         
    WHILE temp > 1 DO
            SET output = output * temp;
            SET temp = temp - 1;
          END WHILE;
      END CASE;
     
    RETURN output;
    END

    This example also illustrates how you can nest multiple flow-control con structs inside one another within a single stored function or stored procedure.



     
     
    >>> More MySQL Articles          >>> More By Apress Publishing
     

       

    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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek