首页 > 代码库 > 游标实例

游标实例

今天写了一个游标,但是不知道怎么在这游标里定义一个变量来记录执行时影响条数,并插入两条数据时就跳出循环结束游标。

没有想出辙,实属头疼。附代码如下:

--游标declare @userID uniqueidentifier--useriddeclare y_curr cursor forselect top 2 userID from Hope_Users_db.dbo.u_Users order by addTime descopen y_curr --打开游标fetch next from y_curr into @userID ----开始循环游标变量while(@@fetch_status=0)---返回被FETCH  语句执行的最后游标的状态,而不是任何当前被连接打开的游标的状态。begin	   if exists (select * from Q_enterprise where userID = @userID)		  begin		     print @userID		  end		else		  begin			   print @userID			   insert into Q_enterprise			   select distinct u.userID, u.userType,u.SyncCharityId as CharityId,u.userTrueName,u.userLogo,0 as donationAmount,e.enterpriseContactMobile,			   e.enterprisePurpose,0 as enterpriseLongitude,0 as enterpriseLatitude,‘‘ as Infoproportion,0 as sharesum,0 as praisesum,			   0 as Donationsum,0 as Farvritessum,0 as Donationsum30,u.addTime,‘‘ as updatetime			   from Hope_Users_db.dbo.u_Users u			   inner join Hope_Donation_DB.dbo.d_Donation d			   on u.userID=d.userID			   inner join Hope_Users_db.dbo.u_Enterprise e			   on u.userID = e.userID			   where u.userID=@userID		  end      fetch next from y_curr into @userID --开始循环游标变量endclose y_curr--关闭游标deallocate y_curr --释放游标go

  

游标实例