@@header

/*
*
*  Author     : Vishal Gupta
*  Purpose    : Display SQL Binds from SQLArea
*  Version    : 10.2 and above
*  Parameters : 1 - SQL_ID
*               2 - Hours           (Default 72 hours)
*               
*
*  Revision History:
*  ===================
*  Date       Author        Description
*  ---------  ------------  -----------------------------------------
*  27-Aug-15  Vishal Gupta  Created
*
*/


/************************************
*  INPUT PARAMETERS
************************************/
UNDEFINE sql_id
UNDEFINE hours

DEFINE sql_id="&&1"
DEFINE hours="&&3"

COLUMN  _force_matching_signature NEW_VALUE  force_matching_signature   NOPRINT
COLUMN  _hours                    NEW_VALUE  HOURS                     NOPRINT

set term off
SELECT DECODE('&&sql_id','%','%','')                            "_force_matching_signature"
     , DECODE('&&hours','','72','&&hours')                      "_hours"
FROM DUAL;
set term on


/************************************
*  MAIN Section
************************************/

DEFINE force_matching_signature=""
COLUMN  _force_matching_signature          NEW_VALUE  force_matching_signature            NOPRINT

set term off
set numf 99999999999999999999999
select to_char(sql.force_matching_signature) "_force_matching_signature" 
  from gv$sqlstats sql
 where sql.sql_id = '&&sql_id' 
   and rownum <= 1;
set term on

PROMPT **************************************************************
PROMPT *  SQL Bind Values from SQL Area
PROMPT *  (Force Matching Signature = '&&force_matching_signature')
PROMPT * 
PROMPT * Input Parameters 
PROMPT *    - SQL Id                   = '&&sql_id'
PROMPT *    - Hours                    = '&&hours'
PROMPT **************************************************************

COLUMN first_load_time           HEADING "First Load Time"     FORMAT a20
COLUMN last_active_time          HEADING "Last Active Time"    FORMAT a20
COLUMN instance_number           HEADING "I#"                  FORMAT 99
COLUMN SQL_ID                    HEADING "SQLId"               FORMAT a13
COLUMN plan_hash_value           HEADING "Plan|Hash|Value"     FORMAT 999999999999  
COLUMN bind_values               HEADING "Bind Values"         FORMAT a100
COLUMN bind_datatype             HEADING "Bind DataType"       FORMAT a80

 
SELECT TO_CHAR(TO_DATE(sql.first_load_time,'YYYY-MM-DD/HH24:MI:SS'),'DD-MON-YYYY HH24:MI:SS')  first_load_time
       , TO_CHAR(ss.last_active_time,'DD-MON-YYYY HH24:MI:SS')  last_active_time
       , ss.inst_id instance_number
       , ss.sql_id               
       , ss.plan_hash_value
       , (select listagg(NVL(name,':B' || position) 
                  ||'[' 
                  || datatype_string || NVL2(scale
                                                  ,'(' || scale || NVL2(precision
                                                                       ,','|| precision
                                                                       , ''
                                                                       ) || ')' 
                                                  ,'' 
                                                  ) 
                  || ']'
                  ||'=' 
                  || CASE 
                     WHEN datatype_string <> 'TIMESTAMP' THEN NVL(value_string,'NULL') 
                     ELSE TO_CHAR(ANYDATA.ACCESStimestamp(VALUE_ANYDATA))
                     END
                    ,','
               ) WITHIN GROUP (order by position)   
          from table(dbms_sqltune.extract_binds(sql.BIND_DATA)) 
          )  bind_values
/*
          , (select listagg(NVL(name,':B' || position) || '=' 
                           || datatype_string || NVL2(scale,'(' || scale || NVL2(precision,','|| precision, '') || ')' ,'' )
                         ,','
                        ) WITHIN GROUP (order by position)   
          from table(dbms_sqltune.extract_binds(sql.BIND_DATA)) 
          )  bind_datatype
*/
  from gv$sqlstats   ss 
     , gv$sqlarea    sql
where ss.inst_id         = sql.inst_id (+)
  AND ss.sql_id          = sql.sql_id (+)
  AND ss.plan_hash_value = sql.plan_hash_value (+)
  AND ss.sql_id LIKE CASE ss.force_matching_signature 
                  WHEN 0 THEN '&&sql_id'
                  ELSE '%'
                  END
  AND ss.force_matching_signature = '&&force_matching_signature'
  AND ss.last_active_time  > sysdate - (&&hours/24)
order by ss.last_active_time 
       , ss.inst_id
;



@@footer
