Skip to main content

Posts

Showing posts with the label kill connections

Simple T-SQL Script to Kill All Connections to a MSSQL Database

When restoring an MSSQL database, it is common to receive an error similar to this one:  " The database could not be exclusively locked to perform the operation ." Msg 5030, Level 16, State 12, Line 1   This error can occur in MSSQL  2005, 2008 or 2012. There are a number of workarounds for this. Some will tell you to disable the website's application pool in IIS, but this doesnt help if someone remotely is connected using SQL Management Studio. You can go into Performance Monitor and manually kill each process, but often the processes jump up faster than you can disable them by hand.  Where that is the case, perform the following procedure. Assuming your restore job task window is properly configured and just open, keep it open. You can right click on the database server and select New Query without closing the restore job task window, which wastes time when you must recreate it. Then, enter the following script into the query menu (be sure to update the