码迷,mamicode.com
首页 > 其他好文 > 详细

游标实例

时间:2014-12-30 18:28:56      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

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

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

--游标
declare @userID uniqueidentifier--userid
declare y_curr cursor for
select top 2 userID from Hope_Users_db.dbo.u_Users order by addTime desc

open 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 --开始循环游标变量
end
close y_curr--关闭游标
deallocate y_curr --释放游标
go

  

游标实例

标签:

原文地址:http://www.cnblogs.com/Wbely/p/4193976.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!