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