首页 > 数据库技术 > 详细

SQL里面的IF ELSE,没有{},使用begin...end

时间:2015-03-19 23:30:52      阅读:657      评论:0      收藏:0      [点我收藏+]

--IF ELSE
--特点:
--1.没有{},使用begin...end
--2.有多重和嵌套
--3.if必须有处理语句,如果只有一句,也可以不使用begin..end,只包含后面一句
--4.没有true/false的概念,只能使用关系运算符的条件表达式
--5.if后面的括号可以没有

--计算office平均分数并输出,如果平均分数超过60分输出成绩最高的三个学生的成绩,否则输出后三名的学生
go
declare @subjectname nvarchar(50)=‘office‘ --科目名称
declare @subjectId int=(select subjectId
from subject where subjectname=@subjectname) --获取科目ID
declare @avg int --科目平均分
set @avg=(select AVG(StudentResult) from Result
where SubjectId=@subjectId and StudentResult is not null) --获取指定科目的平均分
--做判断
if (@avg>=95)
begin
print ‘成绩不错。输出前三名‘
select top 3 * from Result where SubjectId=@subjectId
and StudentResult is not null order by StudentResult desc
end
else
begin
print ‘成绩不好。输出后三名‘
select top 3 * from Result where SubjectId=@subjectId
and StudentResult is not null order by StudentResult
end
-------------
go
declare @name nvarchar(50)=‘office‘--科目名称
declare @id int =(select SubjectId from Subject where SubjectName =@name )--科目ID
declare @avg int = (select AVG (StudentResult ) from Result
where SubjectId =@id and StudentResult is not null )--获取指定科目的平均分
--做判断
if(@avg >=95)
begin
print ‘成绩不错,输出前三名‘
select top 3*from Result where SubjectId =@id and StudentResult is not null
order by StudentResult desc
end
else
begin
print ‘成绩不好,输出后三名‘
select top 3 *from Result where SubjectId =@id and StudentResult is not null
order by StudentResult asc
end





SQL里面的IF ELSE,没有{},使用begin...end

原文:http://www.cnblogs.com/dianshen520/p/4352002.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!