Dropping user in oralce by killing active sessions

Posted on October 29, 2009. Filed under: Oracle | Tags: , |

Today when i was looking through the metalink , i found this script, which seems to be a useful script for DBA’s.
The following PL/SQL script is intended to be used for immediate removal of a user from the database.  It kills all active sessions and then drops the user from the database. I have tested in 10.2, it works fine

CREATE OR REPLACE PROCEDURE kill_drop_user (in_username IN VARCHAR2,
sleep_interval IN NUMBER DEFAULT 10)
AS
PRAGMA AUTONOMOUS_TRANSACTION;
cannot_drop_user EXCEPTION;
PRAGMA EXCEPTION_INIT(cannot_drop_user, -1940);
user_count    NUMBER := -1;
BEGIN
SELECT count(*) INTO user_count FROM dba_users WHERE username = in_username;
IF user_count = 0 THEN
DBMS_OUTPUT.PUT_LINE(‘User ‘ || in_username || ‘ does not exist.’);
RETURN;
END IF;
FOR i IN (SELECT sid, serial# FROM v$session WHERE username = in_username) LOOP
EXECUTE IMMEDIATE ‘alter system kill session ‘ || ”” || i.sid || ‘,’ || i.serial# || ”’ immediate’;
DBMS_OUTPUT.PUT_LINE(‘Killing user ‘ || i.sid || ‘, ‘ || i.serial#);
END LOOP;
LOOP
BEGIN
DBMS_OUTPUT.PUT_LINE(‘Attempting to drop user ‘ || in_username || ‘…’);
EXECUTE IMMEDIATE ‘DROP USER ‘ || in_username || ‘ CASCADE’;
EXIT WHEN SQLCODE <> -1940;
EXCEPTION
WHEN cannot_drop_user THEN
–DBMS_OUTPUT.PUT_LINE(SQLERRM);
DBMS_OUTPUT.PUT_LINE(‘Waiting ‘ || sleep_interval || ‘ seconds for resource clean-up…’);
DBMS_LOCK.SLEEP(sleep_interval);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(‘Inner: ‘ || SQLERRM);
END;
END LOOP;
DBMS_OUTPUT.PUT_LINE(‘Exiting loop with SQLCODE: ‘ || SQLCODE);
DBMS_OUTPUT.PUT_LINE(‘User ‘ || in_username || ‘ has been dropped.’);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(‘Outer: ‘ || SQLERRM);
END;
/

Configuring the Sample Code

This script is intended to be run by the SYS user of the database.
Running the Sample Code

1. Copy the sample code below into a file named kill_drop_user.sql.
2. Open SQL*Plus and connect as user SYS to your database

SQL> CONNECT sys/change_on_install@orcl AS SYSDBA

3. Create a user called TEST with password TEST

SQL> GRANT connect, resource TO test IDENTIFIED BY test;

4. Create the procedure kill_drop_user

SQL> @”C:\scripts\kill_drop_user.sql”

5. Open three (3) SQL*Plus sessions and connect as user TEST.
6. Execute the PL/SQL script

SQL> SET serveroutput ON size 1000000
SQL> SET timing ON
SQL> EXEC kill_drop_user(‘TEST’);

The above script may be a good help for you.

Partho Konar


Read Full Post | Make a Comment ( None so far )

Recently on Parthokonar's Blog...

Cloning An Oracle Database in Windows

Posted on October 28, 2009. Filed under: Oracle | Tags: , , |

Creating a flat file from a table

Posted on September 18, 2009. Filed under: Oracle | Tags: , |

parameters affect database tuning

Posted on September 9, 2009. Filed under: Oracle | Tags: |

Latches and Enqueues

Posted on September 1, 2009. Filed under: Oracle | Tags: , , |

Blocking Transaction

Posted on August 31, 2009. Filed under: Oracle | Tags: |

How to remove a parameter from spfile

Posted on August 26, 2009. Filed under: Oracle | Tags: , |

Dropping a database in Oracle.

Posted on August 26, 2009. Filed under: Oracle | Tags: , |

Archive Log Full in Oracle

Posted on August 26, 2009. Filed under: Oracle | Tags: , , , |

SCRIPT FOR CAPTURING ROLE OR SYS PRIVS

Posted on August 14, 2009. Filed under: Oracle Scripts |

Simple Auditing for illegal Attempts to Break a Username/Password

Posted on August 14, 2009. Filed under: Unix | Tags: , , |

  • Calender

    November 2009
    M T W T F S S
    « Oct    
     1
    2345678
    9101112131415
    16171819202122
    23242526272829
    30  

Liked it here?
Why not try sites on the blogroll...