Solved

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

Posted on 2016-08-15
2
91 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
2 Comments
 
LVL 13

Accepted Solution

by:
Nakul Vachhrajani earned 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Job Hung 17 37
MS SQL Sever Import/export problem 7 51
Options for Linking SQL tables to Access 2013 9 42
Related to SQL Query 5 21
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

726 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