注册 登录
编程论坛 SQL Server论坛

请教case语句用法问题

feige3721 发布于 2010-04-09 17:13, 939 次点击
这两段编程很相似,就是一个很简单的给成绩加评语,但是有一个运行不出来,请高手指点
此为运行错误的,我刚开始学,自己编的
declare @cj float,@msg varchar(100)
set @cj=85
case
 when @cj>=90 and @cj<100 then  set @msg='优秀'
 when @cj>=80 and @cj<90 then set @msg='良好'
 when @cj>=70 and @cj<80 then set @msg='中等'
 when @cj>=60 and @cj<70 then set @msg='及格'
 when @cj>=0 and @cj<60  then set @msg='不及格'
else
  set @msg='你输入的成绩不对,成绩应该在0~100之间'
end
print @msg

下面是运行正确的,只是把变量放在了case语句前面
declare @cj float,@msg varchar(100)
set  @cj=85
set  @msg=
case
 when @cj>=90 and @cj<100 then '优秀'
 when @cj>=80 and @cj<90 then '良好'
 when @cj>=70 and @cj<80 then '中等'
 when @cj>=60 and @cj<70 then '及格'
 when @cj>=0 and @cj<60  then '不及格'
else
  '你输入的成绩不对,成绩应该在0~100之间'
end
print @msg
5 回复
#2
feige37212010-04-09 18:10
补充错误提示
消息 156,级别 15,状态 1,第 3 行
关键字 'case' 附近有语法错误。
消息 156,级别 15,状态 1,第 5 行
关键字 'when' 附近有语法错误。
消息 156,级别 15,状态 1,第 6 行
关键字 'when' 附近有语法错误。
消息 156,级别 15,状态 1,第 7 行
关键字 'when' 附近有语法错误。
消息 156,级别 15,状态 1,第 8 行
关键字 'when' 附近有语法错误。
消息 156,级别 15,状态 1,第 9 行
关键字 'else' 附近有语法错误。
消息 156,级别 15,状态 1,第 12 行
关键字 'print' 附近有语法错误。
#3
zhuyunshen2010-04-09 18:35
do case 表达式
 {   case 值:
      {}
   .
   .
   .
    default {}
 }
#4
czyzhuo2010-04-10 09:09
你自己写的case语句,语法都错了,肯定报错啦
#5
happynight2010-04-10 09:50
CASE 是一个函数 不是一个语句 所以你在CASE里是不能作SET动作的
#6
cnfarer2010-04-10 22:06
then 后面只能是一个表达式
1