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 <运算式> ]
,