SqlServer临时表及While循环

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

你可能感兴趣的:(数据库实用,SQL综合,数据库)