T-SQL: Kill all inactive sessions from specific user

The SQL statement below will kill all sessions from a specific user, which wasn’t active the last hour.
Of course, change ‘my_user’ with your preferred userid. If you want you can also uncomment the ‘program_name’ if you want to filter on program name.


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;