这个需求真奇怪... 可以考虑先把数据存在临时表中,然后用一个循环,每秒添加一次,直到全部添加完,程序如: declare @id int,@dt datetime set @id=1 --读当前时间-1秒的时间 set @dt=dateadd(ss,-1,getdate()) --如果@id>临时表中最大ID while @id<(select count(*) from #t begin --如果与设置时间差大于等于1秒,则添加 if datediff(ss,@dt,getdate())>=1 begin insert into tb select top 10 from #t where id>@id --设置最大添加的ID数,在此假设每次添加10条 set @id=@id+10 --重设时间 set @dt=getdate() end end
可以考虑先把数据存在临时表中,然后用一个循环,每秒添加一次,直到全部添加完,程序如:
declare @id int,@dt datetime
set @id=1
--读当前时间-1秒的时间
set @dt=dateadd(ss,-1,getdate())
--如果@id>临时表中最大ID
while @id<(select count(*) from #t
begin
--如果与设置时间差大于等于1秒,则添加
if datediff(ss,@dt,getdate())>=1
begin
insert into tb select top 10 from #t where id>@id
--设置最大添加的ID数,在此假设每次添加10条
set @id=@id+10
--重设时间
set @dt=getdate()
end
end
这个需求有问题,有的时候设定每秒10w条,数据库不一定能够完成的了。
数据库不是说永远就那么稳定的,这个数字根据什么来设置的。
如果每秒可以处理20w条,我们又为什么要1w条1s的来处理呢?
添加操作会增加一些时间,用户看到效果也是一部分一部分的出现?
create table tb(id int,dt datetime)
declare @id int,@dt datetime
set @id=1
--读当前时间-1秒的时间
set @dt=dateadd(ss,-1,getdate())
--如果@id>临时表中最大ID
while @id<(select count(*) from #t)
begin
--如果与设置时间差大于等于1秒,则添加
if datediff(ss,@dt,getdate())>=1
begin
insert into tb select top 10 id,getdate()dt from #t where id>=@id order by id
--设置最大添加的ID数,在此假设每次添加10条
set @id=@id+10
--重设时间
set @dt=getdate()
end
end
select * from tb
/*
id dt
----------- -----------------------
1 2011-06-14 20:34:03.700
2 2011-06-14 20:34:03.700
3 2011-06-14 20:34:03.700
4 2011-06-14 20:34:03.700
5 2011-06-14 20:34:03.700
6 2011-06-14 20:34:03.700
7 2011-06-14 20:34:03.700
8 2011-06-14 20:34:03.700
9 2011-06-14 20:34:03.700
10 2011-06-14 20:34:03.700
11 2011-06-14 20:34:04.013
12 2011-06-14 20:34:04.013
13 2011-06-14 20:34:04.013
14 2011-06-14 20:34:04.013
15 2011-06-14 20:34:04.013
16 2011-06-14 20:34:04.013
17 2011-06-14 20:34:04.013
18 2011-06-14 20:34:04.013
19 2011-06-14 20:34:04.013
20 2011-06-14 20:34:04.013
21 2011-06-14 20:34:05.013
22 2011-06-14 20:34:05.013
23 2011-06-14 20:34:05.013
24 2011-06-14 20:34:05.013
25 2011-06-14 20:34:05.013
26 2011-06-14 20:34:05.013
27 2011-06-14 20:34:05.013
28 2011-06-14 20:34:05.013
29 2011-06-14 20:34:05.013
30 2011-06-14 20:34:05.013
31 2011-06-14 20:34:06.013
32 2011-06-14 20:34:06.013
33 2011-06-14 20:34:06.013
34 2011-06-14 20:34:06.013
35 2011-06-14 20:34:06.013
36 2011-06-14 20:34:06.013
37 2011-06-14 20:34:06.013
38 2011-06-14 20:34:06.013
39 2011-06-14 20:34:06.013
40 2011-06-14 20:34:06.013
41 2011-06-14 20:34:07.013
42 2011-06-14 20:34:07.013
43 2011-06-14 20:34:07.013
44 2011-06-14 20:34:07.013
45 2011-06-14 20:34:07.013
46 2011-06-14 20:34:07.013
47 2011-06-14 20:34:07.013
48 2011-06-14 20:34:07.013
49 2011-06-14 20:34:07.013(49 行受影响)*/
go
drop table tb,#t