Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Code to Kill Stale Processes


SQL Code to Kill Stale Processes

Author
Message
MyDoggieJessie
MyDoggieJessie
SSCarpal Tunnel
SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)

Group: General Forum Members
Points: 4905 Visits: 7365
I have also seen cases where uses are testing their procedures/queries and leave their session open...depending on the stuff they have been running, it can take up GB's in TempDB - so I kill them as well

______________________________________________________________________________
"Never argue with an idiot; They'll drag you down to their level and beat you with experience" ;-)
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47261 Visits: 44392
AndrewSQLDBA (8/23/2013)
Why would I not want too? They are taking up memory and cpu


An idle thread consumes no CPU. It'll have a 2MB thread stack (or 4MB, can't recall), no other memory usage.


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Sean Pearce
Sean Pearce
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1170 Visits: 3432
This code will kill all processes who do not have an active request, who's last request was more than 24 hours ago and are connecting from management studio. You can adjust the WHERE clause to suit your needs.

DECLARE @SPID INT,
@SQL NVARCHAR(MAX);

DECLARE CUR CURSOR LOCAL FAST_FORWARD FOR
SELECT
s.session_id
FROM
sys.dm_exec_sessions s
LEFT JOIN
sys.dm_exec_requests r
ON s.session_id = r.session_id
WHERE
r.session_id IS NULL
AND program_name LIKE '%Microsoft SQL Server Management Studio%'
AND last_request_start_time < DATEADD(HOUR, -24, GETDATE());

OPEN CUR;

FETCH CUR INTO @SPID;

WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQL = N'KILL ' + CAST(@SPID AS VARCHAR(5));

BEGIN TRY
EXEC sp_executesql @SQL;
PRINT 'SUCCESS: ' + @SQL;
END TRY
BEGIN CATCH
PRINT 'ERROR: ' + @SQL;
END CATCH;

FETCH CUR INTO @SPID;
END;

CLOSE CUR;

DEALLOCATE CUR;





The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search