We help IT Professionals succeed at work.
Get Started

A cursor with the name Pnr_Actualizables already exists

jralsen
jralsen asked
on
1,064 Views
Last Modified: 2012-05-11
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
Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2012
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE