SQL loop statement while introduces the instance

Source: Internet
Author: User

Declare @ I int
Set @ I = 1
While @ I <30
Begin
Insert into test (userid) values (@ I)
Set @ I = @ I + 1
End
---------------

While Condition
Begin
Perform operations
Set @ I = @ I + 1
End

While
Set the conditions for repeated execution of SQL statements or statement blocks. If the specified condition is true, the statement is executed repeatedly. You can use the break and continue keywords to control the execution of statements in the while loop within the loop.

Syntax
While boolean_expression
{SQL _statement | statement_block}
[Break]
{SQL _statement | statement_block}
[Continue]

Parameters
Boolean_expression

Returns a true or false expression. If a Boolean expression contains a SELECT statement, you must enclose the SELECT statement in parentheses.

{SQL _statement | statement_block}

Group statements defined by statement blocks. To define statement blocks, use the control flow keywords begin and end.

Break

This causes the exit from the while loop at the innermost layer. Execute any statement that appears after the end keyword. The end keyword is the end mark of the loop.

Continue

Execute the while loop again and ignore any statements after the continue keyword.

Note
If two or more while loops are nested, the break of the inner layer will exit the next outer loop. First, run all the statements after the inner loop ends, and then run the next outer loop again.

Example
A. Use break and continue in nested if... else and while
In the following example, if the average price is less than $30, the while loop will double the price and select the highest price. If the maximum price is less than or equal to $50, the while loop restarts and doubles the price again. This cycle continues to double the price until the maximum price exceeds $50, then exit the while loop and print a message.

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'

B. Use the while
The following while structure is part of the process named count_all_rows. In the following example, the while structure tests the return value of the function @ fetch_status for the cursor. Because @ fetch_status may return-2,-1, or 0, all cases should be tested. If a row is deleted from the cursor results after execution of this stored procedure, the row is skipped. After successfully extracting (0), the SELECT statement inside the begin... end loop is executed.

Use pubs
Declare tnames_cursor cursor
For
Select table_name
From information_schema.tables
Open tnames_cursor
Declare @ tablename sysname
-- Set @ tablename = 'author'
Fetch next from tnames_cursor into @ tablename
While (@ fetch_status <>-1)
Begin
If (@ fetch_status <>-2)
Begin
Select @ tablename = rtrim (@ tablename)
Exec ('select' + @ tablename + ''' = count (*) from'
+ @ Tablename)
Print''
End
Fetch next from tnames_cursor into @ tablename
End
Close tnames_cursor
Deallocate tnames_cursor

Declare @ I int
Declare @ quxian varchar (2000), @ city varchar (2000), @ Sheng varchar (2000), @ Hot int
Set @ I = 1
While @ I <30
Begin
Set @ quxian = 'xihu'
Set @ city = 'hangzhou'
Set @ Sheng = 'zhejiang'
Set @ Hot = @ I
Insert into address (quxian, city, Sheng, hot) values (@ quxian, @ city, @ Sheng, @ Hot)
Print @ I
Set @ I = @ I + 1
End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.