For Loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nikunjmochi
    New Member
    • Apr 2007
    • 1

    For Loop

    Can U Tell Me How To Use For Loop
  • iburyak
    Recognized Expert Top Contributor
    • Nov 2006
    • 1016

    #2
    There is no For Loop in SQL only While loop.

    See example from help on how to use it.

    A. Use BREAK and CONTINUE with nested IF...ELSE and WHILE
    In this example, if the average price is less than $30, the WHILE loop doubles the prices and then selects the maximum price. If the maximum price is less than or equal to $50, the WHILE loop restarts and doubles the prices again. This loop continues doubling the prices until the maximum price is greater than $50, and then exits the WHILE loop and prints a message.
    Code:
    USE pubs
    GO
    WHILE (SELECT AVG(price) FROM titles) < $30
    BEGIN
       UPDATE titles
          SET price = price * 2
       SELECT MAX(price) FROM titles
       IF (SELECT MAX(price) FROM titles) > $50
          BREAK
       ELSE
          CONTINUE
    END
    PRINT 'Too much for the market to bear'
    Good Luck

    Comment

    Working...