当前位置: 代码迷 >> Sql Server >> 如何才能生成一行1 2 3 4 5 6 7 8…
  详细解决方案

如何才能生成一行1 2 3 4 5 6 7 8…

热度:86   发布时间:2016-04-27 20:11:04.0
怎么才能生成一行1 2 3 4 5 6 7 8……
怎么才能生成一行1   2   3   4   5   6   7   8……   有没有什么直接的方法或者函数   ?

------解决方案--------------------
ALTER function tmp2(@i int)
returns varchar(1000)
as
begin
declare @j varchar(1000)
declare @start int
set @start=1
set @j= ' '

while @start <[email protected]
begin
set @[email protected]+ ' '+cast(@start as varchar)
set @[email protected]+1
end
return @j
end
------解决方案--------------------
不依赖于系统表的

declare @s varchar(8000)
set @s= ' '

select cast(rtrim(t1.a)+rtrim(t2.a) as int) as id into #t
from (select 0 a
union select 1
union select 2
union select 3
union select 4
union select 5
union select 6
union select 7
union select 8
union select 9)t1,
(select 0 a
union select 1
union select 2
union select 3
union select 4
union select 5
union select 6
union select 7
union select 8
union select 9)t2

select @[email protected]+ ' '+rtrim(id)
from #t
order by id
set @s=stuff(@s,1,1, ' ')
print @s

drop table #t

------解决方案--------------------
declare @Tab table(iid int)
declare @strTotal varchar(8000)
set @strtotal= ' '
declare @inti int
set @inti=0
while @inti <100
begin
set @[email protected]+1
insert into @tab values(@inti)
end

select @[email protected]+cast(iid as varchar)+ ' ' from @tab
print @strtotal
  相关解决方案