SELECT s.username,s.program,concat(concat(' ALTER SYSTEM KILL SESSION ''',concat(concat( s.sid,',' ),s.serial#) ),''' IMMEDIATE;') as sql FROM   gv$session s JOIN gv$process p ON p.addr = s.paddr AND p.inst_id = s.inst_id WHERE  s.status='INACTIVE' and SECONDS_IN_WAIT > 60*15 and ( lower(NVL(s.program,'')) in ( '','w3wp.exe') or s.program is null ) order by s.logon_time desc
;
begin
 for i in (SELECT concat(concat(' ALTER SYSTEM KILL SESSION ''',concat(concat( s.sid,',' ),s.serial#) ),''' IMMEDIATE') as sql FROM   gv$session s JOIN gv$process p ON p.addr = s.paddr AND p.inst_id = s.inst_id WHERE  s.status='INACTIVE' and SECONDS_IN_WAIT > 60*15 and ( lower(NVL(s.program,'')) in ( '','w3wp.exe') or s.program is null ) order by s.logon_time desc) LOOP
  execute immediate i."SQL"||' ';
 end loop;
end;
/


--Kill Session in Amazon Oracle RDS
SELECT  s.username,s.status,'exec rdsadmin.rdsadmin_util.kill('''|| s.sid ||''',''' || s.serial# || ''');' sql  FROM   gv$session s JOIN gv$process p ON p.addr = s.paddr AND p.inst_id = s.inst_id WHERE  s.status='INACTIVE' and SECONDS_IN_WAIT > 60*10 order by s.logon_time desc ;
begin
 for i in (SELECT 'exec rdsadmin.rdsadmin_util.kill('''|| s.sid ||''',''' || s.serial# || ''')'  sql  FROM   gv$session s JOIN gv$process p ON p.addr = s.paddr AND p.inst_id = s.inst_id WHERE  s.status='INACTIVE' and SECONDS_IN_WAIT > 60*10  order by s.logon_time desc) LOOP
  execute immediate i."SQL"||' ';
 end loop;
end;
/
