/*
 * Title  : rmn_ses_info.sql
 * Author : EXADIME LLC
 * Created: 05-Apr-2021
 * Updated: 05-Apr-2021
 * Release: 1.0.0
 * Purpose: Shows RMAN session details.
 * History: N/A
 */

start oracle.ini

set echo off
set feedback off
set serverout on
set termout on

set linesize 250
set pagesize 0
set pause off
set long 80

set heading on
set headsep on
set underline on
set colsep " "

set define on
set verify off
set escape off
set embedded on

col "#"			format 9999
col "Node"              format 9999
col "SID"               format 99999
col "Serial#"		format 9999999
col "User Name"		format a30
col "Logon Time"        format a20
col "Machine"		format a20
col "Program"		format a25
col "SQL Id"            format a13 justify right
col "SQL Trace"		format a10
col "PID"               format 99999
col "SPID"              format a10
col "Trace ID"		format a10
col "Status"            format a10
col "State"             format a20
col "Event"             format a30

accept p_order char prompt "Enter order by column {1|2|3|5|6|13} [asc|desc] " default 2,3,13

spool &&rpt_dir/rmn_ses_info.rpt

select
	row_number() over(order by &&p_order)         "#",
	inst_id                                       "Node",
	sid                                           "SID",
	serial#                                       "Serial#",
	username                                      "User Name",
	to_char(logon_time,'dd-mon-yyyy hh:mi:ss')    "Logon Time",
	machine                                       "Machine",
	substr(program,1,25)                          "Program",
	nvl(sql_id,'-------------')                   "SQL Id",
	pid                                           "PID",
	spid                                          "SPID",
	sql_trace                                     "SQL Trace",
	traceid                                       "Trace ID",
	status                                        "Status",
	state					      "State",
	substr(event,1,30)                            "Event"
from (
	select
		s.inst_id,
		s.sid,
		s.serial#,
		s.username,
		s.logon_time,
		s.machine,
		s.program,
		s.sql_id,
		p.pid,
		p.spid,
		s.sql_trace,
		p.traceid,
		s.status,
		s.state,
		s.event
	from
		gv$session s,
		v$process p,
		v$rman_status r
	where
		s.paddr=p.addr(+)
	and	s.username is not null
	and	p.background is null
        and     s.sid=r.sid
        and     r.status='RUNNING'
        and     r.sid>0
)
order by
	&&p_order
/

spool off;

clear columns

set embedded off
set escape off
set verify on
set define on

set colsep " "
set underline on
set headsep on
set heading on

set pause off
set pagesize 24
set linesize 80
set long 80

set termout on
set serverout off
set feedback on
set echo off
