Tuesday, July 28, 2015

Export Dump File Using Procedure.

CREATE OR REPLACE PROCEDURE SP_DUMP_EXPORT (
P_FROM_DATE DATE,
P_TO_DATE DATE
)
IS
  L_DP_HANDLE       NUMBER;
  L_LAST_JOB_STATE  VARCHAR2(30) := 'UNDEFINED';
  L_JOB_STATE       VARCHAR2(30) := 'UNDEFINED';
  L_STS             KU$_STATUS;

  SCHEMA_NAMES VARCHAR2(300);
BEGIN

    SELECT '''' || REPLACE (WM_CONCAT (USERNAME), ',', ''',''') || ''''
      INTO SCHEMA_NAMES
      FROM DBA_USERS
     WHERE USERNAME IN ('RND', 'HR');

  L_DP_HANDLE := DBMS_DATAPUMP.OPEN(
    OPERATION   => 'EXPORT',
    JOB_MODE    => 'SCHEMA',
    REMOTE_LINK => NULL,
    JOB_NAME    => 'MIG_'||TO_CHAR(P_FROM_DATE,'DD_MM_YYYY')||'_'||TO_CHAR(P_TO_DATE,'DD_MM_YYYY'),
    VERSION     => 'LATEST');

  DBMS_DATAPUMP.ADD_FILE(
    HANDLE    => L_DP_HANDLE,
    FILENAME  => 'MIG_'||TO_CHAR(P_FROM_DATE,'DD_MM_YYYY')||'_'||TO_CHAR(P_TO_DATE,'DD_MM_YYYY')||'.DMP',
    DIRECTORY => 'DATA_DIR');

  DBMS_DATAPUMP.ADD_FILE(
    HANDLE    => L_DP_HANDLE,
    FILENAME  => 'MIG_'||TO_CHAR(P_FROM_DATE,'DD_MM_YYYY')||'_'||TO_CHAR(P_TO_DATE,'DD_MM_YYYY')||'.LOG',
    DIRECTORY => 'DATA_DIR',
    FILETYPE  => DBMS_DATAPUMP.KU$_FILE_TYPE_LOG_FILE);
 
   DBMS_DATAPUMP.METADATA_FILTER(L_DP_HANDLE, 'SCHEMA_EXPR', 'IN (' || SCHEMA_NAMES || ')');

  DBMS_DATAPUMP.START_JOB(L_DP_HANDLE);

  DBMS_DATAPUMP.DETACH(L_DP_HANDLE);
END;


SQL> EXEC SP_DUMP_EXPORT('01-JAN-2015','30-JAN-2015');

PL/SQL procedure successfully completed.

SQL>

No comments:

Post a Comment