怎么才能生成一行1 2 3 4 5 6 7 8…… 有没有什么直接的方法或者函数 ?

解决方案 »

  1.   

    select top 100 id=identity(int,1,1) into #t from sysobjects,syscolumnsdeclare @s varchar(8000)
    set @s=''
    select @s=@s+' '+rtrim(id) from #t
    set @s=stuff(@s,1,1,'')
    print @sdrop table #t
      

  2.   

    select '1 2 3 4 5 6 7 8'
    select '1 ','2 ','3 ','4 ','5 ','6 ','7 ','8 '
      

  3.   

    有每有别的方法 不要这样的
    select '1 2 3 4 5 6 7 8'
    select '1 ','2 ','3 ','4 ','5 ','6 ','7 ','8 '
    呵呵     
    也不 要 依赖于系统表的  呵呵   不过谢谢大家了 哈 
      

  4.   

    写一个函数就行了:create           function tmp2(@i int)
    returns  varchar(1000)
    as
    begin
    declare @j varchar(1000)
    declare @start int
    set @start=1 
    set @j=''

    while @start<=@i
    begin
    set @j=@j+cast(@start as varchar(10))
    set @start=@start+1
    end
    return @j
    end
      

  5.   

    使用方法举例:
    select dbo.tmp2(20)---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
    1234567891011121314151617181920(所影响的行数为 1 行)
      

  6.   

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

    while @start<=@i
    begin
    set @j=@j+ ' '+cast(@start as varchar)
    set @start=@start+1
    end
    return @j
    end
      

  7.   

    不依赖于系统表的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)t2select @s=@s+' '+rtrim(id)
    from #t
    order by id
    set @s=stuff(@s,1,1,'')
    print @sdrop table #t
      

  8.   

    declare @Tab table(iid int)
    declare @strTotal varchar(8000)
    set @strtotal=''
    declare @inti int
    set @inti=0
    while @inti<100
    begin
    set @inti=@inti+1
    insert into @tab values(@inti)
    endselect @strtotal=@strtotal+cast(iid as varchar)+' ' from @tab
    print @strtotal