Oracle

Kill user session in Oracle

If you need to kill all the sessions for a particular user you can try using the following script.
It looks for all connections by AUSER (line 6) and then kills them.
SET SERVEROUTPUT ON SIZE 1000000
BEGIN
FOR usersession IN (SELECT s.USERNAME, s.OSUSER, s.STATUS, s.SID, s.SERIAL#
FROM   v$process p,v$session s
WHERE  p.addr = s.paddr
AND    s.username = 'AUSER')
LOOP
BEGIN
IF usersession.STATUS = 'ACTIVE' THEN
EXECUTE IMMEDIATE 'ALTER SYSTEM KILL SESSION ' || '''' || usersession.sid || ',' || usersession.serial# || '''' || 'IMMEDIATE';
ELSIF usersession.STATUS = 'INACTIVE' THEN
EXECUTE IMMEDIATE 'ALTER SYSTEM KILL SESSION ' || '''' || usersession.sid || ',' || usersession.serial# || '''' || 'IMMEDIATE';
ElSE
DBMS_OUTPUT.put_line (usersession.sid || ' : ' || usersession.serial# || ' : ' || usersession.OSUSER ||  ' : ' || usersession.STATUS);
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.put_line(usersession.sid || ' : ' || usersession.serial#  ||  ' : ' || usersession.OSUSER);
END;
END LOOP;
END;