USE master;
GO
--方法一
DECLARE @SQL VARCHAR(3000);
SET @SQL = '';
SELECT @SQL = @SQL+'; KILL ' + RTRIM(SPID)
FROM [sys].[sysprocesses] AS sps
WHERE [sps].[dbid] = DB_ID('test');
SET @SQL = SUBSTRING(@SQL, 2, LEN(@SQL));
EXEC(@SQL);
GO
--方法二
DECLARE @DBName SYSNAME;
SET @DBName = 'ESB'; --这个是要删除的数据库库名
DECLARE @KSQL NVARCHAR(1000)
DECLARE tb CURSOR LOCAL
FOR
SELECT
KSQL = 'KILL ' + CAST([sps].[spid] AS NVARCHAR(10))
FROM [sys].[sysprocesses] AS sps
WHERE dbid = DB_ID(@DBName)--查询@DBName相关的线程
--循环杀掉要删除数据的相关线程
OPEN tb
FETCH NEXT FROM tb INTO @KSQL
WHILE @@FETCH_STATUS = 0
BEGIN
EXECUTE(@KSQL);
FETCH NEXT FROM tb INTO @KSQL
END
CLOSE tb
DEALLOCATE tb
--方法三
ALTER DATABASE [ESB] SET MULTI_USER;--设置为多用户模式