MSSQL basic instruction SQL Process Control command

Source: Internet
Author: User
Tags command line expression mssql

The process control commands used in Transact-SQL languages are similar to the common programming languages, with the following control commands.

4.6.1 IF ... ELSE

The syntax is as follows:

IF < conditional expression >

< command line or program block >

[ELSE [conditional expression]

< command line or program block

where < conditional expression > can be a combination of various expressions, the value of an expression must be the logical value TRUE or FALSE. The ELSE clause is optional, and the simplest if statement has no ELSE clause part. IF ... else is used to determine the execution of a procedure when a condition is set up and to execute another procedure when the condition is not established. If you do not use a program block, if or else you can only execute one command. IF ... else can be nested.

Example 4-9

declare@x int,@y Int,@z int

Select @x=1,@y=2, @z=3

If@x>@y

print ' X>y '--printing string ' x>y '

else if@y>@z

print ' Y>z '

else print ' z>y '

The results of the operation are as follows

Z>y

Note: Up to 32 levels can be nested in Transact-SQL.

4.6.2 BEGIN ... End

The syntax is as follows:

BEGIN

< command line or program block >

End

BEGIN ... End is used to set a program block that will be in the begin ... End of all programs as a unit execution begin ... End often in conditional statements, such as if ... else in use. At the begin ... End can be nested in another begin ... End to define another program block.

4.6.3 case

The case command has two statement formats:

Case < expression >

When < expressions >THEN< expressions >

...

when< Expressions >THEN< Expressions >

[else< expression;

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.