When restoring a backup, how do I disconnect all active connections?

后端 未结 10 1073
失恋的感觉
失恋的感觉 2020-12-22 15:26

My SQL Server 2005 doesn\'t restore a backup because of active connections. How can I force it?

10条回答
  •  不知归路
    2020-12-22 16:07

    Try this:

    DECLARE UserCursor CURSOR LOCAL FAST_FORWARD FOR
    SELECT
        spid
    FROM
        master.dbo.sysprocesses
    WHERE DB_NAME(dbid) = 'dbname'--replace the dbname with your database
    DECLARE @spid SMALLINT
    DECLARE @SQLCommand VARCHAR(300)
    OPEN UserCursor
    FETCH NEXT FROM UserCursor INTO
        @spid
    WHILE @@FETCH_STATUS = 0
    BEGIN
        SET @SQLCommand = 'KILL ' + CAST(@spid AS VARCHAR)
        EXECUTE(@SQLCommand)
        FETCH NEXT FROM UserCursor INTO
            @spid
    END
    CLOSE UserCursor
    DEALLOCATE UserCursor
    GO
    

提交回复
热议问题