SQL Server 批量插入

使用场景

在项目中,涉及到数据库表变更时,可能会需要将一个或多个表中的数据迁移到另一个表中。

这时用sql去执行会很方便!

sql语句

//SQL批量插入

create table #ttableName(id int identity(1,1),customerid int,grade int,popularity int)

declare @n int,@rows int
declare @e int, @g int,@p int
insert #ttableName(customerid,grade,popularity) select Id,Grade,Popularity from Customer where Grade>0 or Popularity>0

select @rows =@@rowcount 
set @n=1 
while @n<=@rows
begin
	select @e=customerid, @g=grade,@p=popularity from #ttableName where id=@n
	
insert into GenericAttribute(EntityId,KeyGroup,[Key],[Value]) values(@e,'Customer','LiveLevel',@g)
insert into GenericAttribute(EntityId,KeyGroup,[Key],[Value]) values(@e,'Customer','LivePopularity',@p)
	select @n=@n+1
end
drop table #ttableName
原文地址:https://www.cnblogs.com/zhubangchao/p/7741526.html