A Microsoft SQL Server Database Administrator’s Daily Bread: Killing Processes

A few years ago, I started on a path to become a database administrator and read many articles on the Internet on how to break into that field.  There were a lot of articles out there, but I don’t remember finding many that offered practical knowledge (i.e. advice on what problems database administrators might encounter on a day-to-day basis).  Please note that at the time I am writing this article, I am not officially a database administrator within my organization, but I do perform database related duties and wanted to share my experience with you.

Working for a hosting company, our customers do not have complete access/control over their databases which is understandable since you are offloading management duties to the hosting company.  Learning how to correctly kill a process can be essential to getting a database back in working order, and here’s a list of the occasions where I’ve had to do it:

1) The database had open transactions causing a database lock.
2) The database somehow got set to SINGLE_USER mode, the customer was still logged in, and it needed to be set back to MULTI_USER mode (This works 9 times out of 10.  Sometimes, the person is logged in with SQL Server Management Studio, and you won’t be able to kill the process.  In that case, you just ask the customer politely to log off.)
3) Trying to bring a database online/offline, and it gets stuck.
4) The database Auto-Close setting got set to true or on.
5) Database Attach/Backup/Restore jobs which get stuck.

There are probably many ways to do this, but I found this to be the simplest to understand:

1) Make sure you’re logged in with an account that has SQL Server Administrator privileges using SQL Server Management Studio.
2) Open a New Query window and type in the following replacing DatabaseName with the name of the database.

USE MASTER
GO

SELECT DB_ID(N'DatabaseName') AS [Database ID]

3) It should return a number in the Results windows.

DatabaseID Results Window

4) Enter the next set of T-SQL commands in the same or new window replacing # with the Database ID number from the Results window.  If you are doing this in the same window, remember to highlight only the following code and then hit F5 or the Execute button so that you will not be executing other T-SQL statements.

USE MASTER
GO

SELECT * FROM SYSPROCESSES WHERE DBID = #

5) You should get a set of results like the one below.

SPIDs

6. To kill the process, look at the spid number and type in the following in the Query window replacing SPID# with the actual number.

KILL SPID#

For open transactions, if the number is greater than 0, then that most likely is the process that is causing the problem, and you can just kill that one.  Familiarize yourself with the column names and their meanings as they will help you identify which processes you should be removing.  Sometimes, I’ve had to clear all the processes associated with the database so that I could execute other commands to perform maintenance on it.  As with anything, use the kill command with great caution and care as you don’t want to be killing system related processes that could crash the SQL Server engine or cause corruption to your databases.