今天寫了一個游標,但是不知道怎么在這游標里定義一個變量來記錄執行時影響條數,并插入兩條數據時就跳出循環結束游標。
沒有想出轍,實屬頭疼。附代碼如下:
--游標
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
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

