游标与临时表 while循环 pk

结论: 游标要快一个数量级。等同于sql.read方式,但很耗资源

create
proc sp_testCursor

as
declare @userChName nvarchar(500),
@userEnName nvarchar(500)

declare cur cursor for
SELECT userChName, userEnName
FROM testA
open cur
fetch next from cur into @userChName,@userEnName --先去第一条数据
while @userChName is not null and @userEnName is not null and @@fetch_status=0 --结束的条件
begin
insert into testB(userChName, userEnName)values(@userChName,@userEnName )
fetch next from cur into @userChName,@userEnName --取下一条数据,相当于C#中的i++
end
close cur --关闭游标
deallocate cur
go


 ------------

 


drop   table   #T
select   identity(int,1,1)   as   id,*   into   #T   from   BIZ_COMPANy_RECALL_ACCOUNT  where memberpkid<10000    --第一条搜索语句
declare   @i   int
declare   @total   int
declare   @EmpCo   varchar(10)
select   @total   =   count(*)   from   #T
set   @i   =   1
while   @i   <=   @total  
begin
    select   @EmpCo=memberpkid   from   #T   where   id   =   @i                           --第二条搜索语句,而且是循环执行的。  
print   @EmpCo
    set   @i   =   @i   +   1
end

drop   table   #T
 

 

 

posted on 2010-12-30 16:03  http  阅读(329)  评论(1编辑  收藏  举报

导航