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;