469,085 Members | 1,010 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,085 developers. It's quick & easy.

For Loop

Can U Tell Me How To Use For Loop
Apr 11 '07 #1
1 7993
iburyak
1,017 Expert 512MB
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.
Expand|Select|Wrap|Line Numbers
  1. USE pubs
  2. GO
  3. WHILE (SELECT AVG(price) FROM titles) < $30
  4. BEGIN
  5.    UPDATE titles
  6.       SET price = price * 2
  7.    SELECT MAX(price) FROM titles
  8.    IF (SELECT MAX(price) FROM titles) > $50
  9.       BREAK
  10.    ELSE
  11.       CONTINUE
  12. END
  13. PRINT 'Too much for the market to bear'
Good Luck
Apr 11 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Charles Alexander | last post: by
43 posts views Thread by Gremlin | last post: by
5 posts views Thread by Martin Schou | last post: by
32 posts views Thread by Toby Newman | last post: by
2 posts views Thread by Alex | last post: by
3 posts views Thread by Ben R. | last post: by
32 posts views Thread by cj | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.