Cómo comprobar si existe el cursor (abrir estado)


¿Cómo puedo comprobar si un cursor está abierto o no? Porque muchas veces me encuentro con el error 'Cursor ya existe". Por favor, hágame saber cómo puedo comprobar si un cursor ya está en estado abierto.

De hecho lo he cerrado así como Desasignado al final (CLOSE ppm_cursor; DEALLOCATE ppm_cursor;) Pero todavía estoy recibiendo el mismo error cuál podría ser la razón.

Author: Maddy, 2011-09-15

5 answers

Puede usar la función CURSOR_STATUS para determinar su estado.

IF CURSOR_STATUS('global','myCursor')>=-1
BEGIN
 DEALLOCATE myCursor
END
 73
Author: Gary W,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-09-15 12:23:12

Cerrar el cursor, si está vacía entonces desasignar:

IF (SELECT CURSOR_STATUS('global','myCursor')) >= -1
 BEGIN
  IF (SELECT CURSOR_STATUS('global','myCursor')) > -1
   BEGIN
    CLOSE myCursor
   END
 DEALLOCATE myCursor
END
 29
Author: Prateek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-01-06 06:53:50

Solo un pequeño cambio a lo que Gary W mencionó, agregando 'SELECT':

IF (SELECT CURSOR_STATUS('global','myCursor')) >= -1
BEGIN
 DEALLOCATE myCursor
END

Http://social.msdn.microsoft.com/Forums/en/sqlgetstarted/thread/eb268010-75fd-4c04-9fe8-0bc33ccf9357

 4
Author: Muhammad Omar ElShourbagy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-03 19:30:24

Rara vez empleo cursores, pero acabo de descubrir otro elemento que puede morderte aquí, el alcance del nombre del cursor.

Si la base de datos CURSOR_DEFAULT es global, obtendrá el error "cursor ya existe" si declara un cursor en un procedimiento almacenado con un nombre particular (por ejemplo, "cur"), y mientras ese cursor está abierto, llama a otro procedimiento almacenado que declara y abre un cursor con el mismo nombre (por ejemplo, "cur"). El error se producirá en el procedimiento almacenado anidado cuando se intentos de abrir "cur".

Ejecute este bit de sql para ver su CURSOR_DEFAULT:

select is_local_cursor_default from sys.databases where name = '[your database name]'

Si este valor es " 0 " entonces importa cómo nombre su cursor anidado!

 0
Author: Tom Regan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-08-15 14:48:58

Esto me sucedió cuando un procedimiento almacenado que se ejecuta en SSMS encontró un error durante el bucle, mientras que el cursor estaba en uso para iterar sobre registros y antes de que se cerrara. Para arreglarlo, agregué código adicional en el bloque CATCH para cerrar el cursor si todavía está abierto(usando CURSOR_STATUS como sugieren otras respuestas aquí).

 0
Author: Aaron,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-11 19:34:40