怎样解决此问题?

Frewin 2004-05-04 11:03:19
有表Ftable(Fid int)
先要对表进行 Insert
知道Insert 的起始值和结束值,怎样一次插入所有数据
如 起始值=100,结束值=1000
除了用循环
Set @i=100
While @i<=1000
Begin
Insert into Ftable(Fid)
values @I
Set @i=@i+1
End
还有什么办法,因为用这种方法开销比较大,
sqlserver有没有象Oracle 的sequence方法,可以指定递增的数值,因为我知道sqlserver有个identity,
但每次只能以1为递增
...全文
47 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
txlicenhe 2004-05-04
  • 打赏
  • 举报
回复
create table #tmp(id int)
insert #tmp
select id from (
select id=a.id+b.id*10+c.id*100+d.id*1000+1 from
(
select id=0 union all select 1
union all select id=2 union all select 3
union all select id=4 union all select 5
union all select id=6 union all select 7
union all select id=8 union all select 9
) a,(
select id=0 union all select 1
union all select id=2 union all select 3
union all select id=4 union all select 5
union all select id=6 union all select 7
union all select id=8 union all select 9
) b,(
select id=0 union all select 1
union all select id=2 union all select 3
union all select id=4 union all select 5
union all select id=6 union all select 7
union all select id=8 union all select 9
) c,(
select id=0 union all select 1
union all select id=2 union all select 3
union all select id=4 union all select 5
union all select id=6 union all select 7
union all select id=8 union all select 9
) d
) aa
where id between @起始值 and @结束值
--order by id

--select * from #tmp
8992026 2004-05-04
  • 打赏
  • 举报
回复
declare @起始值 int,@结束值 int
select @起始值=100
,@结束值=1000

insert Ftable(FId)
select a+b+c+d+1
from (select 0 as a union all select 1 union all select 2
union all select 3 union all select 4 union all select 5
union all select 6 union all select 7 union all select 8
union all select 9 ) as a,
(select 0 as b union all select 10 union all select 20
union all select 30 union all select 40 union all select 50
union all select 60 union all select 70 union all select 80
union all select 90 ) as b,
(select 0 as c union all select 100 union all select 200
union all select 300 union all select 400 union all select 500
union all select 600 union all select 700 union all select 800
union all select 900 ) as c,
(select 0 as d union all select 1000 union all select 2000
union all select 3000 union all select 4000 union all select 5000
union all select 6000 union all select 7000 union all select 8000
union all select 9000 ) as d
where a+b+c+d+1 between @起始值 and @结束值
zjcxc 元老 2004-05-04
  • 打赏
  • 举报
回复
declare @起始值 int,@结束值 int,@count int
select @起始值=100
,@结束值=1000
,@count=@结束值-@起始值+1

set rowcount @count
select id=identity(int,0,1),a=0 into #t from syscolumns
set @count=@count-@@rowcount
while @count>0
begin
set rowcount @count
insert #t select 0 from syscolumns
set @count=@count-@@rowcount
end

set rowcount 0
insert Ftable(Fid)
select @起始值+id from #t order by id

drop table #t

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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