Cierre (KILL) todos los SPIDs conectados a una base de Datos

/, SQL/Cierre (KILL) todos los SPIDs conectados a una base de Datos

Cierre (KILL) todos los SPIDs conectados a una base de Datos

En el caso que se tienen que cerrar todas las conexiones a una base de datos de SQL Server, se puede usar un cursor y el comando KILL para cerrar las sesiones. En este caso, se incluye todos los SPIDs conectados a la base de datos en el cursor. Después se crea la cadena con KILL . Finalmente, se ejecuta el comando KILL para cerrar la conexión.
Debajo está el comando que usa LiteSpeed para terminar todas las sesiones antes de completar sus tareas.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
--	Terminar sessiones en una base de datos
--	Ejecute desde master u otra base de datos
--	Cambie DB_NAME a la base de datos seleccionada 
 
--	Declarar un varchar 
DECLARE @cmdKill VARCHAR (50)
 
--	Popule el cursor con los SPIDs conectados a la base de datos
DECLARE killCursor 
CURSOR FOR
SELECT 'KILL ' + Convert(VARCHAR (5), p.spid )
FROM   master.dbo.sysprocesses AS p
WHERE  p.dbid = db_id( 'DB_NAME')
 
--	Abre el cursor 
OPEN killCursor
FETCH killCursor INTO @cmdKill
 
--	Termina todos los SPID conectados a esta base de datos
WHILE 0 = @@fetch_status
BEGIN
	EXECUTE (@cmdKill )
	FETCH killCursor INTO @cmdKill
END
 
--	Cierre el cursor
CLOSE killCursor
DEALLOCATE killCursor
By | 2015-07-01T21:56:34-05:00 July 1st, 2015|Categories: 200, SQL|Tags: , , , |0 Comments

About the Author:

Mi nombre es Rodolfo y trabajo en Austin, Tejas. He estado trabajando con SQL Server desde 2003. Mi enfoque es SSRS, SSIS y el ajuste de rendimiento.

This Is A Custom Widget

This Sliding Bar can be switched on or off in theme options, and can take any widget you throw at it or even fill it with your custom HTML Code. Its perfect for grabbing the attention of your viewers. Choose between 1, 2, 3 or 4 columns, set the background color, widget divider color, activate transparency, a top border or fully disable it on desktop and mobile.

This Is A Custom Widget

This Sliding Bar can be switched on or off in theme options, and can take any widget you throw at it or even fill it with your custom HTML Code. Its perfect for grabbing the attention of your viewers. Choose between 1, 2, 3 or 4 columns, set the background color, widget divider color, activate transparency, a top border or fully disable it on desktop and mobile.