如何设置已被连接占用的数据库为MULTI_USER模式,数据库 'xxx' 已打开,并且一次只能有一个用户访问
作者:互联网
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;--设置为多用户模式
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;--设置为多用户模式
标签:MULTI,SET,--,数据库,xxx,DBName,SQL,tb,DECLARE 来源: https://www.cnblogs.com/chengyihardworking/p/16225190.html