declare @tb table(fid int identity,fc char(1))
declare @n int
set @n=1234567
declare @i int
--一条条插入的方式 大约24.5秒
print convert(varchar(30),getdate(),121)
set @i=0
set nocount on
while @i<@n
begin
insert @tb (fc) values ('0')
set @i=@i+1
end
set nocount off
print convert(varchar(30),getdate(),121)
select count(*) from @tb
delete @tb
--批量插入的方式 大约5秒
print convert(varchar(30),getdate(),121)
insert @tb (fc) values ('1')
set @i=1
set nocount on
while @i<@n
begin
if @i+@i<=@n
begin --翻倍
insert @tb (fc) select fc from @tb
set @i=@i+@i
print @i
end
else
begin --翻倍的记录数就要超过了
--insert @tb (fc) values ('0')
print convert(varchar(30),getdate(),121)
--insert @tb (fc) select top @n-@i fc from @tb --可惜只有sql2005开始才支持
insert @tb (fc) select fc from @tb where fid<@n-@i+(select min(fid) from @tb)
print @@rowcount
print convert(varchar(30),getdate(),121)
set @i=@i+@i
end
end
set nocount off
print convert(varchar(30),getdate(),121)
select count(*) from @tb
——从实际可以看出:批量插入的效率比一条条的插入要高几倍(1234567条:5s : 24s)
作用呢?
如果需要列出一个从某天开始的连续n天的日期记录。。。。。。
select '2000-01-01'+fid from funMakeRecord(1000) order by fid