SQLServer------如何快速插入几万条测试数据

时间:2021-08-31 08:30:36

方法一:

1.建表

if OBJECT_ID('test') is not null  drop table test
go
create table test
(id int identity(,),vid int ,v varchar(),
constraint pk_test_id primary key (id))
go

2.第一次插入数据

insert into test(vid,v)
select ,REPLICATE('a',) union all
select ,REPLICATE('b',) union all
select ,REPLICATE('c',) union all
select ,REPLICATE('d',) union all
select ,REPLICATE('e',) union all
select ,REPLICATE('f',) union all
select ,REPLICATE('g',) union all
select ,REPLICATE('h',) union all
select ,REPLICATE('i',) union all
select ,REPLICATE('j',)
go

3.第二次插入数据

begin tran  
    insert into test(vid,v)  
    select vid,v  
    from test  
commit  tran
go  

方法二:

1.建表

学生表:
CREATE TABLE [dbo].[Student](
[StudentID] [int] IDENTITY(,) NOT NULL, --主键
[Number] [varchar]() NULL, --学号
[Name] [nchar]() NULL, --学生姓名
[ClassID] [int] NOT NULL --学生所在班级ID
)

2.插入数据

插入学生数据:
declare @count int =;
while @count <
begin
insert into Student select @count,'学生'+ convert(varchar,@count,),cast(ceiling(rand() * ) as int)
set @count = @count + ;
end