首页 > 代码库 > [SQL]用SQL语句断开某个数据库的所有活动连接

[SQL]用SQL语句断开某个数据库的所有活动连接

 1 USE master 2 go 3  4 IF EXISTS ( SELECT  * 5             FROM    dbo.sysobjects 6             WHERE   id = OBJECT_ID(N[dbo].[P_KillConnections]) 7                     AND OBJECTPROPERTY(id, NIsProcedure) = 1 )  8     DROP PROCEDURE [dbo].[P_KillConnections] 9 GO10 11 CREATE PROC P_KillConnections @dbname VARCHAR(200)12 AS 13     DECLARE @sql NVARCHAR(500)14     DECLARE @spid NVARCHAR(20)15 16     DECLARE #tb CURSOR FOR17     SELECT spid=CAST(spid AS VARCHAR(20)) FROM master..sysprocesses WHERE dbid=DB_ID(@dbname)18     OPEN #tb19     FETCH NEXT FROM #tb INTO @spid20     WHILE @@fetch_status = 0 21         BEGIN22             EXEC(kill +@spid)23             FETCH NEXT FROM #tb INTO @spid24         END25     CLOSE #tb26     DEALLOCATE #tb27 go28 29 30 --修改一下31 EXEC P_KillConnections 修改成自己的数据库

 

[SQL]用SQL语句断开某个数据库的所有活动连接