码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2015-03-19 23:31:08      阅读:273      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!