删除用户时,提示正在连接的用户无法删除。那就强制断开连接吧!
错误信息
SQL> drop user usr_sg cascade;
drop user usr_sg cascade
*
ERROR at line 1:
ORA-01940: cannot drop a user that is currently connected
解决办法
查询占用情况
比如查询usr_sg用户的连接情况,这里你要换成大写的USR_SG
语句:
SELECT SID, SERIAL#,USERNAME FROM V$SESSION WHERE USERNAME = 'USR_SG';
执行结果:
SQL> SELECT SID, SERIAL#,USERNAME FROM V$SESSION WHERE USERNAME = 'USR_SG';
SID SERIAL# USERNAME
---------- ---------- ------------------------------
58 1471 USR_SG
59 4039 USR_SG
强制kill会话
后面的参数是SID+SERIAL#
ALTER SYSTEM KILL SESSION '58,1471';
ALTER SYSTEM KILL SESSION '59,4039';
如果觉得,手动拼太麻烦了,可以执行下面语句自动生成sql脚本。
命令:
SELECT 'ALTER SYSTEM KILL SESSION '''||SID||','||SERIAL#||''';' FROM V$SESSION WHERE USERNAME = 'USR_SG';
执行结果:
SQL> SELECT 'ALTER SYSTEM KILL SESSION '''||SID||','||SERIAL#||''';' FROM V$SESSION WHERE USERNAME = 'USR_SG';
'ALTERSYSTEMKILLSESSION'''||SID||','||SERIAL#||''';'
--------------------------------------------------------------------------------
ALTER SYSTEM KILL SESSION '58,1471';
ALTER SYSTEM KILL SESSION '59,4039';
杀进程执行结果如下:
SQL> ALTER SYSTEM KILL SESSION '58,1471';
System altered.
SQL> ALTER SYSTEM KILL SESSION '59,4039';
System altered.
这里我们在来删除用户试试(所有的用户数据会丢失请谨慎操作)。
执行结果:
SQL> drop user usr_sg cascade;
User dropped.
参考
https://blog.csdn.net/Hello_World_QWP/article/details/78687886