Created
February 8, 2013 06:42
-
-
Save oehme/4737130 to your computer and use it in GitHub Desktop.
Force drop user in Oracle database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE | |
open_count integer; | |
BEGIN | |
-- prevent any further connections | |
EXECUTE IMMEDIATE 'alter user @USERNAME account lock'; | |
--kill all sessions | |
FOR session IN (SELECT sid, serial# | |
FROM v$session | |
WHERE username = '@USERNAME') | |
LOOP | |
-- the most brutal way to kill a session | |
EXECUTE IMMEDIATE 'alter system disconnect session ''' || session.sid || ',' || session.serial# || ''' immediate'; | |
END LOOP; | |
-- killing is done in the background, so we need to wait a bit | |
LOOP | |
SELECT COUNT(*) | |
INTO open_count | |
FROM v$session WHERE username = '@USERNAME'; | |
EXIT WHEN open_count = 0; | |
dbms_lock.sleep(0.5); | |
END LOOP; | |
-- finally, it is safe to issue the drop statement | |
EXECUTE IMMEDIATE 'drop user @USERNAME cascade'; | |
END; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment