执行exec后怎么跳出while循环了?

davytao1018 2005-12-15 09:55:51
我用下面的过程怎么只插入了一行数据,print @i也没有打印出来.


--创建存储过程
create proc aa
@degreeID as int,
@sourceDBName as char(20),
@targetDBName as char(20)
as
begin
declare @i int,
@count int,
@sql varchar(400)
--插入默认角色至目标库
exec('update '+@targetDBName+'..ParameterTable set ParameterValue=N'''+@degreeID+''' where ParameterName=''DefaultDegreeID''')

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[temp1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[temp1]


--插入临时表


exec('select * into temp1 from '+@sourceDBName+'..UserInfoTable where userType=4')


alter table temp1 add nid int IDENTITY(1,1)

select * from temp1

--select @count=max(nid) from temp1

set @count=10

--导出用户(非系统默认的用户)
set @i=1
while @i<=@count
begin
set @sql='insert into '+@targetDBName+'..UserInfoTable
select userid,usertype,username,password,sex,birthday,createdate,
cardid,companyid,education,trade,province,city,address,phone,email,
postcode,personalResume,HomePage,networkstatus,linkstatus,deleteflag
from temp1 where nid='+rtrim(@i)
print @sql
exec(@sql)
set @i=@i+1
print @i
end

drop table temp1

end

go

--调用proc
exec aa 4,chinaschool_rs40,chinaschool_rs50_000

--删除存储过程
go
drop proc aa

...全文
250 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
hnhn 2006-03-15
  • 打赏
  • 举报
回复
mark

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧