SQL中批量导入数据 union all 相当于一个事务

--生成测试表Ta   
if not object_id('Ta') is null  
    drop table Ta   
go   
create table Ta(ID int Primary key,Col1 int,Col2 nvarchar(10))   
insert Ta    
select 1,101,'A' union all  
select 2,102,'B' union all  
select 3,103,'C'   

go 

union all 相当于一个事务

if object_id('T2') is not null
drop table T2
go
create table T2(ID int primary key,Col1 int,Col2 nvarchar(20))
insert T2 select 1,201,'X'
insert T2 select 2,202,'Y'
insert T2 select 3,203,'Z'


insert 相当于三个事务


如果加上CTE的话效果也不错!

eg:


GO
declare @end int 
set @end =100000
while(0<@end)
begin
with cte_temp (Col1,Col2)
as
(
select @end,char(@end%255)		 union all 
select @end-1,char((@end-1)%255) union all 
select @end-2,char((@end-2)%255) union all 
select @end-3,char((@end-3)%255) union all 
select @end-4,char((@end-4)%255) union all 
select @end-5,char((@end-5)%255) union all 
select @end-6,char((@end-6)%255) union all 
select @end-7,char((@end-7)%255) union all 
select @end-8,char((@end-8)%255) union all 
select @end-9,char((@end-9)%255) union all 
select @end-10,char((@end-10)%255) union all 
select @end-11,char((@end-11)%255) union all 
select @end-12,char((@end-12)%255) union all 
select @end-13,char((@end-13)%255) union all 
select @end-9,char((@end-9)%255) union all 
select @end-10,char((@end-10)%255) union all 
select @end-11,char((@end-11)%255) union all 
select @end-12,char((@end-12)%255) union all 
select @end-13,char((@end-13)%255) union all 
select @end-14,char((@end-12)%255) 
)
insert Ta select * from cte_temp
set @end=@end-20;
end
GO


你可能感兴趣的:(SQL中批量导入数据 union all 相当于一个事务)