回 帖 发 新 帖 刷新版面

主题:sql server中的T-sql语言的case怎么用啊.

我自己写了两个case语句:
一个是简单的case语句:
declare @i int
set @i=1
case @i*@i
  when 1 then print'@i=1'
  when 2  then print'@i=2'
else print'@i是另外的值'
end 

提示错误,case语句附近有错误,但@i*@i是最简单的表达式

一个搜索的case语句
declare @i int ,@sum int
set @i=1
case 
  when @i=1 then print '@i=1'
  when @i=2 then print '@i=2'
end
这个也不行,郁闷了,谁来写两个正确的给小弟参考下啊.

回复列表 (共1个回复)

沙发

自己完成:
declare @i char(10),@bb int
set @bb=4
set @i=case   --设置case的返回值赋给@i
  when @bb>6 then  '@bb>6'  --但条件=then时 返回‘@bb>6’
  when @bb>5 then  '@bb>5'
  when @bb>4 then  '@bb>4'
  when @bb>3 then  '@bb>3'
  when @bb>2 then '@bb>2'
end
print @i--输出@i信息

declare @i char(10)
select @i=case isnumeric(11.22)--case对象只能返回值,而不能执行语句或条件句
  when 1 then '正确'  --1是函数isnumeric的返回值是1或0
  when 0 then  '错误'
end  --少有的需要结束end语句
print @i        --所以这里输出@i的返回值

我来回复

您尚未登录,请登录后再回复。点此登录或注册