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

来源:互联网 发布:最垃圾的父母 知乎 编辑:程序博客网 时间:2024/06/16 11:41

--1.whiledeclare @t table (id int)declare @i int set @i=1while @i<101begin insert into @t select @i set @i=@i+1endselect * from @t--2. gotodeclare @t1 table (id int)declare @j int set @j=1f:insert into @t1 select @jset @j=@j+1if(@j<101)goto fselect * from @t1--3.identity+临时表select top 100 identity(int,1,1) id into #tmp from syscolumns,sysobjectsselect 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_valuesselect 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;

0 0
原创粉丝点击