日期:2014-05-17  浏览次数:20489 次

CASE语句问题
我用T-SQL语句写段修改编号的代码,总是报CASE语句语法错误。代码如下:
declare @propertyno char (10)
declare @prno int(3)
select @propertyno=max(propertyno) from property where propertyno like 'hg%'
SELECT @propertyno = dbo.F_Get_no(@propertyno)+1
 
case len(@propertyno)
  when '1' then @propertyno = 'XX00000+'@propertyno
  when '2' then @propertyno = 'XX0000+'@propertyno
  when '3' then @propertyno = 'XX000+'@propertyno
  when '4' then @propertyno = 'XX00+'@propertyno
  when '5' then @propertyno = 'XX0+'@propertyno
  when '6' then @propertyno = 'XX+'@propertyno
else 
  print 'propertyno数据出错'
end 
print @propertyno

不知道是什么原因,请大家帮帮忙。
CASE语句

------解决方案--------------------
我猜一下你的意图

case len(@propertyno)
  when '1' then @propertyno = 'XX00000+'@propertyno
  when '2' then @propertyno = 'XX0000+'@propertyno
  when '3' then @propertyno = 'XX000+'@propertyno
  when '4' then @propertyno = 'XX00+'@propertyno
  when '5' then @propertyno = 'XX0+'@propertyno
  when '6' then @propertyno = 'XX+'@propertyno
else 
  print 'propertyno数据出错'
end 

这段,改成
SELECT @propertyno=
case len(@propertyno)
  when '1' then  'XX00000+'@propertyno
  when '2' then 'XX0000+'@propertyno
  when '3' then  'XX000+'@propertyno
  when '4' then  'XX00+'@propertyno
  when '5' then 'XX0+'@propertyno
  when '6' then  'XX+'@propertyno
else 
  print 'propertyno数据出错'
end 
------解决方案--------------------
下面的语句是有问题的,sql server的t-sql不支持case 选择结构,

case 语句只能在一个sql语句中使用的

case len(@propertyno)
  when '1' then @propertyno = 'XX00000+'@propertyno
  when '2' then @propertyno = 'XX0000+'@propertyno
  when '3' then @propertyno = 'XX000+'@propertyno
  when '4' then @propertyno = 'XX00+'@propertyno
  when '5' then @propertyno = 'XX0+'@propertyno
  when '6' then @propertyno = 'XX+'@propertyno
else 
  print 'propertyno数据出错'
end
 
是有问题的
------解决方案--------------------
SELECT @propertyno=
case len(@propertyno)
  when '1' then  'XX00000+'@propertyno
  when '2' then 'XX0000+'@propertyno
  w