SqlServer临时表及While循环

本文详细介绍了如何使用SQL语句创建临时表,并通过一系列操作(如插入数据、遍历表和复制数据)将成员信息表中的数据迁移到新的登录信息表中。操作包括定义临时表、向其插入数据以及遍历表并将数据复制到目标表。

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

1.定义临时表@temptb

DECLARE @temptb TABLE
(
[id] int indentity(1,1),
UserName nvarchar(50),
Password varchar(20)
)


2.向临时表中添加数据

将memberInfo表中的UserName和Password插入到临时表@temptb中
insert into @temptb(UserName,Password) select UserName,Password from memberInfo


3. 依次遍历临时表@temptb中的数据,将其添加的到新表中

Declare @currentIndex int
Declare @totalRows int
Declare @userName nvarchar(50)
Declare @password varchar(20)
select @currentIndex=1
select @totalRows=count(1) from @temptb

while(@currentIndex<=@totalRows)
begin
select @userName=null
select @password=null
select @userName=userName from @temptb where id=@currentIndex
select @password=password from @temptb where id=@currentIndex
insert into loginInfo(userName,password)
values(@userName,@password)
select @currentIndex=@currentIndex+1;
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值