In this Document
Goal
Solution
References
Applies to:
PL/SQL – Version: 8.1.7.4 to 10.1.0.2
Information in this document applies to any platform.
Goal
This article presents one suggested way of killing a session using an “ALTER SYSTEM KILL SESSION ‘sid,serial#’ ” statement in PL/SQL dynamically where the sid and serial# are values being passed in determined at runtime via additional SQL commands.
Solution
The Sid and serial values can be obtained from associated queries against the V$SESSION view.
For example, if a single user SCOTT was logged in and that is the session to kill, the following SQL can be used to obtain the required information:
SQL> select sid, serial# from v$session where username = ‘SCOTT’;
SID SERIAL#
—- ———-
26 10654
The SID (26) and Serial# (10654) are used within the following anonymous block to kill SCOTT’s session.
declare
sid number := 26;
serial number := 10654;
str varchar2(1000);
killstr varchar2(20);
begin
killstr := sid||’,’||serial;
str:=’alter system kill session ”’||killstr||””;
dbms_output.put_line(‘Kill String is ‘||killstr);
execute immediate str;
end;
/