← Tutti gli articoli
How to close all the database User connections. Sql Server 2008.
22 September 2010 ·
TSQL · Article ·
197 visite
How to close all the database User connections. Sql Server 2008.
Create the procedure [dbo].[utl_KillUsers] on the master database:
Then execute it:
USE master GO CREATE PROCEDURE [dbo].[utl_KillUsers] @dbname varchar(50) as SET NOCOUNT ON DECLARE @strSQL varchar(255) CREATE table #tmpUsers( spid int, eid int, status varchar(30), loginname varchar(50), hostname varchar(50), blk int, dbname varchar(50), cmd varchar(30), request_id int ) INSERT INTO #tmpUsers EXEC SP_WHO DECLARE LoginCursor CURSOR READ_ONLY FOR SELECT spid, dbname FROM #tmpUsers WHERE dbname = @dbname DECLARE @spid varchar(10) DECLARE @dbname2 varchar(40) OPEN LoginCursor FETCH NEXT FROM LoginCursor INTO @spid, @dbname2 WHILE (@@fetch_status <> -1) BEGIN IF (@@fetch_status <> -2) BEGIN PRINT 'Killing ' + @spid SET @strSQL = 'KILL ' + @spid EXEC (@strSQL) END FETCH NEXT FROM LoginCursor INTO @spid, @dbname2 END CLOSE LoginCursor DEALLOCATE LoginCursor DROP table #tmpUsers
Then execute it:
use master go exec [dbo].[utl_KillUsers] 'ContraTest'For more information see: force-close-connections