今天写了一个游标,但是不知道怎么在这游标里定义一个变量来记录执行时影响条数,并插入两条数据时就跳出循环结束游标。
没有想出辙,实属头疼。附代码如下:
--游标 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