REM V1: SQLID
REM V2: OWNER.VM
REM V3: FLAG

set serveroutput on
set verify off

DECLARE
 P_SQL_ID VARCHAR2(100) := '&1';
 P_MV VARCHAR2(100) := '&2';
 
 v_sqltext varchar2(32000);
 v_dbid number;

 err_num          number;
 err_msg          varchar2 (255);
BEGIN
    select dbid
      into v_dbid
      from dba_hist_database_instance
     where rownum <= 1;
      
    select sql_fulltext
      into v_sqltext
      from v$sql
     where sql_id = P_SQL_ID;
    
    dbms_mview.EXPLAIN_REWRITE(v_sqltext, P_MV, '&3');
	
	commit;
EXCEPTION
 when others then
      err_num := sqlcode;
      err_msg := sqlerrm;

      rollback;
      raise_application_error(-20001, 'error general. '||err_num||'-'||err_msg);

END;
/
