SQL Process Control Command in basic MSSQL tutorial

Source: Internet
Author: User
The process control commands used by the Transact-SQL language are similar to the common programming languages, which mainly include the following control commands. 4.6.1IFELSE its syntax is as follows: IF condition expression command line or program block [ELSE [condition expression] command line or program block] Where condition expression can be a combination of various expressions, however, the expression value must be a logical value.

The process control commands used by the Transact-SQL language are similar to the common programming languages, which mainly include the following control commands. 4.6.1 IFELSE Syntax: IF condition expression: command line or program block [ELSE [condition expression] command line or program block] Where condition expression can be a combination of various expressions, however, the expression value must be a logical value.

The process control commands used by the Transact-SQL language are similar to the common programming languages, which mainly include the following control commands.

4.6.1 IF... ELSE

The syntax is as follows:

IF <条件表达式>

   <命令行或程序块>

[ELSE [conditional expression]

   <命令行或程序块> ]

Where <条件表达式> It can be a combination of various expressions, but the value of the expression must be a logical value of "true" or "false ". The ELSE clause is optional. The simplest IF statement does not have the ELSE clause. IF... ELSE is used to determine that a program is executed when a condition is set, and another program is executed when the condition is not met. IF you do not use a block, IF or ELSE 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' -- print the string 'x> y'

Else if @ y> @ z

Print 'y> Z'

Else print 'z> y'

The running result is as follows:

Z> y

Note: At most 32 levels can be nested in Transact-SQL.

4.6.2 BEGIN... END

The syntax is as follows:

BEGIN

   <命令行或程序块>

END

BEGIN... END is used to set a program block... All programs in END are considered as a unit to execute BEGIN... END is often used in conditional statements, such as IF... ELSE. In BEGIN... Another BEGIN… can be nested in the END... END to define another program block.

4.6.3 CASE

The CASE command has two statement formats:

CASE <运算式>

WHEN <运算式> THEN <运算式>

...

WHEN <运算式> THEN <运算式>

[ELSE <运算式> ]

,

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.