?
Solved

Following SQL Query runs successfully but doesn't kill the sessions/processes

Posted on 2016-08-15
2
Medium Priority
?
107 Views
Last Modified: 2016-08-15
Heyas,

I found the following query on Google:

DECLARE @SQL varchar(max)

SELECT @SQL = COALESCE(@SQL,'') + 'Kill ' + Convert(varchar, SPId) + ';'
FROM MASTER..SysProcesses
WHERE DBId = DB_ID(<databasename>) AND SPId <> @@SPId

--SELECT @SQL
EXEC(@SQL)

Fairly straightforward however when I run it doesn't work, but the execution window says query run successfully.

How would I go about debugging this? I am running SQL Server 2008 R2

Any assistance is appreciated.

Thank you.
0
Comment
Question by:Zack
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 14

Accepted Solution

by:
Nakul Vachhrajani earned 2000 total points
ID: 41757339
Can you confirm that the statement is indeed well-formed? Just uncomment the SELECT (@SQL), comment out the EXEC and then run the statement to confirm.

Next it is possible that by the time your query runs, the processes have ended. KILL will run silently.

Finally, try replacing sysprocesses with newer DMV variants (sys.dm_tran_locks, sys.dm_exec_sessions, or sys.dm_exec_requests) because sysprocess is included purely for backward compatibility.
0
 

Author Closing Comment

by:Zack
ID: 41757352
Thanks using the new DMV variants fixed the issue.
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question