T-SQL: Kill all inactive sessions from specific user

bschelst/ July 17, 2020/ SQL-Server/ 0 comments

DECLARE @sql NVARCHAR(MAX) = N'';

SELECT @sql += N'KILL ' + CONVERT(VARCHAR(11), session_id) + N';'
FROM sys.dm_exec_sessions
  --WHERE [program_name] = N'program_name'
  where login_name = N'my_user'
  AND last_request_end_time < DATEADD(HOUR, -1, SYSDATETIME());

select  @sql;
ExEC sys.sp_executesql @sql;
Share this Post

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
*
*