已知一个变量,如:5,怎样用一句SQL,生成如下升序多条的纪录。
====
1
2
3
4
5
====

解决方案 »

  1.   

    5是变量的值?
    描述不清楚升序 order by 字段
      

  2.   

    那就先创建一个表,再用一个for循环,添加n各这样的纪录不就可以了吗
      

  3.   

    declare @intCount as int
    set @intCount = 5
    declare @vchSqlCmd varchar(800)
    set @vchSqlCmd = 'select top ' + cast(@intCount as varchar(20))
    set @vchSqlCmd = @vchSqlCmd + ' from yourtable'
    exec(@vchSqlCmd)
      

  4.   

    我也想过一个最简单的方法:select 1 union select 2 union select 3 union select 4 union select 5,但如果是10000行的话,那这个SQL也太长了。
      

  5.   

    declare @b table(id int)
    declare @a intset @a=5
    while @a>=1
    begin
    insert into @b select @a
    set @a=@a-1
    end
    select * from @b order by id