Udostępnij za pośrednictwem


KILL (Transact-SQL)

Terminates a user process that is based on the session ID (SPID in SQL Server 2000 and earlier) or unit of work (UOW). If the specified session ID or UOW has a lot of work to undo, the KILL statement may take some time to complete, particularly when it involves rolling back a long transaction.

In Microsoft SQL Server 2000 and later versions, KILL can be used to terminate a normal connection, which internally terminates the transactions that are associated with the specified session ID. The statement can also be used to terminate orphaned and in-doubt distributed transactions when Microsoft Distributed Transaction Coordinator (MS DTC) is in use.

Syntax

KILL { session ID | UOW } [ WITH STATUSONLY ] 

Arguments

  • session ID
    Is the session ID of the process to terminate. session ID is a unique integer (int) that is assigned to each user connection when the connection is made. The session ID value is tied to the connection for the duration of the connection. When the connection ends, the integer value is released and can be reassigned to a new connection.

    Use KILL session ID to terminate regular nondistributed and distributed transactions that are associated with a specified session ID.

  • UOW
    Identifies the Unit of Work ID (UOW) of distributed transactions. UOW is a GUID that may be obtained from the request_owner_guid column of the sys.dm_tran_locks dynamic management view. UOW also can be obtained from the error log or through the MS DTC monitor. For more information about monitoring distributed transactions, see the MS DTC documentation.

    Use KILL UOW to terminate orphaned distributed transactions. These transactions are not associated with any real session ID, but instead are associated artificially with session ID = '-2'. This session ID makes it easier to identify orphaned transactions by querying the session ID column in sys.dm_tran_locks, sys.dm_exec_sessions, or sys.dm_exec_requests dynamic management views.

  • WITH STATUSONLY
    Generates a progress report about a specified session ID or UOW that is being rolled back due to an earlier KILL statement. KILL WITH STATUSONLY does not terminate or roll back the session ID or UOW; the command only displays the current progress of the rollback.

Remarks

KILL is commonly used to terminate a process that is blocking other important processes with locks, or a process that is executing a query that is using necessary system resources. System processes and processes running an extended stored procedure cannot be terminated.

Use KILL very carefully, especially when critical processes are running. You cannot kill your own process. Other processes you should not to kill include the following:

  • AWAITING COMMAND
  • CHECKPOINT SLEEP
  • LAZY WRITER
  • LOCK MONITOR
  • SIGNAL HANDLER

Use @@SPID to display the session ID value for the current session.

To obtain a report of active session ID values, you can query the session_id column of the sys.dm_tran_locks, sys.dm_exec_sessions, and sys.dm_exec_requests dynamic management views. You can also view the SPID column that is returned by the sp_who system stored procedure. If a rollback is in progress for a specific SPID, the cmd column in the sp_who result set for that SPID will indicate KILLED/ROLLBACK.

When a particular connection has a lock on a database resource and blocks the progress of another connection, the session ID of the blocking connection shows up in the blocking_session_id column of sys.dm_exec_requests or the blk column returned by sp_who.

The KILL command can be used to resolve in-doubt distributed transactions. These transactions are unresolved distributed transactions that occur because of unplanned restarts of the database server or MS DTC coordinator. For more information about in-doubt transactions, see the "Two-Phase Commit" section in Using Marked Transactions (Full Recovery Model).

Using WITH STATUSONLY

KILL WITH STATUSONLY generates a report only if the session ID or UOW is currently being rolled back because of a previous KILL session ID|UOW statement. The progress report states the amount of rollback completed (in percent) and the estimated length of time left (in seconds), in the following form:

Spid|UOW <xxx>: Transaction rollback in progress. Estimated rollback completion: <yy>% Estimated time left: <zz> seconds

If the rollback of the session ID or UOW has finished when the KILL session ID|UOW WITH STATUSONLY statement is executed, or if no session ID or UOW is being rolled back, KILL session ID|UOW WITH STATUSONLY will return the following error:

"Msg 6120, Level 16, State 1, Line 1"

"Status report cannot be obtained. Rollback operation for Process ID <session ID> is not in progress."

The same status report can be obtained by repeating the same KILL session ID|UOW statement without using the WITH STATUSONLY option; however, we do not recommend doing this. Repeating a KILL session ID statement might terminate a new process if the rollback had finished and the session ID was reassigned to a new task before the new KILL statement is run. Specifying WITH STATUSONLY prevents this from happening.

Permissions

Requires membership in the sysadmin and processadmin fixed database roles.

Examples

A. Using KILL to terminate a session

The following example shows how to terminate session ID 53.

KILL 53;
GO

B. Using KILL session ID WITH STATUSONLY to obtain a progress report

The following example generates a status of the rollback process for the specific session ID.

KILL 54;
KILL 54 WITH STATUSONLY;
GO

--This is the progress report.
spid 54: Transaction rollback in progress. Estimated rollback completion: 80% Estimated time left: 10 seconds.

C. Using KILL to terminate an orphaned distributed transaction

The following example shows how to terminate an orphaned distributed transaction (session ID = -2) with a UOW of D5499C66-E398-45CA-BF7E-DC9C194B48CF.

KILL 'D5499C66-E398-45CA-BF7E-DC9C194B48CF';

See Also

Reference

KILL STATS JOB (Transact-SQL)
KILL QUERY NOTIFICATION SUBSCRIPTION (Transact-SQL)
Functions (Transact-SQL)
SHUTDOWN (Transact-SQL)
@@SPID (Transact-SQL)
sys.dm_exec_requests
sys.dm_exec_sessions
sys.dm_tran_locks
sp_lock (Transact-SQL)
sp_who (Transact-SQL)

Help and Information

Getting SQL Server 2005 Assistance

Change History

Release History

14 April 2006

Changed content:
  • Replaced references to SPID with session ID