【发布时间】:2009-11-27 12:05:14
【问题描述】:
我想修改以下内容,因为它似乎不会杀死进程 - 我认为它应该断开用户连接(这是一样的吗?)。我希望能够杀死特定数据库的所有进程 - 如何修改以下内容:
create procedure [dbo].[sp_killusers](@database varchar(30))
as
----------------------------------------------------
-- * Created By David Wiseman, Updated 19/11/2006
-- * http://www.wisesoft.co.uk
-- * This procedure takes the name of a database as input
-- * and uses the kill statment to disconnect them from
-- * the database.
-- * PLEASE USE WITH CAUTION!!
-- * Usage:
-- * exec sp_killusers 'databasename'
----------------------------------------------------
set nocount on
declare @spid int
declare @killstatement nvarchar(10)
-- Declare a cursor to select the users connected to the specified database
declare c1 cursor for select request_session_id
from sys.dm_tran_locks
where resource_type='DATABASE'
AND DB_NAME(resource_database_id) = @database
open c1
fetch next from c1 into @spid
-- for each spid...
while @@FETCH_STATUS = 0
begin
-- Don't kill the connection of the user executing this statement
IF @@SPID <> @spid
begin
-- Construct dynamic sql to kill spid
set @killstatement = 'KILL ' + cast(@spid as varchar(3))
exec sp_executesql @killstatement
-- Print killed spid
print @spid
end
fetch next from c1 into @spid
end
-- Clean up
close c1
deallocate c1
更新
上述方法不起作用,即它不会终止进程。
它不会终止进程。我看着 活动监视器及其静止 显示过程仍在继续,我可以 看到我的查询仍在工作 查询窗口。当我执行“杀死 53”时, 查询在查询窗口中停止,并且 该过程已从活动中消失 监视器!所以杀死工作,但这个过程为什么不?
【问题讨论】:
-
你能定义你所说的“杀死进程”是什么意思吗?我认为不可能从存储过程中终止常规 Windows 进程(无论如何这会很奇怪,请考虑为此编写应用程序)。
-
我说的是 SQL Server - 以及 SQL Server 中的进程。试图杀死 SQL Server 而不是 windows 中的进程/查询。
标签: sql-server process kill