ORA-01940: cannot drop a user that is currently connected
A user that has open sessions cannot be dropped, in such a case, the following statement would throw the error meesage ORA-01940: cannot drop a user that is currently connected:
select
'alter system kill session ''' || sid || ',' || serial# || ''' immediate;' stmt
from
gv$session
where
username ='K';
The following anonymous PL/SQL block uses execute immediate to execute the statements created with the previous select statement.
In addition, it uses the force option available with 23c to immediatly terminate all sessions of a given user:
begin
for r in (
select
'alter system kill session ''' || sid || ',' || serial# ||
case when dbms_db_version.version >= 23 then ' force' end ||
'''' stmt
from
gv$session
where
username ='GTFS_ZURICH') loop
execute immediate r.stmt;
end loop;
end;
/