Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

"Cursor already exists" error

We need to get a script done for our client (their developers, DBAs in the dept quit within a few weeks and no one bothered to ask them to turn over their code and scripts!)

My manager came up with this cursor loop but I get an error (I know cursors are not recommended but we have to get this done for now). Not sure where he got the script but I think the placement of closing the cursors is off somewhere..

error: A cursor with the name 'activationCursor' already exists.

Declare @rYear int
Declare @rMonth int
Declare reportCursor Cursor
	for Select year, month from #ReportDateTable
Open reportCursor;
Fetch Next From reportCursor into @rYear, @rMonth
while @@FETCH_STATUS = 0
	Begin
		Declare @aYear int
		Declare @aMonth int
		Declare activationCursor Cursor
			for Select year, month from #ActivateDateTable
			Open activationCursor
			Fetch Next From activationCursor into @aYear, @aMonth
			while @@FETCH_STATUS = 0
				Begin
					-- Do your stuff and insert into @Usage
                                      INSERT INTO #usage (reportdate,activationdate,usage)
				         SELECT
				                .....
					Fetch Next from activationCursor into @aYear, @aMonth
				End
			Close activationCursor
		 --Insert into Usage table
		 
		 Fetch Next from reportCursor into @rYear, @rMonth	
	End
	Close reportCursor

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Camillia

ASKER

Thanks, let me try.