有关sql的小小问题,多多指教!!!^_^

来源:百度知道 编辑:UC知道 时间:2024/06/11 20:42:37
declare @i nvarchar(50)
set @i=''
declare @sum int
set @sum=0
while @sum<=len(a.n)
begin
select (set @i=@i+c)as 最终结果
from (select substring(a.n,@sum,1)as c from(select 'asdf1*h456' as n)a )
where ascii(c) between 48 and 57
set @sum=@sum+1
end

运行后报错,哪位高手能指点迷津,是哪里出错了?
c 代表用substring函数截取的那个字符,而ascii 是把字符转化为 ASCII码的函数

错误有两处!
错误1
while @sum<=len(a.n) 这里a.n是什么???代表(select 'asdf1*h456' as n)a?? 你那是字符串不是数据库里的表的列。

错误2
select (set @i=@i+c)as 最终结果
from (select substring(a.n,@sum,1)as c from(select 'asdf1*h456' as n)a )
where ascii(c) between 48 and 57

@i 的赋值错误 需要去掉set ,并且要 在a) 后面加上一个代码
还有你处理的不是表,没必要写那么复杂

下面是我帮你改的

declare @i nvarchar(50)
set @i=''
declare @sum int
set @sum=0
while @sum<=len('asdf1*h456')
begin
select @i=@i+c
from (select substring(a.n,@sum,1)as c from(select 'asdf1*h456' as n)a ) b
where ascii(c) between 48 and 57
set @sum=@sum+1
end
print @i

答案是 1456

----------------下面的另外一种写法,你的写法有点复杂
declare @i nvarchar(50)
set @i=''
declare @sum int
set @sum=0
while @sum<=len('asdf1*h456')
begin
select @i=@i+c
from (selec