A cursor with the name Pnr_Actualizables already exists

When you run the stored procedure a second time throws the error
"A cursor with the name Pnr_Actualizables  already exists"
The SP contains OPEN, FETCH, CLOSE, DEALLOCATE


HELP!! :(


CREATE PROCEDURE miprocedimiento @rowprocesables INT OUTPUT
AS


/*Declaracion de variables*/
DECLARE @resv_id int
DECLARE @error int
DECLARE @rowcount int

/*Seteo de variable top, indica cantidad de registros a extraer*/
SET @rowprocesables = 0

/*Declaración de cursos que extraerá los RESV.resv_id candidatos a actualizar*/
DECLARE Pnr_Actualizables CURSOR FOR
SELECT TOP 150000 RES.resv_id 
FROM dbo.HOMOLOGACION_PNR HOM, dbo.RESV RES
WHERE HOM.rvas_cdg_pnr = RES.rvas_cdg_pnr
AND HOM.rvas_fch_reserva = RES.rvas_fch_reserva



/*Captura error de query y cantidad de registros afectados*/
SELECT @error = @@error,@rowcount = @@rowcount

/*Si no hubo errores en la query anterior y existen registros, */
/*entonces se abre el cursor y se inicia la actualizacion      */
IF @error = 0 AND @rowcount > 0 
BEGIN
	OPEN Pnr_Actualizables
		FETCH NEXT FROM Pnr_Actualizables
		INTO @resv_id
		WHILE @@FETCH_STATUS = 0
		BEGIN
			
			/*Actualizamos la tabla RESV para su registro @resv_id, manteniendo el cruce de su informacion */
			/*para el rvas_cdg_pnr y rvas_fch_reserva y que no existe en la tabla procesados*/
			UPDATE dbo.RESV
			SET dbo.RESV.rvas_cdg_pnr = HOM.rvas_cdg_pnr_sabre
			FROM HOMOLOGACION_PNR HOM
			WHERE HOM.rvas_cdg_pnr = dbo.RESV.rvas_cdg_pnr
			AND HOM.rvas_fch_reserva = dbo.RESV.rvas_fch_reserva
			AND dbo.RESV.resv_id = @resv_id

			

			/*Captura error de query y cantidad de registros afectados*/
			SELECT @error = @@error,@rowcount = @@rowcount
			PRINT 'ERROR' + @error
			
			/*Si existe un error o no se actualizaron registros se ingresa el error */
			/*a una tabla de error*/
			IF @error <> 0 OR @rowcount = 0
			BEGIN
				INSERT INTO dbo.RESV_PROC_ERROR
				(RESV_ID,FECHA_ERROR,ERROR,REGISTROS)
				values
				(@resv_id,getdate(),@error,@rowcount)

				
			END
			ELSE
			BEGIN
				/*Se inserta el registro procesado en la tabla dbo.RESV_PROCESADOS*/
				INSERT INTO dbo.RESV_PROCESADOS (resv_id) values (@resv_id)
				COMMIT
				
			END

			FETCH NEXT FROM Pnr_Actualizables
			INTO @resv_id
		END
CLOSE Pnr_Actualizables
DEALLOCATE Pnr_Actualizables
	
	/*Se rescata si hay al menos un registros a procesar*/
	SELECT TOP 1 RES.resv_id
	FROM dbo.HOMOLOGACION_PNR HOM,  dbo.RESV RES
	WHERE HOM.rvas_cdg_pnr = RES.rvas_cdg_pnr
	AND HOM.rvas_fch_reserva = RES.rvas_fch_reserva

	
	
	/*Captura error de query y cantidad de registros afectados*/
	SELECT @error = @@error,@rowcount = @@rowcount
	
	/*Si no existió errores en la query anterior, retornamos la cantidad de registros de acuerdo a su variable @top_fin */
	/*Si se produjo un error, devuelve el valor @rowprocesables seteado en las primeras lineas de este script*/
	IF @error = 0
	SET @rowprocesables = @rowcount
	
END

GO

Open in new window

error-cursor.jpg
jralsenAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anthony PerkinsCommented:
Except when the following is true:
IF @error = 0
    AND @rowcount > 0

The cursor gets created but never destroyed.
0
Anthony PerkinsCommented:
So if you add the following at the end, it should solve the problem:
      IF @error = 0
      SET @rowprocesables = @rowcount
      
    END
ELSE
    DEALLOCATE Pnr_Actualizables

GO



Suerte.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alpesh PatelAssistant ConsultantCommented:
You have to destroy cursor when you exit from SP at any point
0
jralsenAuthor Commented:
Thanks for the clarification
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.