IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]---------------生成1-100数字的方法(包括SQL SERVER和DB2)----------

    fredrickhu发表于 2014-12-31 16:34:10
    love 0

    
    
    --1.while
    declare @t table (id int)
    declare @i int set @i=1
    while @i<101
    begin
     insert into @t select @i
     set @i=@i+1
    end
    select * from @t
    
    --2. goto
    declare @t1 table (id int)
    declare @j int set @j=1
    f:
    insert into @t1 select @j
    set @j=@j+1
    if(@j<101)
    goto f
    select * from @t1
    
    --3.identity+临时表
    select top 100 identity(int,1,1) id into #tmp from syscolumns,sysobjects
    
    
    select id from #tmp;
    
    drop table #tmp
    
    --4.row_number()
    select top 100 row_number() over(order by (select 1)) from syscolumns,sysobjects
    
    --5.系统辅助表master..spt_values
    select number from master..spt_values where type='p' and number between 1 and 100
    
    --6.CTE递归
    ;with cte as
    (
    select 1 id union all select cte  from t where id<100
    )
    select * from t 
    
    
    --7.用0-9做序列
    ;with f as
    (
     select 0 id 
     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
    )
    select a.id+b.id+1 id from m a ,(select id*10 id from m) b
    
    
    
    
    附上DB2的方法:
    
    
    select row_number()over(order by (values 1)) as a from sysibm.sysdummy1,sysibm.syscoulmns fetch first 100 rows only;
    
    



沪ICP备19023445号-2号
友情链接