刪除當前資料庫連線使用者

stephenjwq發表於2019-04-18
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 '<DBNAME>'     ##輸入你的DBNAME


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9522838/viewspace-2641753/,如需轉載,請註明出處,否則將追究法律責任。

相關文章