sql产生一列连续数字

aimeast 2010-02-03 08:36:53
如题。输入一个范围,生成这个范围内连续的数字。
例如输入 100 123
生成
100
101
102
...
...
123。
...全文
1170 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
jasper 2010-02-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wuyq11 的回复:]
declare @i as int
set @i=1
declare @s as varchar(20)
while(@i <124)
begin
  set @s=replace(str(@i,3),' ','0')
  print @s
  set @i=@i+1
end
[/Quote]
为什么要转化为字符串呢?
jasper 2010-02-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bancxc 的回复:]
SQL code呵呵select (100+number )AS Nfrom master..spt_valueswhere type='p'and (100+number)between100and123/*N
-----------
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

(所影响的行数为 24 行)*/
[/Quote]

这个表中的number是不确定的
aimeast 2010-02-04
  • 打赏
  • 举报
回复
[Quote=引用 31 楼 jasper 的回复:]
引用 5 楼 wuyq11 的回复:
declare @i as int
set @i=1
declare @s as varchar(20)
while(@i <124)
begin
  set @s=replace(str(@i,3),' ','0')
  print @s
  set @i=@i+1
end

为什么要转化为字符串呢?
[/Quote]
前面补0
aimeast 2010-02-03
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 bancxc 的回复:]
SQL codeselectmax(n),min(n)from #where nbetween100and10000000--
----------- -----------10000000100

(所影响的行数为1 行)
[/Quote]在我这运行就得不到这个结果。应该是我的数据库里面的东西比较少。看来此方法并不通用。
bancxc 2010-02-03
  • 打赏
  • 举报
回复
select max(n),min(n) from # where n between 100 and 10000000 
--
----------- -----------
10000000 100

(所影响的行数为 1 行)
bancxc 2010-02-03
  • 打赏
  • 举报
回复
不清楚了
试了下 我的好像没错
aimeast 2010-02-03
  • 打赏
  • 举报
回复
打错了
drop table #
select identity(int,1,1) as n into # from syscolumns a,syscolumns b

select max(n),min(n) from # where n between 100 and 10000000
aimeast 2010-02-03
  • 打赏
  • 举报
回复
drop table #
select identity(int,1,1) as n into # from syscolumns a,syscolumns b

select * from # where n between 100 and 10000000 order by n

结果:
178084 100

并不是10000000
bancxc 2010-02-03
  • 打赏
  • 举报
回复
可以用between 100 and 100000000[Quote=引用 21 楼 aimeast 的回复:]
引用 20 楼 whb147 的回复:
用 Top (123-100) 控制精度
select top 23 identity(int,1,1) as n into # from syscolumns a,syscolumns b

然后用 起始数 100

select 100+n from #

100-10000000这样的结果是得不到的。
[/Quote]
aimeast 2010-02-03
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 whb147 的回复:]
用 Top (123-100) 控制精度
select top 23 identity(int,1,1) as n into # from syscolumns a,syscolumns b

然后用 起始数 100

select 100+n from #
[/Quote]
100-10000000这样的结果是得不到的。
whb147 2010-02-03
  • 打赏
  • 举报
回复
用 Top (123-100) 控制精度
select top 23 identity(int,1,1) as n into # from syscolumns a,syscolumns b

然后用 起始数 100

select 100+n from #
whb147 2010-02-03
  • 打赏
  • 举报
回复
aimeast 2010-02-03
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 bancxc 的回复:]
SQL codeselectidentity(int,1,1)as ninto #from syscolumns a,syscolumns bselect*from #
[/Quote]怎么控制一个精确范围?
bancxc 2010-02-03
  • 打赏
  • 举报
回复
select identity(int,1,1) as n into # from syscolumns a,syscolumns b

select * from #
aimeast 2010-02-03
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 bancxc 的回复:]
SQL codecreatetable tb(nint)godeclare@iintdeclare@nintselect@i=1select@n=@iinsertinto tbvalues(1)while@n*2<=2000begininsertinto tb(n)select@n+nfrom tbselect@n=@n*2endinsertinto tb(n)select@n+nfrom tbw?-
[/Quote]
好想法,插入一半,复制另一半!
bancxc 2010-02-03
  • 打赏
  • 举报
回复
create table tb(n int)
go

declare @i int
declare @n int

select @i=1
select @n=@i

insert into tb values(1)

while @n*2<=2000
begin
insert into tb(n)
select @n+n from tb
select @n=@n*2
end

insert into tb(n)
select @n+n from tb where @n+n<=2000

select * from tb

--drop table tb
这个稍微好点 哈哈
aimeast 2010-02-03
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 bancxc 的回复:]
有引用 10 楼 aimeast 的回复:
有没有比这个更快的方法了?SQL codedeclare@sintdeclare@eintdeclare@qvarchar(150)set@s=10set@e=20000select0as'a'into #t1while@s <=@ebegininsert #t1 (a)values (@s)set@s=@s+1endselect'XXA'+cast(aasvarchar),'aa'from #t1?-

[/Quote]我想知道方法。谢谢。
bancxc 2010-02-03
  • 打赏
  • 举报
回复
有[Quote=引用 10 楼 aimeast 的回复:]
有没有比这个更快的方法了?SQL codedeclare@sintdeclare@eintdeclare@qvarchar(150)set@s=10set@e=20000select0as'a'into #t1while@s<=@ebegininsert #t1 (a)values (@s)set@s=@s+1endselect'XXA'+cast(aasvarchar),'aa'from #t1?-
[/Quote]
aimeast 2010-02-03
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 bancxc 的回复:]
看你的需求了 int类型还有个最大最小值呢引用 8 楼 aimeast 的回复:
引用 6 楼 bancxc 的回复:
如果有必要的话你就自己建一个辅助表
里边有足够多的数字
你可以利用它生成你想要的结果

呵呵,早想着这样了。这个“足够多”就太不好把握了。

继续讨论。

[/Quote]
就是不知道范围是多少。不过肯定是在int之内的。总不能把所有的int都存进去吧。
wuyq11 2010-02-03
  • 打赏
  • 举报
回复
插入数据到临时表
加载更多回复(10)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧