• One of the easiest and fastest ways to clear all connections to a database is to use the following code (change the DB name, of course)... read the comments because there is a danger in doing so... and, keep in mind that this kills EVERYTHING with great prejudice. Any long-running transactions will take time to rollback. It also kills connections made by jobs. I'll say it again... it KILLS EVERYTHING. Use with care.

    USE [master]

    GO

    --===== This kills everything except the connection running this command.

    ALTER DATABASE [YourDbNameHere] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

    --===== This sets it back to normal so that, if you lose the connection,

    -- some app doesn't grab the only connection locking you out from it.

    -- As an alternative, you could set it to restricted users but that

    -- will also still let some apps in if they inappropriately have

    -- the restricted privs

    ALTER DATABASE [YourDbNameHere] SET MULTI_USER;

    --===== Then, do your stuff immediately and without delay.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)