日期:2014-05-18  浏览次数:20737 次

看看各位大神们用多少种方法生成数字序列1到100?
各位大神们请展示下你们生成数字序列1到100的方法。

先放一个:
with t
as
(select 1 as dt
 union all
 select dt+1 from t
 where dt+1<=100)
select dt from t option(maxrecursion 0)

------解决方案--------------------
SQL code
 declare @i int
 set @i=1
 xxoo:
 print @i
 set @i=@i+1
 if @i<101
 goto xxoo

------解决方案--------------------
select number from master..spt_values where type='p' and number between 1 and 100
------解决方案--------------------
SQL code
select number from master..spt_values  b where b.type='p' and number between 1 and 100

/*
number      
----------- 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

(所影响的行数为 100 行)

*/

------解决方案--------------------
SQL code
select top 100000 row_number()over(order by rand()) from syscolumns,sysobjects,sysindexes

------解决方案--------------------
SQL code

--生成1-100的6种方法:

--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.if+goto(小爱已经给出了)
declare @t1 table (id int)
declare @j int set @j=1
maco:
insert into @t1 select @j
set @j=@j+1
if(@j<101)
goto maco
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()(Vidor已经给出了)
select top 100 row_number() over(order by (select 1)) from syscolumns,sysobjects

--5.系统辅助表master..spt_values(SQL777和dawugui已经给出了)
select number from master..spt_values where type='p' and number between 1 and 100

--6.CTE递归(楼主已经给出了)
;with t as
(
select 1 id union all select id+1 from t where id<100
)
select * from t 

--楼下继续补充

------解决方案--------------------
SQL code
go
create table tmp(ID int identity)
go
insert tmp default values
go 100
go
select * from tmp
drop table tmp