Database query specifies the current number of connections and shut down all connections to the specified database

- The current database can accept the maximum number of connections
the SELECT @@ MAX_CONNECTIONS
- query the database for all current connections
sp_who

- Specify the database query the current number of connections

SELECT * FROM
[Master].[dbo].[SYSPROCESSES] WHERE [DBID]
IN
(
SELECT
   [DBID]
FROM
   [Master].[dbo].[SYSDATABASES]
WHERE
   NAME='fdaysmanygoods'
)

--关闭指定数据库的所有连接
use   master
declare   @spid   int,@str   varchar(100),@dbid   int,@dbname   varchar(255)
set   @dbname= 'fdaysmanygoods '
select   @dbid=dbid   from   master.dbo.sysdatabases   WHERE   name   =   @dbname
declare   cur_spid   cursor   local   for
      select   spid   from   master.dbo.sysprocesses   where   dbid=@dbid
open   cur_spid  
fetch   from   cur_spid   into   @spid
while   @@fetch_status=0  
begin
    set   @str= 'kill   '+Cast(@spid   as   varchar(10))
    exec(@str)
    fetch   from   cur_spid   into   @spid
end
close   cur_spid
deallocate   cur_spid

Reproduced in: https: //www.cnblogs.com/kevinGao/archive/2012/07/05/2605591.html

Guess you like

Origin blog.csdn.net/weixin_33998125/article/details/93052527