USE MASTER
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_KillDatabaseProcesses]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_KillDatabaseProcesses]
GO
CREATE PROCEDURE dbo.sp_KillDatabaseProcesses
( @databaseName varchar(100))
AS
DECLARE @databaseId int,
@sysProcessId int,
@cmd varchar(1000)
EXEC ('USE MASTER')
SELECT @databaseId = dbid FROM master..sysdatabases
WHERE [name] = @databaseName
DECLARE sysProcessIdCursor CURSOR FOR
SELECT spid FROM [master]..[sysprocesses] WHERE [dbid] = @databaseId
OPEN sysProcessIdCursor
FETCH NEXT FROM sysProcessIdCursor INTO @sysProcessId
WHILE @@fetch_status = 0
BEGIN
SET @cmd = 'KILL '+ convert(nvarchar(30),@sysProcessId)
PRINT @cmd
EXEC(@cmd)
FETCH NEXT FROM sysProcessIdCursor INTO @sysProcessId
END
DEALLOCATE sysProcessIdCursor
GO
No comments:
Post a Comment