Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2016-08-15
2
Medium Priority
?
112 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

636 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