一、IF ... ELSE语句
1、使用:
declare @num int
select @num=AVG(分数)
from 成绩信息
select @num
if @num>90
BEGIN
PRINT '这门课老师教课非常成功!'
end
ELSE
PRINT '这门课老师还需要继续努力!'
2、IF…ELSE语句嵌套
declare @num int
select @num=AVG(分数)
from 成绩信息 where 课程编号 ='2' and 考试编号='0801'
select @num
if @num>=60
BEGIN
if @num<70
print '成绩刚刚及格,还需要继续努力!'
else if @num<80
print '成绩中等'
else if @num<90
print '成绩良好'
else print '成绩优秀'
end
ELSE
PRINT '这个老师教学方法还需要改进!'
二、case语句
1、语法格式
CASE input_expression
WHEN when_expression THEN result_expression
[…n]
[ELSE else_result_expression]
END
2、case语句示例
select 员工姓名,所任职位,员工职称=
case 所任职位
when '经理' then '高级职称'
when '主管' then '中级职称'
when '职员' then '初级职称'
else '其他职称'
end
from 员工信息
三、While循环语句
语法格式
WHILE boolean_expression
{sql_statement|statement_block}
[BREAK]
{sql_statement|statement_block}
[CONTINUE]
{sql_statement|statement_block}
实例
declare @i int,@num int
set @i=1
while @i<10
begin
set @num=@num*@i
set @i=@i+1
end
print @num
四、WAITFOR延迟语句
语法格式
WAITFOR
{
DELAY time
|TIME time
}
示例
waitfor time '15:37:30'
exec sp_help
使用time是在一个具体时间执行下面的命令
waitfor delay '00:00:05'
exec sp_help
使用delay是延迟多少时间执行下面的语句