@@header

/*
*
*  Author     : Vishal Gupta
*  Purpose    : Add hints to an existing SQL Profile
*  Parameters : 1 - SQL Profile Name
*               2 - Hint to be added to SQL Profile
*          
*
*  Revision History:
*  ===================
*  Date       Author        Description
*  ---------  ------------  -----------------------------------------
*  16-Mar-15  Vishal Gupta  Created
*
*/

UNDEFINE SQLPROFILENAME
UNDEFINE OUTLINE_HINT

DEFINE SQLPROFILENAME='&&1' 
-- Use single quotes to initialize this variable, as OUTLINE HINTs themselves have double quotes.
DEFINE OUTLINE_HINT='&&2' 

set serveroutput on
declare
     ar_profile_hints   sys.sqlprof_attr;
     cl_profile_hints   clob;
     cl_profile_hints2  clob;
     cl_sql_text        clob;
     buffer             VARCHAR2(4000);
     l_profile_name     VARCHAR2(30) := '&&SQLPROFILENAME'  ;
     l_category         dba_sql_profiles.category%TYPE;
     l_description      dba_sql_profiles.description%TYPE;
begin
    
    
    cl_profile_hints := '<outline_data>' ;
    
    SELECT XMLQuery( '/outline_data/hint'  passing xmltype(sd.comp_data) RETURNING CONTENT ).getClobVal() xmlval
         , sp.sql_text
         , sp.category
         , sp.description
     INTO cl_profile_hints2
        , cl_sql_text
        , l_category
        , l_description
     FROM dba_sql_profiles sp
     JOIN sys.sqlobj$ so ON so.signature = sp.signature AND so.category  = sp.category AND so.obj_type = 1 /* 1 = SQLProfile, 2=SQL Plan  */ 
     JOIN sys.sqlobj$auxdata ad ON ad.signature = so.signature AND ad.category  = so.category  AND ad.obj_type = 1  /* 1 = SQLProfile, 2=SQL Plan  */
	    JOIN sys.sqlobj$data sd ON sd.signature = so.signature AND sd.category  = so.category  AND sd.obj_type = 1  /* 1 = SQLProfile, 2=SQL Plan  */
     WHERE sp.name = l_profile_name
     ;

     dbms_lob.append( cl_profile_hints,cl_profile_hints2);
     
     dbms_lob.append( cl_profile_hints,'<hint><![CDATA[' || '&&OUTLINE_HINT' || ']]></hint>' );
     dbms_lob.append( cl_profile_hints,'</outline_data>');
     
     --dbms_output.put_line(cl_profile_hints);
     
     BEGIN
        DBMS_SQLTUNE.ALTER_SQL_PROFILE (name=> l_profile_name , attribute_name =>'CATEGORY', value =>'DONOTUSE');
     EXCEPTION
          WHEN others THEN
                 null;          
     END;

     DBMS_SQLTUNE.DROP_SQL_PROFILE (name=> 'TEMP_SQL_PROFILE' , ignore => TRUE);
     DBMS_SQLTUNE.ALTER_SQL_PROFILE (name=> l_profile_name , attribute_name =>'NAME', value => 'TEMP_SQL_PROFILE');

     dbms_sqltune.import_sql_profile(  sql_text    => cl_sql_text
                                     , profile_xml => cl_profile_hints
                                     , category    => l_category
                                     , name        => l_profile_name 
                                     , description => l_description
                                     , force_match => TRUE
                                     , validate    => TRUE
                                     , replace     => FALSE
      );
     
      
     DBMS_SQLTUNE.DROP_SQL_PROFILE (name=> 'TEMP_SQL_PROFILE' , ignore => TRUE);
     
     dbms_output.put_line('.');
     dbms_output.put_line('Hint added to SQL Profile ' ||l_profile_name );
     dbms_output.put_line('. ');
     dbms_output.put_line('Use following to view hints in this SQL profile : ');
     dbms_output.put_line('.      @sql_profile_hints ' ||l_profile_name );

end;
/

@@footer
