读书人

CASE语句有关问题

发布时间: 2013-11-21 23:38:25 作者: rapoo

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
when '3' then 'XX000+'@propertyno
when '4' then 'XX00+'@propertyno
when '5' then 'XX0+'@propertyno
when '6' then 'XX+'@propertyno
else
set @propertyno='propertyno数据出错'
end
[解决办法]
try this,


declare @propertyno char(50)
declare @prno int

select @propertyno=max(propertyno)
from property
where propertyno like 'hg%'

select @propertyno=dbo.F_Get_no(@propertyno)+1

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
'propertyno数据出错'
end

print @propertyno

读书人网 >SQL Server

热点推荐