Session information on known SID basis


set
echo off
set
linesize 132
set
verify off
set
feedback off
set
serveroutput on;
declare
  SID             number        := 0 ;
  SERIAL          number        := 0 ;
  username        varchar(20)   := ”;
  Status        
 varchar(8)    := ”;
  machine         varchar(10)   := ”;
  terminal        varchar(25)   := ”;
  program         varchar(30)   := ”;
  Module          varchar(30)   := ”;
  Action          varchar(20)   := ”;
  sql_hash_value  number       
:= 0 ;
  logontime       varchar(30)   := ”;
  last_call_et    number        := 0 ;
  proc            number        := 0 ;
  spid            number        := 0 ;
  event           varchar(30)   := ”;
  state           varchar(30)   := ”;
  sql_text        varchar(1000) := ”;
cursor
cur1 is
select
    a.sid 
sid,
    a.serial# 
serial,
    a.username username,
    a.status status ,
    a.machine machine,
    a.terminal terminal,
    a.program program,
    a.module module,
    a.action action,
    a.sql_hash_value sql_hash_value,
    to_char(a.logon_time,’DD-Mon-YYYY
HH:MI:SS’) logontime,
    a.last_call_et last_call_et,
    a.process proc,
    b.spid spid,
    sw.event event,
    sw.state state
  from gv$session a, gv$process b,  gv$session_wait  sw
  where a.paddr=b.addr
    and a.inst_id=b.inst_id
    and a.sid =’&sid’
    and a.inst_id=sw.inst_id
    and a.sid=sw.sid;
begin
 
DBMS_OUTPUT.PUT_LINE(‘—————————————————————–‘);
  DBMS_OUTPUT.PUT_LINE(‘ Database session detail
for the shadow process ‘);
 
DBMS_OUTPUT.PUT_LINE(‘—————————————————————–‘);
  for m in cur1 loop
    DBMS_OUTPUT.ENABLE(50000);
    DBMS_OUTPUT.PUT_LINE(‘  ‘);
    DBMS_OUTPUT.PUT_LINE(‘SID………… : ‘ ||
m.sid);
    DBMS_OUTPUT.PUT_LINE(‘SERIAL#…….. : ‘
|| m.serial);
    DBMS_OUTPUT.PUT_LINE(‘USERNAME……. : ‘
|| m.username);
    DBMS_OUTPUT.PUT_LINE(‘STATUS……… : ‘
|| m.status);
    DBMS_OUTPUT.PUT_LINE(‘Machine…….. : ‘
|| m.machine);
    DBMS_OUTPUT.PUT_LINE(‘Terminal……. : ‘ ||
m.terminal);
    DBMS_OUTPUT.PUT_LINE(‘Program…….. : ‘
|| m.program);
    DBMS_OUTPUT.PUT_LINE(‘Module……… : ‘
|| m.module);
    DBMS_OUTPUT.PUT_LINE(‘Action……… : ‘
|| m.action);
    DBMS_OUTPUT.PUT_LINE(‘SQL Hash Value. : ‘
|| m.sql_hash_value);
    DBMS_OUTPUT.PUT_LINE(‘Logon Time….. : ‘
|| m.logontime);
    DBMS_OUTPUT.PUT_LINE(‘Last Call Et… : ‘
|| m.last_call_et);
    DBMS_OUTPUT.PUT_LINE(‘Process ID….. : ‘
|| m.proc);
    DBMS_OUTPUT.PUT_LINE(‘SPID……….. : ‘
|| m.spid);
    DBMS_OUTPUT.PUT_LINE(‘Session Waiting for
event:’||m.event);
    DBMS_OUTPUT.PUT_LINE(‘Session state
………..:’||m.state);
    for rec in ( select distinct(sql_text)
sql_text from v$session s,v$sql v where
       
s.sql_hash_value=v.hash_value and s.sql_address=v.address and
s.sid=m.sid)
    loop
      dbms_output.put_line(substr(‘SQL_TEXT
is……….:’||rec.sql_text,1,255));
    end loop;
   
DBMS_OUTPUT.PUT_LINE(‘——————————————————————–‘);
    DBMS_OUTPUT.PUT_LINE(‘ ‘);
  end loop;
end;
/

—       dbms_output.put_line(SUBSTR(‘SQL_TEXT
is……….:’||rec.sql_text,1,255));

Another version

set
echo off
set
linesize 132
set
verify off
set
feedback off
set
serveroutput on;
declare
  SID             number        := 0 ;
  SERIAL          number        := 0 ;
  username        varchar(20)   := ”;
  Status          varchar(8)    := ”;
  machine         varchar(10)   := ”;
  terminal        varchar(25)   := ”;
  program         varchar(30)   := ”;
  Module          varchar(30)   := ”;
  Action          varchar(20)   := ”;
  sql_hash_value  number       
:= 0 ;
  logontime       varchar(30)   := ”;
  last_call_et    number        := 0 ;
  proc            number        := 0 ;
  spid            number        := 0 ;
  event           varchar(30)   := ”;
  state           varchar(30)   := ”;
  sql_text        varchar(1000) := ”;
cursor
cur1 is
select
    a.sid 
sid,
    a.serial# 
serial,
    a.username username,
    a.status status ,
    a.machine machine,
    a.terminal terminal,
    a.program program,
    a.module module,
    a.action action,
    a.sql_hash_value sql_hash_value,
    to_char(a.logon_time,’DD-Mon-YYYY
HH:MI:SS’) logontime,
    a.last_call_et last_call_et,
    a.process proc,
    b.spid spid,
    b.event event,
    b.state state
  from gv$session a, gv$process b,  gv$session_wait  sw
  where a.paddr=b.addr
    and a.inst_id=b.inst_id
    and b.spid=’&1′
    and a.inst_id=sw.inst_id
    and a.sid=sw.sid;
begin
 
DBMS_OUTPUT.PUT_LINE(‘—————————————————————–‘);
  DBMS_OUTPUT.PUT_LINE(‘ Database session
detail for the shadow process ‘);
 
DBMS_OUTPUT.PUT_LINE(‘—————————————————————–‘);
  for m in cur1 loop
    DBMS_OUTPUT.ENABLE(50000);
    DBMS_OUTPUT.PUT_LINE(‘  ‘);
    DBMS_OUTPUT.PUT_LINE(‘SID………… : ‘
|| m.sid);
    DBMS_OUTPUT.PUT_LINE(‘SERIAL#…….. : ‘
|| m.serial);
    DBMS_OUTPUT.PUT_LINE(‘USERNAME……. : ‘ ||
m.username);
    DBMS_OUTPUT.PUT_LINE(‘STATUS……… : ‘
|| m.status);
    DBMS_OUTPUT.PUT_LINE(‘Machine…….. : ‘
|| m.machine);
    DBMS_OUTPUT.PUT_LINE(‘Terminal……. : ‘
|| m.terminal);
    DBMS_OUTPUT.PUT_LINE(‘Program…….. : ‘
|| m.program);
    DBMS_OUTPUT.PUT_LINE(‘Module……… : ‘
|| m.module);
    DBMS_OUTPUT.PUT_LINE(‘Action……… : ‘
|| m.action);
    DBMS_OUTPUT.PUT_LINE(‘SQL Hash Value. : ‘
|| m.sql_hash_value);
    DBMS_OUTPUT.PUT_LINE(‘Logon Time….. : ‘
|| m.logontime);
   
DBMS_OUTPUT.PUT_LINE(‘Last Call Et… : ‘ || m.last_call_et);
    DBMS_OUTPUT.PUT_LINE(‘Process ID….. : ‘
|| m.proc);
    DBMS_OUTPUT.PUT_LINE(‘SPID……….. : ‘
|| m.spid);
    DBMS_OUTPUT.PUT_LINE(‘Session Waiting for
event:’||m.event);
    DBMS_OUTPUT.PUT_LINE(‘Session state
………..:’||m.state);
   
DBMS_OUTPUT.PUT_LINE(‘——————————————————————–‘);
    DBMS_OUTPUT.PUT_LINE(‘ ‘);
  end loop;
end;
/
Recent Posts