Search code examples
oracle-databasesessiondata-dictionary

How To Kill Oracle DB Sessions Who are inActive Only?


I need to Terminate(kill ) All Sessions inside Oracle Db v$session where the status is not Active
To list active and Inactive Sessions i used this statement:

SELECT sid, serial#, status FROM v$session;

I need a Statement to terminate Sessions where status != Active


Solution

  • There is Only one way to Terminate a session and it is by 'sid,serial#'
    this is a Statement for example :

    ALTER SYSTEM KILL SESSION 'sid,serial#' IMMEDIATE;
    

    So we need to go through a cycle to Perform this Statement for All 'sid,serial#' but Only when status!=active: This Code Sould Solve this Problem :

    BEGIN
      FOR sess IN (select sid,serial# from v$session where status<>'ACTIVE')
      LOOP
          EXECUTE IMMEDIATE 'alter system kill session ''' || sess.sid  || ',' 
            || sess.serial# || ''' immediate';
      END LOOP;
    END;