Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2003
    Posts
    46

    Unanswered: Need script to kill all processes on a database

    I restore a backup every week. It serves a couple of purposes. One, to verify the backup and secondly, to give us a current testing/training environment. As things evolve, it would be nice to script this and then schedule it as a job.

    I can write the restore statement, but it fails if any sessions are open on the db. I thought about using sp_who and the the kill statement. But I was having trouble using the recordset returned by sp_who.

    Now I'm thinking the sysprocess table is a better place to look. Does anybody have some experience in this area and can point me in the right direction?

    While I'm going down this path, does anybody have a script to look at the master database and dynamically manage their backup strategy? That would be super cool?!?!

    Thanks,

    Alex8675

  2. #2
    Join Date
    May 2007
    Location
    somewhere in dbforums
    Posts
    221
    Quote Originally Posted by alex8675
    I restore a backup every week. It serves a couple of purposes. One, to verify the backup and secondly, to give us a current testing/training environment. As things evolve, it would be nice to script this and then schedule it as a job.

    I can write the restore statement, but it fails if any sessions are open on the db. I thought about using sp_who and the the kill statement. But I was having trouble using the recordset returned by sp_who.

    Now I'm thinking the sysprocess table is a better place to look. Does anybody have some experience in this area and can point me in the right direction?
    Go to google type "kill all processes IN A DATABASE SQL SERVER" i got atleast 10 different SP's which will do your job......choose the one which suits you

  3. #3
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    this will work if you are not logged connected to the database and no other sysadmin is doing anything...

    ALTER DATABASE MyDatabase SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  4. #4
    Join Date
    Dec 2003
    Posts
    46

    Thank you

    Very elegant solution and exactly what I was looking for...but I couldn't find and was going a long way around to find what was readily available.

  5. #5
    Join Date
    Oct 2006
    Location
    CA
    Posts
    210
    Quote Originally Posted by Thrasymachus
    this will work if you are not logged connected to the database and no other sysadmin is doing anything...

    ALTER DATABASE MyDatabase SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE
    This one's not as elegant, but worked when I had to disconnect / move the log files tonight.
    DECLARE @sql nVARCHAR(4000)
    SET @sql = ''

    -- To disconnect, first kill all processes.
    SELECT @sql = @sql + ' KILL ' + CAST(spid AS VARCHAR(10)) + ' '
    FROM master.dbo.sysprocesses
    WHERE DB_NAME(dbid) = @MYDB
    AND spid > 50 AND spid <> @@SPID
    EXEC(@sql)
    Thrasymachus; Do I have to do something to undo the SET?
    Last edited by vich; 08-07-07 at 03:23. Reason: added script

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •