471,591 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,591 software developers and data experts.

For Loop

Can U Tell Me How To Use For Loop
Apr 11 '07 #1
1 8062
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 XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.