T-SQL 逻辑控制语句 ifelse while casewhen

ifelse,如果逻辑语句有多行,用begin end 包裹

T-SQL 逻辑控制语句 ifelse while casewhen

 use StudentManageDB
go
--查询成绩
declare @cAvg int
select @cAvg=avg(CSharp) from ScoreList
inner join Students on ScoreList.StudentId=Students.StudentId where ClassId=1
print 'C#平均成绩:'+convert(varchar(20),@cAvg)
--判断成绩
if(@cAvg>=80)
print '软件一班成绩优秀!'
else
print '软件一班成绩一般!'

while,如果逻辑语句有多行,用begin end 包裹

T-SQL 逻辑控制语句 ifelse while casewhen

 use StudentManageDB
go
print '加分之前的C#成绩:'
select StudentId,CSharp from ScoreList
declare @CSharp int,@StuId int
while(1=1)
begin
select top 1 @CSharp=CSharp,@StuId=StudentId
from ScoreList where CSharp<60
if (@CSharp<60)
update ScoreList set CSharp=CSharp+1
where StudentId=@StuId
if((select count(*) from ScoreList where CSharp<60)=0)
break
end
print '加分之后的C#成绩:'
select StudentId,CSharp from ScoreList

case when

T-SQL 逻辑控制语句 ifelse while casewhen

 use StudentManageDB
go
select 学号=StudentId,
总评=CASE
when (CSharp+SQLServerDB)/2>=90 then 'A'
when (CSharp+SQLServerDB)/2 between 80 and 89 then 'B'
when (CSharp+SQLServerDB)/2 between 70 and 79 then 'C'
when (CSharp+SQLServerDB)/2 between 60 and 69 then 'D'
else '不及格'
end
from ScoreList
上一篇:IOS 读取本地的Json/plist 文件


下一篇:java开发环境配置——Maven