#!/usr/bin/ksh
# whatsegment_space ()
#------------------------------------------------------------------------------#
#    Module Name:  whatsegment_space  (c) Michael Barone                       #
#                                                                              #
#    Purpose:      Report on the Owner-Segments / Space-Used.                  #
#                                                                              #
#    Maintenance                                                               #
#    Date          Author        Description                                   #
#    -----------   ------------  --------------------------------------------  #
#    10-Oct-2009   M.Barone      Module design/creation                        #
#------------------------------------------------------------------------------#
{
        DIRNAM=$(dirname $0)
        ORASID=$(echo $CONTEXT_NAME | cut -f1 -d'_')
        HNAME=$(uname -n)
                
        clear
        echo
        echo  " #--------------------------------------------------------#"
        echo  " # whatsegment_space: Oracle Apps Database Owner-Segments #"
        echo  " #--------------------------------------------------------#"
        echo

        #------------------------------------------------------------------#
        #  Trap:  Set CNTL-C CNTL-D CNTL-\ Before Password Protection      #
        #------------------------------------------------------------------#
        trap        "stty echo; return"     2
        trap        "stty echo; return"     3
        stty -echo

        read PASSWD?" Please Enter the Oracle [APPS] Passwd: " 
            
        stty echo
        echo
        #------------------------------------------------------------------#
        #  Trap:  Reset CNTL-C CNTL-D CNTL-\ After Password Protection     #
        #------------------------------------------------------------------#
        trap        2
        trap        3

        read SEGOWN?"        Enter the [Segment-Owner] Ex: APPLSYS: " 
        read SEGNME?"        Enter the [Segment-Name]: Ex. FND_CONC: " 

        clear
        ORANAM=${ORASID}

#------------------------------------------------------------------------------#
#  SQL*Plus ("Here-Document")                                                  #
#------------------------------------------------------------------------------#
sqlplus  -s  /nolog  <<-END_FILE

        CONNECT  APPS/$PASSWD

        clear   BREAKS
        clear   BUFFER
        clear   COLUMNS
        clear   COMPUTES
        clear   SCREEN
        clear   SQL
        clear   TIMING

        set     feedback        off
        set     flush           on
        set     heading         on
        set     pause           off
        set     space           1
        set     termout         on
        set     verify          on
        set     linesize        80
        set     pagesize        999

        SPOOL /tmp/whatsegment_space_$$.txt

        COLUMN  datevalue       NOPRINT NEW_VALUE       DATEVAR
        COLUMN  timevalue       NOPRINT NEW_VALUE       TIMEVAR

        COLUMN  c_stop       NOPRINT 
        COLUMN  c_time       NOPRINT

        BREAK   ON  tablespace_name     skip 2  ON REPORT

        COMPUTE SUM LABEL 'Total'  OF  bytes    ON tablespace_name
        COMPUTE SUM LABEL 'Total'  OF  bytes    ON REPORT

        COLUMN  owner                   FORMAT A10
        COLUMN  tablespace_name         FORMAT A06
        COLUMN  segment_name            FORMAT A38
        COLUMN  segment_type            FORMAT A06
        COLUMN  bytes                   FORMAT 999,999,999,999

        TTITLE  CENTER  'Oracle SegmentSpace Used by SchemaName'        -
                LEFT    DATEVAR                                         -
                RIGHT   'Page:'         FORMAT 999 SQL.pno      skip 1  -
                CENTER  'whatsegment_space -- Sorted by Tablespace'     -
                LEFT    TIMEVAR                                 skip 2 

        SELECT  TO_CHAR(sysdate, 'DD-MON-YY')   datevalue,
                TO_CHAR(sysdate, 'HH24:MI:SS')  timevalue,
                owner, tablespace_name,
                segment_name, segment_type,
                bytes
         FROM   dba_segments
        WHERE   owner           =  UPPER('${SEGOWN}')
          AND   segment_name    LIKE  UPPER('%${SEGNME}%')
        ORDER   BY owner, tablespace_name, segment_name, segment_type, bytes;

EXIT;

END_FILE
print
print "Please see /tmp/whatsegment_space_$$.txt"
print
}

