Ayuda Cursores

17/10/2005 - 14:25 por RCM | Informe spam
Hola a todos.

Quiero utilizar un procedimiento almacenado para borrar tablas
temporales del servidor. Este es el código:

declare BorrarTablasTemporales cursor for

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.tables
WHERE TABLE_NAME like 'tmp_dist_%'

declare @TablaTemp nvarchar(50)

open BorrarTablasTemporales

fetch first from BorrarTablasTemporales
into @TablaTemp

while @@fetch_status = 0
begin

drop table @TablaTemp

fetch next from BorrarTablasTemporales
into @TablaTemp

end

deallocate BorrarTablasTemporales


El error que recibo es este:

Servidor: mensaje 170, nivel 15, estado 1, línea 26
Línea 26: sintaxis incorrecta cerca de '@TablaBorrar'.

Sin embargo, cuando hago el Drop Table tmp_dist_009, así, sin usar la
variable, sí que me funciona.

Advierto que estoy bastante pez en Transact, así que si es una chorrada, no
me regañeis mucho.

Gracias.

Preguntas similare

Leer las respuestas

#6 Maxi
17/10/2005 - 15:26 | Informe spam
Nop, hay 2 tipos de tablas, las # y las ##, las primeras mueren en el
contexto por ej un SP y las segundas son compartidas (globales) pero no es
buena practica borrar las tablas temporales, esto es un indicio de mala
programacion y futuros problemas :(


Salu2
Maxi [MVP SQL SERVER]


"Lautaurus" escribió en el mensaje
news:4353a647$0$41143$
Maxi, sólo las tablas cuyo nombre empieza por # se mueren al cerrar la
conexion.


Maxi escribió:
Hola, no creo que sea una buena practica borrar tablas temporales porque
si!! si estan es porque alguien las esta usando, o con que criterio las
vas a borrar? sabias que las tablas temporales tienen un alcance y que
por ej si se cierra la conexion ya se mueren

Respuesta Responder a este mensaje
#7 Lautaurus
17/10/2005 - 15:38 | Informe spam
Extraido del BOL, "Creating and Modifying a Table":

The two types of temporary tables, local and global, differ from each
other in their names, their visibility, and their availability. Local
temporary tables have a single number sign (#) as the first character of
their names; they are visible only to the current connection for the
user; and they are deleted when the user disconnects from instances of
Microsoft® SQL Server™ 2000. Global temporary tables have two number
signs (##) as the first characters of their names; they are visible to
any user after they are created; and they are deleted when all users
referencing the table disconnect from SQL Server.


Maxi escribió:
Nop, hay 2 tipos de tablas, las # y las ##, las primeras mueren en el
contexto por ej un SP y las segundas son compartidas (globales) pero no es
buena practica borrar las tablas temporales, esto es un indicio de mala
programacion y futuros problemas :(

Respuesta Responder a este mensaje
#8 Maxi
17/10/2005 - 15:41 | Informe spam
Hola, y yo que dije?


Salu2
Maxi [MVP SQL SERVER]


"Lautaurus" escribió en el mensaje
news:4353a946$0$41145$
Extraido del BOL, "Creating and Modifying a Table":

The two types of temporary tables, local and global, differ from each
other in their names, their visibility, and their availability. Local
temporary tables have a single number sign (#) as the first character of
their names; they are visible only to the current connection for the user;
and they are deleted when the user disconnects from instances of
Microsoft® SQL Server™ 2000. Global temporary tables have two number signs
(##) as the first characters of their names; they are visible to any user
after they are created; and they are deleted when all users referencing
the table disconnect from SQL Server.


Maxi escribió:
Nop, hay 2 tipos de tablas, las # y las ##, las primeras mueren en el
contexto por ej un SP y las segundas son compartidas (globales) pero no
es buena practica borrar las tablas temporales, esto es un indicio de
mala programacion y futuros problemas :(

Respuesta Responder a este mensaje
#9 Lautaurus
17/10/2005 - 15:52 | Informe spam
Tu dijiste que las tablas temporales se mueren al cerrar la conexión, y
yo creo que esto sólo sucede con las tablas que comienzan por #.
Sin embargo si creas una tabla "pepito" (que no comienze por #) en el
tempdb, esa tabla existe hasta que se reinicie el servicio o se borre
explicitamente.

Maxi escribió:
Hola, y yo que dije?

Respuesta Responder a este mensaje
#10 Maxi
17/10/2005 - 16:03 | Informe spam
Sip, pero no deberias crear tablas pepitos en la tempdb ;-)


Salu2
Maxi [MVP SQL SERVER]


"Lautaurus" escribió en el mensaje
news:4353acb5$0$41147$
Tu dijiste que las tablas temporales se mueren al cerrar la conexión, y yo
creo que esto sólo sucede con las tablas que comienzan por #.
Sin embargo si creas una tabla "pepito" (que no comienze por #) en el
tempdb, esa tabla existe hasta que se reinicie el servicio o se borre
explicitamente.

Maxi escribió:
Hola, y yo que dije?

Respuesta Responder a este mensaje
Ads by Google
Help Hacer una preguntaSiguiente AnteriorRespuesta Tengo una respuesta
Search Busqueda sugerida