快速生成一个指定条记录数的记录集

 
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值