Flow control statements in SQL Server

Source: Internet
Author: User
Tags goto

    • Begin End

The statement defines a block of SQL code that is typically used in the IF and while statements

Declare @num int ;Set @num=0; while  @num<Tenbegin    Set @num=@num+1; Print 'Hello Word'End

    • If Else

Conditional Judgment statement, where else is optional

if  (Selectfrom where name=' Zhang San ') = 1    Print ' the sex of Zhang San is: male ' Else    Print ' the sex of Zhang San is: female '

    • While , break, continue
Declare @num int ;Set @num=0; while  @num<Tenbegin    Set @num=@num+1; Print 'Hello Word'    if @num=2        Continue    if @num=5         BreakEnd

Description: This example outputs 5 lines Hello Word

    • Goto label (custom tag)

This statement is used to unconditionally transfer the execution order of statements to user-defined lable

Declare @num int;Set @num=0; Echo:Print 'Hello Word'Set @num=@num+1; while  @num<Tenbegin    GotoEchoEnd

    • Return

The statement is used to unconditionally exit a query or a procedure

Declare @num int ;Set @num=0; while  @num<Tenbegin    Set @num=@num+1; Print 'Hello Word'    if @num=5        returnEnd

    • WAITFOR Delay/time

The statement is used to define a time of day, executing a block of statements. WAITFOR DELAY ' time ' indicates how long it will take to wait, and waitfor when to wait for the moment to execute.

Example: Output ' Hello word ' after 10 seconds

waitfor ' 00:00:10 ' Print ' Hello Word ' -- or Print ' Hello Word ' waitfor ' 00:00:10 '

Example: 12:00 clock output ' Hello word '

waitfor ' 12:00:00 ' Print ' Hello Word ' -- or Print ' Hello Word ' waitfor ' 12:00:00 '

The flow control statements in SQL Server are described here.

Flow control statements in SQL Server

Related Article

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.