You may use the following block
declare
v_command varchar2(500);
begin
for c in (
select sid, serial#, machine, module, username, client_info, status, inst_id
from gv$session v
where v.status != 'ACTIVE'
--and v.inst_id = '&i_inst_id'
order by username
)
loop
begin
v_command := 'alter system kill session ''' || c.sid || ',' ||c.serial# ||
',@'||to_char(c.inst_id)||''' immediate';
execute immediate v_command;
exception when others then dbms_output.put_line(sqlerrm);
end;
end loop;
end;
where v$session
is replaced with gv$session
dynamic performance view, and INST_ID
column collation with the instance number parameter for the case of having RAC DB
cases. i.e. even if you have a Single instance database, the above code might be used, too.