Saturday, 16 September 2023

Kill command in SQL server

When we are connection to the SQL server instance or opening the new query window, SQL server assigns a unique identification number to each connection. It is the Server Process ID and commonly known as SPID. 

 With the help of @@SPID we will get the current SPID. See below.  

 SPID 1 to 50 is reserved for SQL Server internal processes, and the user process starts from SPID 51.

We can get the all active SPID using SP_WHO or SP_WHO 2 command.

 

 Kill Command

This command is use to Ends a user process that is based on the session ID. 

Syntax

Kill <SPID>

This kill the particular session.

Example  

Here both Session ID 56 and 66 are connected. Now we are killing the session id 66 for session ID 56 window. Running the kill command.

SPID : 66 killed successfully.

If we run again we will get the below message

  

Below is the script to kill the all SPID for a database

DECLARE @str_sql NVARCHAR(1000),

        @db_name VARCHAR(100),

        @number_of_session_killed INT 

SET @db_name = 'AdventureWorks2019'

 PRINT 'START: Killing active connections to the "'

      + @db_name + '" database'

 -- Count Connections

SELECT @number_of_session_killed = Count(*)

FROM   master.dbo.sysprocesses

WHERE  Db_name(dbid) = @db_name

       AND dbid <> 0

       AND spid <> @@spid

-- Create the sql to kill the active database connections

SET @str_sql = ''

 SELECT @str_sql = @str_sql + 'kill ' + CONVERT(CHAR(10), spid) + ' '

FROM   master.dbo.sysprocesses

WHERE  Db_name(dbid) = @db_name

       AND dbid <> 0

       AND spid <> @@spid

EXEC (@str_sql)

PRINT @str_sql

PRINT 'END: Killed "'

      + Cast(@number_of_session_killed AS VARCHAR(4))

      + '" active connections to the "' + @db_name

      + '" database' 

go

Running this script    

We can't kill our own process also shouldn't kill the following processes:

Ø  AWAITING COMMAND

Ø  CHECKPOINT SLEEP

Ø  LAZY WRITER

Ø  LOCK MONITOR

Ø  SIGNAL HANDLER

KILL WITH STATUSONLY

This option tells SQL Server to terminate the specified process and provide information about the termination status without actually terminating the process. It's a way to check the status of the process before deciding whether to proceed with termination.

Syntax

KILL SPID WITH STATUSONLY; 

Running the command;

 

This gives the information only. This is not terminating the SPID 58. Still it is in active state. Let’s see.

 

Still Session ID 58 is active.

14 comments:

  1. Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job Man,Keep it up. Meanwhile visit our website for essay writing service |
    essay writing help
    essay writing services
    essay writing help australia
    online essay help
    essay help online
    essay help australia
    essay services online

    ReplyDelete
  2. Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it.

    ReplyDelete
  3. Thanks for sharing this information For more info, please connect Best Calendar Management Services in USA

    ReplyDelete
  4. I appreciate your clear explanations and the outstanding alignment of your content with your blog! I strongly recommend visiting the website below if you're interested in sharing your work.
    https://3zenx.com/performance-testing-training-in-hyderabad

    ReplyDelete
  5. Hi, I am new to your audience on your article, I would say that you are doing fantastic work, and this would be super useful.
    Latest News

    ReplyDelete
  6. Thanks for sharing this information For more info, please connect Top RPO Companies In USA 2024

    ReplyDelete
  7. Thanks for sharing this information For more info, please connect Best SEO Services in USA

    ReplyDelete
  8. I appreciate your kind words about the knowledge you provide and your openness to suggestions for enhancing my life and personal growth. One way to pursue this could involve exploring opportunities to improve your skills in Global Degrees After finishing 12th grade Global Degrees can help Indian students study abroad, offering them a chance for an amazing academic adventure. Global Degrees works with universities to create a supportive environment where students can do well in their studies and grow personally and culturally.

    ReplyDelete
  9. Thank you for sharing useful information with us. please keep sharing like this. And if you are searching an ONLINE MBA DEGREE COURSE from Top Universities in India, Colleges discovery platform, which connects students or working professionals with Universities/colleges, at the same time offering information about colleges, courses, entrance exam details, admission notifications, scholarships, and all related topics. Please visit below links:
    https://onlinembadegree.in/

    ReplyDelete
  10. Amazing blog, thanks for sharing. At a time when internet popularity is spreading at a rapid pace it's important that you use this platform for knowing about different things easily. Reporters as well as bloggers from different portals of the world are updating these blogs on a regular basis.
    click here for more information





    ReplyDelete
  11. Thanks for sharing such a nice blog with us . keep updating please reach on https://overseasdropshipper.com/

    ReplyDelete
  12. Thanks for sharing such a nice blog with us . keep updating please reach on https://overseasdropshipper.com/

    ReplyDelete

If you have any doubt, please let me know.

Popular Posts