set feedback off
set serveroutput on
set pagesize 0
set ver off
set echo off
set timing off

spool /opt/oracle/product/11.1.0/db/dbs/masking2.log

set escape \
-- Script Header Section
-- ==============================================

-- functions and procedures

CREATE OR REPLACE PROCEDURE mgmt$mask_sendMsg (msg IN VARCHAR2) IS
    msg1 VARCHAR2(1020);
    len INTEGER := length(msg);
    i INTEGER := 1;
BEGIN
    dbms_output.enable (1000000);

    LOOP
      msg1 := SUBSTR (msg, i, 255);
      dbms_output.put_line (msg1);
      len := len - 255;
      i := i + 255;
    EXIT WHEN len <= 0;
    END LOOP;
END mgmt$mask_sendMsg;
/

CREATE OR REPLACE PROCEDURE mgmt$mask_errorExit (msg IN VARCHAR2) IS
BEGIN
    mgmt$mask_sendMsg (msg);
    mgmt$mask_sendMsg ('errorExit'||'!');
END mgmt$mask_errorExit;
/

CREATE OR REPLACE PROCEDURE mgmt$mask_errorExitOraError (msg IN VARCHAR2, errMsg IN VARCHAR2) IS
BEGIN
    mgmt$mask_sendMsg (msg);
    mgmt$mask_sendMsg (errMsg);
    mgmt$mask_sendMsg ('errorExitOraError'||'!');
END mgmt$mask_errorExitOraError;
/

CREATE OR REPLACE PROCEDURE mgmt$mask_checkDBAPrivs 
AUTHID CURRENT_USER IS
    granted_role REAL := 0;
    user_name user_users.username%type;
BEGIN
SELECT USERNAME INTO user_name FROM USER_USERS;
    EXECUTE IMMEDIATE 'SELECT 1 FROM SYS.DBA_ROLE_PRIVS WHERE GRANTED_ROLE = ''DBA'' AND GRANTEE = :1'
      INTO granted_role       USING user_name;
EXCEPTION
    WHEN OTHERS THEN
       IF SQLCODE = -01403 OR SQLCODE = -00942  THEN
      mgmt$mask_sendMsg ( 'WARNING checking privileges... User Name: ' || user_name);
      mgmt$mask_sendMsg ( 'User does not have DBA privs. ' );
      mgmt$mask_errorExitOraError ( 'The script will fail if it tries to perform operations for which the user lacks the appropriate privilege. ',' ' );
      END IF;
END mgmt$mask_checkDBAPrivs;
/

CREATE OR REPLACE PROCEDURE mgmt$mask_setUpJobTable (script_id IN INTEGER, job_table IN VARCHAR2, step_num OUT INTEGER)
AUTHID CURRENT_USER IS
    ctsql_text VARCHAR2(200) := 'CREATE TABLE ' || job_table || '(SCRIPT_ID NUMBER, LAST_STEP NUMBER, unique (SCRIPT_ID))';
    itsql_text VARCHAR2(200) := 'INSERT INTO ' || job_table || ' (SCRIPT_ID, LAST_STEP) values (:1, :2)';
    stsql_text VARCHAR2(200) := 'SELECT last_step FROM ' || job_table || ' WHERE script_id = :1';

    TYPE CurTyp IS REF CURSOR;  -- define weak REF CURSOR type
    stsql_cur CurTyp;  -- declare cursor variable

BEGIN
    step_num := 0;
    BEGIN
      EXECUTE IMMEDIATE ctsql_text;
    EXCEPTION
      WHEN OTHERS THEN
        NULL;
    END;

    BEGIN
      OPEN stsql_cur FOR  -- open cursor variable
        stsql_text USING  script_id;
      FETCH stsql_cur INTO step_num;
      IF stsql_cur%FOUND THEN
        NULL;
      ELSE
        EXECUTE IMMEDIATE itsql_text USING script_id, step_num;
        COMMIT;
        step_num := 1;
      END IF;
      CLOSE stsql_cur;
    EXCEPTION
      WHEN OTHERS THEN
        mgmt$mask_errorExit ('ERROR selecting or inserting from table: ' || job_table);
        return;
    END;

    return;

EXCEPTION
      WHEN OTHERS THEN
        mgmt$mask_errorExit ('ERROR accessing table: ' || job_table);
        return;
END mgmt$mask_setUpJobTable;
/

CREATE OR REPLACE PROCEDURE mgmt$mask_deleteJobTableEntry(script_id IN INTEGER, job_table IN VARCHAR2, step_num IN INTEGER, highest_step IN INTEGER)
AUTHID CURRENT_USER IS
    delete_text VARCHAR2(200) := 'DELETE FROM ' || job_table || ' WHERE SCRIPT_ID = :1';
BEGIN

    IF step_num <= highest_step THEN
      return;
    END IF;

    BEGIN
      EXECUTE IMMEDIATE delete_text USING script_id;
      IF SQL%NOTFOUND THEN
        mgmt$mask_errorExit ('ERROR deleting entry from table: ' || job_table);
        return;
      END IF;
    EXCEPTION
        WHEN OTHERS THEN
          mgmt$mask_errorExit ('ERROR deleting entry from table: ' || job_table);
          return;
    END;

    COMMIT;
END mgmt$mask_deleteJobTableEntry;
/

CREATE OR REPLACE PROCEDURE mgmt$mask_setStep (script_id IN INTEGER, job_table IN VARCHAR2, step_num IN INTEGER)
AUTHID CURRENT_USER IS
    update_text VARCHAR2(200) := 'UPDATE ' || job_table || ' SET last_step = :1 WHERE script_id = :2';
BEGIN
    -- update job table
    EXECUTE IMMEDIATE update_text USING step_num, script_id;
    IF SQL%NOTFOUND THEN
      mgmt$mask_sendMsg ('NOTFOUND EXCEPTION of sql_text: ' || update_text);
      mgmt$mask_errorExit ('ERROR accessing table: ' || job_table);
      return;
    END IF;
    COMMIT;
EXCEPTION
    WHEN OTHERS THEN
      mgmt$mask_errorExit ('ERROR accessing table: ' || job_table);
      return;
END mgmt$mask_setStep;
/

create or replace package mgmt$mask_util authid current_user is

    procedure set_seed ( s number );
    function get_seed return number;
    function strval (len number) return varchar2;
    function numval (low number, high number) return number;
    function randomencode (i_input VARCHAR2, pad_length NUMBER) RETURN VARCHAR2; 

end mgmt$mask_util;
/
create or replace package body mgmt$mask_util is

    hsd number := -1;

    procedure set_seed ( s number ) is
    begin
        hsd := s;
    end;

    function get_seed return number is
    begin
        return hsd;
    end;

    function strval (len number) return varchar2 is
    begin
        return dbms_random.string('l', len);
    end;

    function numval (low number, high number) return number is
    begin
        return dbms_random.value(low,high);
    end;

    FUNCTION randomencode (i_input VARCHAR2, pad_length NUMBER) RETURN VARCHAR2 IS
        TYPE charmap IS TABLE OF VARCHAR2(1) INDEX BY BINARY_INTEGER;
        l_input NUMBER;
        l_mod NUMBER;
        l_retCode VARCHAR2(100);
        l_map charmap;
        l_base number := 25;
        BEGIN
        if (i_input is null) then
            return lpad('a',pad_length,'a');
        end if;

        l_map(0)  := 'a';
        l_map(1)  := 'b';
        l_map(2)  := 'c';
        l_map(3)  := 'd';
        l_map(4)  := 'e';
        l_map(5)  := 'f';
        l_map(6)  := 'g';
        l_map(7)  := 'h';
        l_map(8)  := 'i';
        l_map(9)  := 'j';
        l_map(10) := 'k';
        l_map(11) := 'l';
        l_map(12) := 'm';
        l_map(13) := 'n';
        l_map(14) := 'o';
        l_map(15) := 'p';
        l_map(16) := 'q';
        l_map(17) := 'r';
        l_map(18) := 's';
        l_map(19) := 't';
        l_map(20) := 'u';
        l_map(21) := 'v';
        l_map(22) := 'w';
        l_map(23) := 'x';
        l_map(24) := 'y';
        l_map(25) := 'z';

        l_input := i_input;
        l_retCode := '';

        LOOP 
    	   -- skip 'a' for padding
            l_mod := l_input mod l_base + 1; 
            l_retCode := l_map(l_mod) || l_retCode ;
            IF (l_input >= l_base) THEN
                l_input := round(l_input / l_base);
            ELSE 
                l_input := 0;
            END IF;
            -- dbms_output.put_line('left ' || l_input || ' mod ' || l_mod );
            EXIT WHEN l_input = 0;
        END LOOP;
        return lpad(l_retCode, pad_length, 'a');
    END ;
END mgmt$mask_util;
/

create or replace type mgmt$mask_array_list as table of varchar2(128)
/
CREATE OR REPLACE PROCEDURE mgmt$mask_setUpMappingTable (sid VARCHAR2, sourcecol VARCHAR2, sourcetab VARCHAR2)
AUTHID CURRENT_USER IS
    ctsql_text VARCHAR2(400) := 'CREATE TABLE MGMT_DM_TT_' || sid || 
      '(ORIG_VAL, NEW_VAL) ' ||
      ' AS (SELECT '|| sourcecol || ',' || sourcecol ||
      ' FROM ' || sourcetab || ' WHERE ROWNUM<1) ';
    itsql_text VARCHAR2(400) := 'INSERT INTO  MGMT_DM_TT_' || sid || 
      ' SELECT ' || sourcecol || ', dm_seq.nextval FROM ' ||
      '( SELECT DISTINCT ' || sourcecol || ' FROM ' ||  sourcetab || ')';
    dlsql_text VARCHAR2(400) := 'DELETE FROM  MGMT_DM_TT_' || sid  ;
BEGIN
    BEGIN
      EXECUTE IMMEDIATE ctsql_text;
    EXCEPTION
      WHEN OTHERS THEN
        EXECUTE IMMEDIATE dlsql_text;
    END;
EXCEPTION
      WHEN OTHERS THEN
        mgmt$mask_errorExit ('ERROR accessing table: MGMT_DM_TT_' || sid);
END mgmt$mask_setUpMappingTable;
/

CREATE OR REPLACE PROCEDURE mgmt$step_1_2(script_id IN INTEGER, job_table IN VARCHAR2, step_num IN OUT INTEGER)
AUTHID CURRENT_USER IS
    sqlerr_msg VARCHAR2(100);
BEGIN
    IF step_num <> 1 THEN
      return;
    END IF;

    mgmt$mask_setStep (2, 'MGMT$MASK_CHECKPOINT', step_num);
    step_num := step_num + 1;
    BEGIN
      mgmt$mask_sendMsg ('COMMIT');
      EXECUTE IMMEDIATE 'COMMIT';
      mgmt$mask_sendMsg ('ALTER SESSION ENABLE PARALLEL DML');
      EXECUTE IMMEDIATE 'ALTER SESSION ENABLE PARALLEL DML';
    EXCEPTION
      WHEN OTHERS THEN
        sqlerr_msg := SUBSTR(SQLERRM, 1, 100);
        mgmt$mask_errorExitOraError('ERROR executing steps ',  sqlerr_msg);
        step_num := -1;
        return;
    END;
END mgmt$step_1_2;
/

CREATE OR REPLACE PROCEDURE mgmt$step_2_2(script_id IN INTEGER, job_table IN VARCHAR2, step_num IN OUT INTEGER)
AUTHID CURRENT_USER IS
    sqlerr_msg VARCHAR2(100);
BEGIN
    IF step_num <> 2 THEN
      return;
    END IF;

    mgmt$mask_setStep (2, 'MGMT$MASK_CHECKPOINT', step_num);
    step_num := step_num + 1;
    BEGIN
      mgmt$mask_sendMsg ('DECLARE 
  CURSOR fk_sql IS select refr.owner, refr.table_name, refr.constraint_name 
     from dba_constraints refd, dba_constraints refr 
     where        refd.owner = ''SCOTT'' and 
             refd.table_name = ''EMP_M'' and 
        refr.constraint_type = ''R'' and 
                refr.r_owner = refd.owner and 
      refr.r_constraint_name = refd.constraint_name;
BEGIN 
  FOR fk IN fk_sql 
  LOOP 
    EXECUTE IMMEDIATE ''ALTER TABLE "'' || fk.owner || ''"."'' || 
      fk.table_name || ''" DROP CONSTRAINT "'' || fk.constraint_name || ''"''; 
  END LOOP;
END;');
      EXECUTE IMMEDIATE 'DECLARE 
  CURSOR fk_sql IS select refr.owner, refr.table_name, refr.constraint_name 
     from dba_constraints refd, dba_constraints refr 
     where        refd.owner = ''SCOTT'' and 
             refd.table_name = ''EMP_M'' and 
        refr.constraint_type = ''R'' and 
                refr.r_owner = refd.owner and 
      refr.r_constraint_name = refd.constraint_name;
BEGIN 
  FOR fk IN fk_sql 
  LOOP 
    EXECUTE IMMEDIATE ''ALTER TABLE "'' || fk.owner || ''"."'' || 
      fk.table_name || ''" DROP CONSTRAINT "'' || fk.constraint_name || ''"''; 
  END LOOP;
END;';
    EXCEPTION
      WHEN OTHERS THEN
        sqlerr_msg := SUBSTR(SQLERRM, 1, 100);
        mgmt$mask_errorExitOraError('ERROR executing steps ',  sqlerr_msg);
        step_num := -1;
        return;
    END;
END mgmt$step_2_2;
/

CREATE OR REPLACE PROCEDURE mgmt$step_3_2(script_id IN INTEGER, job_table IN VARCHAR2, step_num IN OUT INTEGER)
AUTHID CURRENT_USER IS
    sqlerr_msg VARCHAR2(100);
BEGIN
    IF step_num <> 3 THEN
      return;
    END IF;

    mgmt$mask_setStep (2, 'MGMT$MASK_CHECKPOINT', step_num);
    step_num := step_num + 1;
    BEGIN
      mgmt$mask_sendMsg ('DECLARE 
  CURSOR c_sql IS select owner, table_name, constraint_name, 
    constraint_type, generated, index_name from dba_constraints 
  where        owner = ''SCOTT'' and 
          table_name = ''EMP_M'' and 
     constraint_type <> ''R''; 
BEGIN 
  FOR c IN c_sql 
  LOOP 
      EXECUTE IMMEDIATE ''ALTER TABLE "'' || c.owner || ''"."'' || 
        c.table_name || ''" DROP CONSTRAINT "'' || c.constraint_name || ''"'';
  END LOOP;
END;');
      EXECUTE IMMEDIATE 'DECLARE 
  CURSOR c_sql IS select owner, table_name, constraint_name, 
    constraint_type, generated, index_name from dba_constraints 
  where        owner = ''SCOTT'' and 
          table_name = ''EMP_M'' and 
     constraint_type <> ''R''; 
BEGIN 
  FOR c IN c_sql 
  LOOP 
      EXECUTE IMMEDIATE ''ALTER TABLE "'' || c.owner || ''"."'' || 
        c.table_name || ''" DROP CONSTRAINT "'' || c.constraint_name || ''"'';
  END LOOP;
END;';
    EXCEPTION
      WHEN OTHERS THEN
        sqlerr_msg := SUBSTR(SQLERRM, 1, 100);
        mgmt$mask_errorExitOraError('ERROR executing steps ',  sqlerr_msg);
        step_num := -1;
        return;
    END;
END mgmt$step_3_2;
/

CREATE OR REPLACE PROCEDURE mgmt$step_4_2(script_id IN INTEGER, job_table IN VARCHAR2, step_num IN OUT INTEGER)
AUTHID CURRENT_USER IS
    sqlerr_msg VARCHAR2(100);
BEGIN
    IF step_num <> 4 THEN
      return;
    END IF;

    mgmt$mask_setStep (2, 'MGMT$MASK_CHECKPOINT', step_num);
    step_num := step_num + 1;
    BEGIN
      mgmt$mask_sendMsg ('ALTER TABLE "SCOTT"."EMP_M" RENAME TO "EMP_M$DMASK"');
      EXECUTE IMMEDIATE 'ALTER TABLE "SCOTT"."EMP_M" RENAME TO "EMP_M$DMASK"';
    EXCEPTION
      WHEN OTHERS THEN
        sqlerr_msg := SUBSTR(SQLERRM, 1, 100);
        mgmt$mask_errorExitOraError('ERROR executing steps ',  sqlerr_msg);
        step_num := -1;
        return;
    END;
END mgmt$step_4_2;
/

CREATE OR REPLACE PROCEDURE mgmt$step_5_2(script_id IN INTEGER, job_table IN VARCHAR2, step_num IN OUT INTEGER)
AUTHID CURRENT_USER IS
    sqlerr_msg VARCHAR2(100);
BEGIN
    IF step_num <> 5 THEN
      return;
    END IF;

    mgmt$mask_setStep (2, 'MGMT$MASK_CHECKPOINT', step_num);
    step_num := step_num + 1;
    BEGIN
      mgmt$mask_sendMsg ('CREATE TABLE "SCOTT"."EMP_M"   TABLESPACE "USERS" PCTFREE 10 INITRANS 1 MAXTRANS 255 STORAGE  ( INITIAL 64K BUFFER_POOL DEFAULT)  NOLOGGING  NOCOMPRESS  PARALLEL AS SELECT s."EMPNO", c0m3."ENAME", s."JOB", s."MGR", s."HIREDATE", s."SAL", s."COMM", s."DEPTNO" FROM (SELECT ROWID rid, ROWNUM rn, "EMPNO", "ENAME", "JOB", "MGR", "HIREDATE", "SAL", "COMM", "DEPTNO" FROM "SCOTT"."EMP_M$DMASK") s, (SELECT ROWNUM rn, m."ENAME" "ENAME" FROM (SELECT "ENAME" FROM "SCOTT"."EMP_M$DMASK" ORDER BY DBMS_RANDOM.VALUE) m)c0m3 WHERE s.rn=c0m3.rn');
      EXECUTE IMMEDIATE 'CREATE TABLE "SCOTT"."EMP_M"   TABLESPACE "USERS" PCTFREE 10 INITRANS 1 MAXTRANS 255 STORAGE  ( INITIAL 64K BUFFER_POOL DEFAULT)  NOLOGGING  NOCOMPRESS  PARALLEL AS SELECT s."EMPNO", c0m3."ENAME", s."JOB", s."MGR", s."HIREDATE", s."SAL", s."COMM", s."DEPTNO" FROM (SELECT ROWID rid, ROWNUM rn, "EMPNO", "ENAME", "JOB", "MGR", "HIREDATE", "SAL", "COMM", "DEPTNO" FROM "SCOTT"."EMP_M$DMASK") s, (SELECT ROWNUM rn, m."ENAME" "ENAME" FROM (SELECT "ENAME" FROM "SCOTT"."EMP_M$DMASK" ORDER BY DBMS_RANDOM.VALUE) m)c0m3 WHERE s.rn=c0m3.rn';
    EXCEPTION
      WHEN OTHERS THEN
        sqlerr_msg := SUBSTR(SQLERRM, 1, 100);
        mgmt$mask_errorExitOraError('ERROR executing steps ',  sqlerr_msg);
        step_num := -1;
        return;
    END;
END mgmt$step_5_2;
/

CREATE OR REPLACE PROCEDURE mgmt$step_6_2(script_id IN INTEGER, job_table IN VARCHAR2, step_num IN OUT INTEGER)
AUTHID CURRENT_USER IS
    sqlerr_msg VARCHAR2(100);
BEGIN
    IF step_num <> 6 THEN
      return;
    END IF;

    mgmt$mask_setStep (2, 'MGMT$MASK_CHECKPOINT', step_num);
    step_num := step_num + 1;
    BEGIN
      mgmt$mask_sendMsg ('ALTER TABLE "SCOTT"."EMP_M" LOGGING  NOPARALLEL ');
      EXECUTE IMMEDIATE 'ALTER TABLE "SCOTT"."EMP_M" LOGGING  NOPARALLEL ';
    EXCEPTION
      WHEN OTHERS THEN
        sqlerr_msg := SUBSTR(SQLERRM, 1, 100);
        mgmt$mask_errorExitOraError('ERROR executing steps ',  sqlerr_msg);
        step_num := -1;
        return;
    END;
END mgmt$step_6_2;
/

CREATE OR REPLACE PROCEDURE mgmt$step_7_2(script_id IN INTEGER, job_table IN VARCHAR2, step_num IN OUT INTEGER)
AUTHID CURRENT_USER IS
    sqlerr_msg VARCHAR2(100);
BEGIN
    IF step_num <> 7 THEN
      return;
    END IF;

    mgmt$mask_setStep (2, 'MGMT$MASK_CHECKPOINT', step_num);
    step_num := step_num + 1;
    BEGIN
      mgmt$mask_sendMsg ('DROP TABLE "SCOTT"."EMP_M$DMASK" PURGE');
      EXECUTE IMMEDIATE 'DROP TABLE "SCOTT"."EMP_M$DMASK" PURGE';
    EXCEPTION
      WHEN OTHERS THEN
        sqlerr_msg := SUBSTR(SQLERRM, 1, 100);
        mgmt$mask_errorExitOraError('ERROR executing steps ',  sqlerr_msg);
        step_num := -1;
        return;
    END;
END mgmt$step_7_2;
/

CREATE OR REPLACE PROCEDURE mgmt$step_8_2(script_id IN INTEGER, job_table IN VARCHAR2, step_num IN OUT INTEGER)
AUTHID CURRENT_USER IS
    sqlerr_msg VARCHAR2(100);
BEGIN
    IF step_num <> 8 THEN
      return;
    END IF;

    mgmt$mask_setStep (2, 'MGMT$MASK_CHECKPOINT', step_num);
    step_num := step_num + 1;
    BEGIN
      mgmt$mask_sendMsg ('BEGIN DBMS_STATS.GATHER_TABLE_STATS(''"SCOTT"'', ''"EMP_M"'', estimate_percent=>DBMS_STATS.AUTO_SAMPLE_SIZE, degree=>DBMS_STATS.DEFAULT_DEGREE, cascade=>TRUE); END;');
      EXECUTE IMMEDIATE 'BEGIN DBMS_STATS.GATHER_TABLE_STATS(''"SCOTT"'', ''"EMP_M"'', estimate_percent=>DBMS_STATS.AUTO_SAMPLE_SIZE, degree=>DBMS_STATS.DEFAULT_DEGREE, cascade=>TRUE); END;';
    EXCEPTION
      WHEN OTHERS THEN
        sqlerr_msg := SUBSTR(SQLERRM, 1, 100);
        mgmt$mask_errorExitOraError('ERROR executing steps ',  sqlerr_msg);
        step_num := -1;
        return;
    END;
END mgmt$step_8_2;
/

CREATE OR REPLACE PROCEDURE mgmt$step_9_2(script_id IN INTEGER, job_table IN VARCHAR2, step_num IN OUT INTEGER)
AUTHID CURRENT_USER IS
    sqlerr_msg VARCHAR2(100);
BEGIN
    IF step_num <> 9 THEN
      return;
    END IF;

    mgmt$mask_setStep (2, 'MGMT$MASK_CHECKPOINT', step_num);
    step_num := step_num + 1;
    BEGIN
      mgmt$mask_sendMsg ('alter system flush shared_pool');
      EXECUTE IMMEDIATE 'alter system flush shared_pool';
    EXCEPTION
      WHEN OTHERS THEN
        sqlerr_msg := SUBSTR(SQLERRM, 1, 100);
        mgmt$mask_errorExitOraError('ERROR executing steps ',  sqlerr_msg);
        step_num := -1;
        return;
    END;
END mgmt$step_9_2;
/

CREATE OR REPLACE PROCEDURE mgmt$step_10_2(script_id IN INTEGER, job_table IN VARCHAR2, step_num IN OUT INTEGER)
AUTHID CURRENT_USER IS
    sqlerr_msg VARCHAR2(100);
BEGIN
    IF step_num <> 10 THEN
      return;
    END IF;

    mgmt$mask_setStep (2, 'MGMT$MASK_CHECKPOINT', step_num);
    step_num := step_num + 1;
    BEGIN
      mgmt$mask_sendMsg ('alter system checkpoint');
      EXECUTE IMMEDIATE 'alter system checkpoint';
    EXCEPTION
      WHEN OTHERS THEN
        sqlerr_msg := SUBSTR(SQLERRM, 1, 100);
        mgmt$mask_errorExitOraError('ERROR executing steps ',  sqlerr_msg);
        step_num := -1;
        return;
    END;
END mgmt$step_10_2;
/

CREATE OR REPLACE PROCEDURE mgmt$mask_cleanup_2 (script_id IN INTEGER, job_table IN VARCHAR2, step_num IN INTEGER, highest_step IN INTEGER)
AUTHID CURRENT_USER IS
BEGIN
    IF step_num <= highest_step THEN
      return;
    END IF;

    mgmt$mask_sendMsg ('Starting cleanup of recovery tables');

    mgmt$mask_deleteJobTableEntry(script_id, job_table, step_num, highest_step);

    mgmt$mask_sendMsg ('Completed cleanup of recovery tables');
END mgmt$mask_cleanup_2;
/

CREATE OR REPLACE PROCEDURE mgmt$mask_commentheader_2 IS
BEGIN
     mgmt$mask_sendMsg ('--   Target database:	orcl');
     mgmt$mask_sendMsg ('--   Script generated at:	22-JUN-2010   13:33');
END mgmt$mask_commentheader_2;
/

-- Script Execution Controller
-- ==============================================

variable step_num number;
exec mgmt$mask_commentheader_2;
exec mgmt$mask_sendMsg ('Starting Data Masking');
show user;
exec mgmt$mask_checkDBAPrivs;
exec mgmt$mask_setupJobTable (2, 'MGMT$MASK_CHECKPOINT', :step_num);

exec mgmt$step_1_2(2, 'MGMT$MASK_CHECKPOINT', :step_num);
exec mgmt$step_2_2(2, 'MGMT$MASK_CHECKPOINT', :step_num);
exec mgmt$step_3_2(2, 'MGMT$MASK_CHECKPOINT', :step_num);
exec mgmt$step_4_2(2, 'MGMT$MASK_CHECKPOINT', :step_num);
exec mgmt$step_5_2(2, 'MGMT$MASK_CHECKPOINT', :step_num);
exec mgmt$step_6_2(2, 'MGMT$MASK_CHECKPOINT', :step_num);
exec mgmt$step_7_2(2, 'MGMT$MASK_CHECKPOINT', :step_num);
exec mgmt$step_8_2(2, 'MGMT$MASK_CHECKPOINT', :step_num);
exec mgmt$step_9_2(2, 'MGMT$MASK_CHECKPOINT', :step_num);
exec mgmt$step_10_2(2, 'MGMT$MASK_CHECKPOINT', :step_num);

exec mgmt$mask_sendMsg ('Completed Data Masking. Starting cleanup phase.');

exec mgmt$mask_cleanup_2 (2, 'MGMT$MASK_CHECKPOINT', :step_num, 10);

exec mgmt$mask_sendMsg ('Starting cleanup of generated procedures');

DROP PROCEDURE mgmt$step_1_2;
DROP PROCEDURE mgmt$step_2_2;
DROP PROCEDURE mgmt$step_3_2;
DROP PROCEDURE mgmt$step_4_2;
DROP PROCEDURE mgmt$step_5_2;
DROP PROCEDURE mgmt$step_6_2;
DROP PROCEDURE mgmt$step_7_2;
DROP PROCEDURE mgmt$step_8_2;
DROP PROCEDURE mgmt$step_9_2;
DROP PROCEDURE mgmt$step_10_2;

DROP PROCEDURE mgmt$mask_cleanup_2;
DROP PROCEDURE mgmt$mask_commentheader_2;


exec mgmt$mask_sendMsg ('Completed cleanup of generated procedures');

exec mgmt$mask_sendMsg ('Script execution complete');

spool off
set pagesize 24
set serveroutput off
set feedback on
set echo on
set ver on
