• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1006
  • Last Modified:

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
0
jralsen
Asked:
jralsen
  • 2
1 Solution
 
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
 
Alpesh PatelAssistant ConsultantCommented:
You have to destroy cursor when you exit from SP at any point
0
 
jralsenAuthor Commented:
Thanks for the clarification
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now