Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get "A cursor with the name already exists"?

I have this trigger:

CREATE TRIGGER CHECKINGMAXQTYDAYSVACANCY     ON TDINCI AFTER INSERT  AS     DECLARE         @incidentCode int,         @dateStart datetime,         @dateEnd datetime,         @daysAccumulated int,         @maxDaysAvailable int      set @daysAccumulated = 0;      select @incidentCode = CO_INCI from inserted;     select @maxDaysAvailable = IN_DIAS_GANA from TCINCI         where CO_INCI = @incidentCode;      declare detailsCursor CURSOR FOR         select FE_INIC, FE_FINA from TDINCI         where CO_INCI = @incidentCode;      open detailsCursor;      if CURSOR_STATUS('variable', 'detailsCursor') >= 0     begin         fetch next from detailsCursor             into @dateStart, @dateEnd;          while @@FETCH_STATUS = 0         begin             set @daysAccumulated = @daysAccumulated + (DATEDIFF(DAY, @dateStart, @dateEnd) + 1);              fetch next from detailsCursor             into @dateStart, @dateEnd;         end         close detailsCursor;         deallocate detailsCursor;     end     IF(@maxDaysAvailable > @daysAccumulated)     BEGIN         RAISERROR ('No se pueden ingresar mas dias de los programados en la cabecera de incidencias.', 16, 1);         ROLLBACK TRANSACTION;         RETURN      END GO 

When I do a Insert to the table TDINCI

INSERT INTO TDINCI  VALUES (1, '20150101', '20150115', '2015-2015') 

I get an error:

A cursor with the name 'detailsCursor' already exists.

I open

open detailsCursor; 

and close the cursor.

close detailsCursor; deallocate detailsCursor; 

Maybe there is something with the scope of cursor that I don't manage? Thanks in advance.

like image 842
Erick Asto Oblitas Avatar asked Feb 28 '15 19:02

Erick Asto Oblitas


People also ask

How do I know if my cursor exists?

If the database CURSOR_DEFAULT is global, you will get the "cursor already exists" error if you declare a cursor in a stored procedure with a particular name (eg "cur"), and while that cursor is open you call another stored procedure which declares and opens a cursor with the same name (eg "cur").

How do I drop a cursor in SQL?

DEALLOCATE removes the association between a cursor and the cursor name or cursor variable. If a name or variable is the last one referencing the cursor, the cursor is deallocated and any resources used by the cursor are freed. Scroll locks used to protect the isolation of fetches are freed at DEALLOCATE .

What are cursors in SQL?

A cursor in SQL is a temporary work area created in system memory when a SQL statement is executed. A SQL cursor is a set of rows together with a pointer that identifies a current row. It is a database object to retrieve data from a result set one row at a time.

What is the purpose of cursor to list them all?

Answer. Answer: The Cursor Tools provide easy access to a number of closely related sub-features. They are located at the top-middle part of the user interface.


1 Answers

You are using global cursor that will be defined each time you are calling this procedure and give you the same error.

Define a local cursor. Just put the keyword LOCAL after CURSOR:

declare detailsCursor CURSOR LOCAL FOR ... 
like image 52
Ahmad Avatar answered Sep 23 '22 23:09

Ahmad