端口数据库连接

USE master
go

IF EXISTS ( SELECT  *
            FROM    dbo.sysobjects
            WHERE   id = OBJECT_ID(N'[dbo].[P_KillConnections]')
                    AND OBJECTPROPERTY(id, N'IsProcedure') = 1 ) 
    DROP PROCEDURE [dbo].[P_KillConnections]
GO

CREATE PROC P_KillConnections @dbname VARCHAR(200)
AS 
    DECLARE @sql NVARCHAR(500)
    DECLARE @spid NVARCHAR(20)

    DECLARE #tb CURSOR FOR
    SELECT spid=CAST(spid AS VARCHAR(20)) FROM master..sysprocesses WHERE dbid=DB_ID(@dbname)
    OPEN #tb
    FETCH NEXT FROM #tb INTO @spid
    WHILE @@fetch_status = 0 
        BEGIN
            EXEC('kill '+@spid)
            FETCH NEXT FROM #tb INTO @spid
        END
    CLOSE #tb
    DEALLOCATE #tb
go


--修改一下
EXEC P_KillConnections '修改成自己的数据库'

 

posted @ 2015-11-17 14:44  melody&bobo  阅读(303)  评论(0编辑  收藏  举报