SQL Server中快速生成大量记录的SQL脚本

来源:这里教程网 时间:2026-03-02 09:58:51 作者:

下面脚本快速生成1百万条记录的表:

set nocount on
use law
go

if object_id('dbo.nums') is not null
drop table dbo.nums
go

create table dbo.nums(n int not null primary key) on newFG
declare @max as int, @rc as int;
set @max=1000000
set @rc=1

insert into nums values(1)
while @rc*2<=@max
begin
insert into dbo.nums select n+@rc from dbo.nums
set @rc=@rc*2
end

insert into dbo.nums
select n+@rc from dbo.nums where n+@rc<=@max

select count(*) from dbo.nums

[@more@]

相关推荐