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
上一篇:HDU 6199 2017沈阳网络赛 DP


下一篇:2014中秋节,用java为QQ游戏美女找茬写辅助