安全地删除数据库用户
SET SERVEROUT ON
CREATE OR REPLACE PROCEDURE gracefullyDropUser(v_username IN VARCHAR2) IS
l_cnt integer;
sqlStmt VARCHAR2(1000);
BEGIN
sqlStmt := 'alter user ' || v_username || ' account lock'; --锁住用户,防止其他新会话的连接
EXECUTE IMMEDIATE sqlStmt;
dbms_output.put_line(sqlStmt);
FOR x IN (SELECT * FROM v$session WHERE username = v_username) LOOP
sqlStmt := 'alter system disconnect session ''' || x.sid || ',' ||
x.serial# || ''' IMMEDIATE'; --循环disconnect每一个已存的session连接,并等待所有session被删除
EXECUTE IMMEDIATE sqlStmt;
dbms_output.put_line(sqlStmt);
END LOOP;
-- Wait until all sessions are disconnected forcely, check every 2 seconds
LOOP
SELECT COUNT(*) INTO l_cnt FROM v$session WHERE username = v_username;
EXIT WHEN l_cnt = 0;
dbms_lock.sleep(2);
dbms_output.put_line('hold on ...');
END LOOP;
sqlStmt := 'drop user ' || v_username || ' cascade'; --安全地删除数据库用户
EXECUTE IMMEDIATE sqlStmt;
dbms_output.put_line(sqlStmt);
END gracefullyDropUser;
/
execute gracefullyDropUser('AGILE');
alter user AGILE account lock
alter system disconnect session '12,97' IMMEDIATE
alter system disconnect session '20,11' IMMEDIATE
alter system disconnect session '141,118' IMMEDIATE
hold on ...
hold on ...
drop user AGILE cascade
PL/SQL procedure successfully completed.
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341