Process Control Statements:
BEGIN ... END |
WAITFOR |
Goto |
While |
IF ... ELSE |
Break |
RETURN |
Continure |
|
1.BEGIN ... END
BEGIN ... The end statement is used to combine multiple Transact-SQL statements into a single logical block. When a Process Control statement must execute a block of statements that contains two or more T-SQL statements, use begin ... End statement.
1 BEGIN 2 {sql_statement ...} 3 END
2.IF
1 IF < conditional expression >2 | program Block}
IF ... ELSE
1 IF < conditional expressions >2 | program Block 1} 3 ELSE 4 | Program Block 2}
Code:
1 Declare @x int2 Set @x=43 if @x%2=04 Print '@x is even'5 Else6 Print '@x is an odd number'
:
3.CASE
1 Case input_expression 2 when Then result_expression 3 [...n] 4 5 ELSE else_result_expression 6 7 END
Code:
1 UsePractice2 Go3 Select *Note= Case4 whenSex='Girl' Then 'female'5 whenSex=' Boy' Then 'male'6 Else 'not recognized'7 End8 fromInfo_stu
Effect:
4.WHILE
Code:
1 DECLARE @n int,@sum int2 Set @n=13 Set @sum=04 while @n<=Ten5 begin6 Set @sum=@sum+@n7 Set @n=@n+18 End9 Print @sum
Effect:
SQL Server Process Control