Solved

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

Posted on 2016-08-15
2
79 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 12

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
string fuctions 4 25
What is the proper way to use for criteria in left join? 7 25
Can > be used for a Text field 6 37
Increment column based of a FK 8 20
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…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
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.

786 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