begin
insert into Employee(EmployeeNo,EmployeeName,CreateUser,CreateDate)
values(@Index,'员工'+ cast(@Index as CHAR(5)),'system',GETDATE())
set @Index = @Index+1
end
select DATEDIFF(MS,@Timer,GETDATE()) as '执行时间(毫秒)'
set statistics time off;
执行普通循环插入10000条数据,大概需要1200多毫秒,结果如图所示
2、事务循环插入
begin tran;
set statistics time on;
declare @Index int;
declare @Timer Datetime;
set @Index=1;
set @Timer = GETDATE();
while @Index <=10000
begin
insert into Employee(EmployeeNo,EmployeeName,CreateUser,CreateDate)
values(@Index,'员工'+ cast(@Index as CHAR(5)),'system',GETDATE())
set @Index = @Index+1
end
select DATEDIFF(MS,@Timer,GETDATE()) as '执行时间(毫秒)'