forked from carlos-sierra/cscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cs_kill_sid.sql
executable file
·59 lines (59 loc) · 1.92 KB
/
cs_kill_sid.sql
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
----------------------------------------------------------------------------------------
--
-- File name: cs_kill_sid.sql
--
-- Purpose: Kill one User Session
--
-- Author: Carlos Sierra
--
-- Version: 2020/12/06
--
-- Usage: Execute connected to CDB or PDB.
--
-- Example: $ sqlplus / as sysdba
-- SQL> @cs_kill_sid.sql
--
-- Notes: Developed and tested on 12.1.0.2.
--
---------------------------------------------------------------------------------------
--
PRO
PRO 1. Enter SID:
DEF sid = '&1.';
UNDEF 1;
--
VAR sid VARCHAR2(13);
EXEC :sid := SUBSTR('&&sid.'||',', 1, INSTR('&&sid.'||',', ',') - 1);
--
SET SERVEROUT ON;
DECLARE
l_sid_serial DBMS_UTILITY.name_array; -- e.g.: 123,90536 (associative array type)
l_statament VARCHAR2(32767);
session_marked_for_kill EXCEPTION;
PRAGMA EXCEPTION_INIT(session_marked_for_kill, -00031); -- ORA-00031: session marked for kill
session_does_not_exist EXCEPTION;
PRAGMA EXCEPTION_INIT(session_does_not_exist, -00030); -- ORA-00030: User session ID does not exist.
BEGIN
SELECT sid||','||serial#
BULK COLLECT INTO l_sid_serial
FROM v$session
WHERE type = 'USER' AND sid = :sid AND sid <> SYS_CONTEXT('USERENV', 'SID');
--
IF l_sid_serial.LAST >= l_sid_serial.FIRST THEN -- session found
SYS.DBMS_SYSTEM.KSDWRT(dest => 2, tst => TO_CHAR(SYSTIMESTAMP, 'YYYY-MM-DD"T"HH24:MI:SS.FF3')||' cs_kill_sid: killing sid '||:sid);
FOR i IN l_sid_serial.FIRST .. l_sid_serial.LAST
LOOP
l_statament := 'ALTER SYSTEM DISCONNECT SESSION '''||l_sid_serial(i)||''' IMMEDIATE';
DBMS_OUTPUT.put_line(l_statament||';');
BEGIN
EXECUTE IMMEDIATE l_statament;
EXCEPTION
WHEN session_marked_for_kill OR session_does_not_exist THEN NULL;
END;
END LOOP;
SYS.DBMS_OUTPUT.put_line((l_sid_serial.LAST - l_sid_serial.FIRST + 1)||' session killed!');
ELSE
DBMS_OUTPUT.put_line('no session found');
END IF;
END;
/