SQL Server 批量插入

本文介绍了一种使用SQL语句进行数据库表间数据迁移的方法。通过创建临时表并利用循环逐条插入的方式,实现了从源表到目标表的数据迁移,特别适用于需要迁移包含等级和人气字段的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用场景

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

这时用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值