快速生成导入亿级测试数据到sqlserver

如果采用insert into 循环一条一条插入速度比较慢

可以先将数据插入临时表,然后在临时表数据量到达批量插入的行数时执行例如:目标表 (col1,col2,col3)

SELECT col1,col2,col3  into #temp FROM 目标表 WHERE 1=2 --根据目标表结构复制一个临时表
declare @n int
set @n=1
while @n<=100000000
begin
insert  #temp(col1,col2,col3)
values ( NEWID(),
'48'+right( '00000000'+cast(@n as nvarchar(10)),8),
CAST(ABS(CHECKSUM(newID()))%CAST( cast('2003-10-01' as datetime)-cast('1968-03-02' as datetime) AS BIGINT)+CAST(cast ('1968-03-02' as datetime) AS BIGINT) AS DATETIME)+RAND() --随机时间
)
 if @n%100001=100000  --每10万条记录执行一次批量插入,可以自定义
 begin 
 insert 目标表(col1,col2,col3) select * from #temp
 truncate table #temp
 end 
set @n=@n+1
end
--drop table #temp
原文地址:https://www.cnblogs.com/vitas/p/7696412.html