How to close all connections on a database ?

  • Rather than looping through sys.sysprocesses and kill all SPID's, I remember that there was some ALTER DATABASE command that closes all existing connections ?

  • ALTER DATABASE DBNAMESET SINGLE_USER WITH ROLLBACK IMMEDIATE

    The database will be in single user mode after this has run.

    Gethyn Elliswww.gethynellis.com

  • Thanks Gethyn

    This is exacltly what I needed. Also, should I set it to multi user after restore ?

  • If the database was not backed up while in single user mode, then it will not be restored in that condition. So, no - you won't need to set it back.

    You could also use the following:

    ALTER DATABASE {your database} SET OFFLINE WITH ROLLBACK IMMEDIATE;

    Then perform your restore. This will prevent any other processes from taking the single user connection and blocking your restore.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply