Repository navigation
Expand file tree
/
Copy pathSQL_KILLNONAVSESSION.sql
More file actions
109 lines (90 loc) · 2.63 KB
/
Copy pathSQL_KILLNONAVSESSION.sql
File metadata and controls
109 lines (90 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
USE [master]
GO
/****** Object: StoredProcedure [dbo].[SP_SQL_KillNoNavSession] Script Date: 18/04/2018 15.53.50 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Roberto Stefantti
-- Create date: <13/01/2017>
-- Description: KILL SLEEPING NO NAV SESSIONS
-- =============================================
CREATE PROCEDURE [dbo].[SP_SQL_KillNoNavSession]
AS
BEGIN
----------------------------------------------------------------------------
-- Variable
----------------------------------------------------------------------------
DECLARE @Session TABLE
(
id INT IDENTITY(1,1) PRIMARY KEY,
[sid] int,
ltime DATETIME,
hname varchar(MAX),
uid varchar(100),
lret datetime,
etime int
);
DECLARE @NotAffectedUserList TABLE
(
id INT IDENTITY(1,1) PRIMARY KEY,
[uid] varchar(MAX)
);
DECLARE @session_time as int;
DECLARE @vSID as int;
DECLARE @command as varchar(MAX);
DECLARE @RowIndex AS INT;
DECLARE @MaxIndex AS INT;
----------------------------
set @session_time=15; -- Assign Session time in Minutes
-- Assign List of Users who "will not be affected" by this Session Kill
INSERT into @NotAffectedUserList SELECT 'sa'
INSERT into @NotAffectedUserList SELECT 'NT AUTHORITY\SERVIZIO DI RETE'
INSERT into @NotAffectedUserList SELECT 'DOMAIN\SERVICE'
INSERT into @NotAffectedUserList SELECT 'scheduler'
INSERT into @Session
SELECT
session_id as [Session ID],
login_time as [Login Time],
host_name as [Host Name],
LOGIN_NAME as [Login ID],
last_request_end_time as [Last Request End Time],
DATEDIFF(minute, last_request_end_time,getdate()) as[Elapsed Time (minutes)]
FROM
sys.dm_exec_sessions
WHERE
open_transaction_count = 0
AND host_name <> 'NAV-SERVER' COLLATE Latin1_General_CI_AS
AND host_name <> 'SQL-SERVER' COLLATE Latin1_General_CI_AS
AND host_name is not NULL
AND DATEDIFF(minute, last_request_end_time,getdate())>=@session_time
--AND client_version=4
AND login_name COLLATE Latin1_General_CI_AS not in
(
SELECT DISTINCT uid
FROM @NotAffectedUserList
)
SET @RowIndex = 1;
SELECT @MaxIndex = MAX(id) FROM @Session;
WHILE( @RowIndex <= @MaxIndex)
BEGIN
SELECT @vSID = sid
FROM @Session
WHERE id = @RowIndex;
-- String together the KILL statement
SELECT @command = 'KILL ' + CAST(@vSID AS varchar(5))
BEGIN TRY
-- Execute the final string to KILL the spids
EXEC (@command)
END TRY
BEGIN CATCH
declare @emsg varchar(500);
SELECT
@emsg=ERROR_MESSAGE();
print @emsg;
END CATCH
SET @RowIndex = @RowIndex + 1;
END;
END
GO