SQL 取消当前的所有在执行的查询语句进程

DECLARE @sql NVARCHAR(MAX),@currDbName NVARCHAR(100)
SET @currDbName=DB_NAME() --如果在当前库上执行,可改为: SET @currDbName = DB_NAME()
WHILE EXISTS( SELECT 1 FROM sys.sysprocesses WHERE dbid=DB_ID(@currDbName) AND spid!=@@SPID )
BEGIN
SELECT TOP 1 @sql='kill '+CAST(spid AS NVARCHAR(50)) FROM sys.sysprocesses WHERE dbid=DB_ID(@currDbName) AND spid!=@@SPID
EXEC (@sql)
END

 

posted @ 2016-08-31 14:42  管学文  阅读(1275)  评论(0编辑  收藏  举报