Before I get any militant cursor-bashing, let me say that I'm trying to use nested cursors to do something I only need to do once, but if I ran the operative stored procedure once for each user and agency I have to do it a few hundred times. I thought a nested cursor in this case would save me some work, however, when I run this script it goes through the outer cursor only once, while the inner works just fine for that run. In the test case, the outer cursor set consists of two rows, and the inner one has about fifty. It goes through the first row of the outer cursor, and all fifty of the inner, but then it's done. As you can see, I am saving off the result of the outer fetch (the @@fetch_status) so it doesn't interfere with the inner cursor.
I can't see what the problem is (obviously). Can anyone see what I can't?
declare @fetch_user int
declare @fetch_agency int
declare user_cursor cursor for
select upn from #users
open user_cursor
fetch next from user_cursor into @upn
select @fetch_user = @@fetch_status
while @fetch_user = 0
begin
declare agency_cursor cursor for
select agency, subagency from agency_system where system_id = 1
open agency_cursor
fetch next from agency_cursor into @agency, @subagency
select @fetch_agency = @@fetch_status
while @fetch_agency = 0
begin
select @upn, @agency, @subagency
EXEC AddUserToAgencyInRole
@upn
, @agency
, @subagency
, @system_id
, @role_id
, @response output
fetch next from agency_cursor into @agency, @subagency
select @fetch_agency = @@fetch_status
end
close agency_cursor
deallocate agency_cursor
fetch next from user_cursor into @upn
select @fetch_user = @@fetch_status
end
close user_cursor
deallocate user_cursor