从目录文件

时间:2017-08-03 10:44:01

标签: sql oracle for-loop utl-file

下面是我从目录中获取文件并使用UTL文件处理程序将它们插入表中的代码,使用 GET_DIR_LIST2(' / path&#)在 DIR_LIST2 中成功加载文件39;); 但是当在进程中使用指定的映射将它们加载到我的表中时,每次执行进程时总是需要100个文件,如何使用游标或for循环在for循环中应用上限在一次进程执行中加载所有文件的其他方法。

 create or replace PROCEDURE UPLOAD_CMS_VISA
AS
BEGIN
DECLARE
f utl_file.file_type;
s                  VARCHAR2(4000);
BRANCH_CODE        VARCHAR2(500);
TRANS_DATE         VARCHAR2(500);
TRANS_TIME         VARCHAR2(500);
SEQ_NO             VARCHAR2(500);
MSG_TYPE           VARCHAR2(500);
REMOTE_ACCOUNT     VARCHAR2(500);
FRG_CURR_AMT       VARCHAR2(500);
LOC_CURR_AMT       VARCHAR2(500);
CHEQUE_NO          VARCHAR2(500);
IBCA_NO            VARCHAR2(500);
CR_DR_CODE         VARCHAR2(500);
DESC_DETAIL        VARCHAR2(500);
DOC_NO             VARCHAR2(500);
Y_LIVECMGT_ID      VARCHAR2(500);
FILLER_ONE         VARCHAR2(500);
T24_ACC_NO         VARCHAR2(500);
FILLER_TWO         VARCHAR2(500);
INST_CODE          VARCHAR2(500);
FILLER             VARCHAR2(500);
LAST_DATE_MODIFIED VARCHAR2(500);
LEAD_COMPANY       VARCHAR2(500);
LEAD_COUNTRY       VARCHAR2(500);
V_TABLE_RECORD_COUNT NUMBER :=0;
V_FILE_RECORD_COUNT NUMBER :=0;
-- start_pos NUMBER;
ex    BOOLEAN;
flen  NUMBER;
bsize NUMBER;
CURSOR C1
IS
  SELECT FILENAME
  FROM DIR_LIST2
  WHERE FILENAME NOT IN
    ( SELECT FILENAME FROM CMS_CONTROL
    );
BEGIN
GET_DIR_LIST2( 'path' );
FOR FILE_REC IN C1
LOOP
  UTL_FILE.FGETATTR('CSHMGMT', FILE_REC.FILENAME, EX, FLEN, BSIZE);
  DBMS_OUTPUT.PUT_LINE(FILE_REC.FILENAME);
  IF EX THEN

  V_TABLE_RECORD_COUNT :=0;
  V_FILE_RECORD_COUNT:= 0;

    dbms_output.put_line('File Exist');
    F := UTL_FILE.FOPEN('CSHMGMT', FILE_REC.FILENAME, 'R');
    LOOP
      BEGIN
      V_FILE_RECORD_COUNT := V_FILE_RECORD_COUNT + 1;

        UTL_FILE.GET_LINE(F,S);
        BRANCH_CODE        := (SUBSTR(s, 1, 4));
        TRANS_DATE         := (SUBSTR(S, 5,8));
        TRANS_TIME         := (SUBSTR(S, 13,6));
        SEQ_NO             := (SUBSTR(S,19,10));
        MSG_TYPE           := (SUBSTR(S,29,4));
        REMOTE_ACCOUNT     := (SUBSTR(S,33,10));
        FRG_CURR_AMT       := (SUBSTR(S,43,14));
        LOC_CURR_AMT       := (SUBSTR(S,57,14));
        CHEQUE_NO          := (SUBSTR(S,71,8));
        IBCA_NO            := (SUBSTR(S,79,8));
        CR_DR_CODE         := (SUBSTR(S,87,1));
        DESC_DETAIL        := (SUBSTR(s,88,21));
        DOC_NO             := (SUBSTR(S,109,6));
        Y_LIVECMGT_ID      := (SUBSTR(s,115,16));
        FILLER_ONE         := (SUBSTR(S,131,65));
        T24_ACC_NO         := (SUBSTR(s,196,16));
        FILLER_TWO         := (SUBSTR(s,212,199));
        INST_CODE          := (SUBSTR(S,411,6));
        FILLER             := (SUBSTR(s,417));
        LAST_DATE_MODIFIED := NULL;
        LEAD_COMPANY       := 001;
        LEAD_COUNTRY       := 'PK';
        INSERT
        INTO CMS_VISA_TEST
          (
            BRANCH_CODE,
            TRANS_DATE,
            TRANS_TIME,
            SEQ_NO,
            MSG_TYPE,
            REMOTE_ACCOUNT,
            FRG_CURR_AMT,
            LOC_CURR_AMT,
            CHEQUE_NO,
            IBCA_NO,
            CR_DR_CODE,
            DESC_DETAIL,
            DOC_NO,
            Y_LIVECMGT_ID,
            FILLER_ONE,
            T24_ACC_NO,
            FILLER_TWO,
            INST_CODE,
            FILLER,
            LAST_DATE_MODIFIED,
            LEAD_COMPANY,
            LEAD_COUNTRY,
            SYSTEM_DATE
          )
          VALUES
          (
            TRIM(BRANCH_CODE),
            CONVERT_TO_DATE(TRANS_DATE),
            TRIM(TRANS_TIME),
            TRIM(SEQ_NO),
            TRIM(MSG_TYPE),
            TRIM(REMOTE_ACCOUNT),
            TRIM(FRG_CURR_AMT),
            TRIM(LOC_CURR_AMT),
            TRIM(CHEQUE_NO),
            TRIM(IBCA_NO),
            TRIM(CR_DR_CODE),
            TRIM(DESC_DETAIL),
            TRIM(DOC_NO),
            TRIM(Y_LIVECMGT_ID),
            TRIM(FILLER_ONE),
            TRIM(T24_ACC_NO),
            TRIM(FILLER_TWO),
            TRIM(INST_CODE),
            TRIM(FILLER),
            CONVERT_TO_DATE(LAST_DATE_MODIFIED),
            LEAD_COMPANY,
            LEAD_COUNTRY,
            SYSDATE
          );
        COMMIT;

        V_TABLE_RECORD_COUNT := V_TABLE_RECORD_COUNT +1;
        --END IF;
      EXCEPTION
      WHEN utl_file.INVALID_PATH THEN
        DBMS_OUTPUT.PUT_LINE('Invalid Path');
        ERROR_LOGGER ('UPLOAD_CMS_VISA', 'Invalid Pat','FILE_NAME='|| FILE_REC.FILENAME||' TRANS_TIME='|| TRIM(TRANS_TIME)||' LOC_CURR_AMT='||TRIM(LOC_CURR_AMT)||' TT NO='||Y_LIVECMGT_ID, SQLCODE, SUBSTR(SQLERRM, 1, 500));
      WHEN utl_file.INVALID_MODE THEN
        dbms_output.put_line('Invalid Mode');
        ERROR_LOGGER ('UPLOAD_CMS_VISA', 'Invalid Mod','FILE_NAME='|| FILE_REC.FILENAME||' TRANS_TIME='|| TRIM(TRANS_TIME)||' LOC_CURR_AMT='||TRIM(LOC_CURR_AMT)||' TT NO='||Y_LIVECMGT_ID, SQLCODE, SUBSTR(SQLERRM, 1, 500));
      WHEN UTL_FILE.INVALID_FILEHANDLE THEN
        dbms_output.put_line('Invalid File Handler');
        ERROR_LOGGER ('UPLOAD_CMS_VISA', 'Invalid File Handler','FILE_NAME='|| FILE_REC.FILENAME||' TRANS_TIME='|| TRIM(TRANS_TIME)||' LOC_CURR_AMT='||TRIM(LOC_CURR_AMT)||' TT NO='||Y_LIVECMGT_ID, SQLCODE, SUBSTR(SQLERRM, 1, 500));
      WHEN utl_file.INVALID_OPERATION THEN
        DBMS_OUTPUT.PUT_LINE('Invalid Operation');
        ERROR_LOGGER ('UPLOAD_CMS_VISA', 'Invalid Operation','FILE_NAME='|| FILE_REC.FILENAME||' TRANS_TIME='|| TRIM(TRANS_TIME)||' LOC_CURR_AMT='||TRIM(LOC_CURR_AMT)||' TT NO='||Y_LIVECMGT_ID, SQLCODE, SUBSTR(SQLERRM, 1, 500));
      WHEN utl_file.READ_ERROR THEN
        DBMS_OUTPUT.PUT_LINE('Read Error');
        ERROR_LOGGER ('UPLOAD_CMS_VISA', 'Read Error','FILE_NAME='|| FILE_REC.FILENAME||' TRANS_TIME='|| TRIM(TRANS_TIME)||' LOC_CURR_AMT='||TRIM(LOC_CURR_AMT)||' TT NO='||Y_LIVECMGT_ID, SQLCODE, SUBSTR(SQLERRM, 1, 500));
      WHEN utl_file.WRITE_ERROR THEN
        DBMS_OUTPUT.PUT_LINE('Write Error');
        ERROR_LOGGER ('UPLOAD_CMS_VISA', 'Write Error','FILE_NAME='|| FILE_REC.FILENAME||' TRANS_TIME='|| TRIM(TRANS_TIME)||' LOC_CURR_AMT='||TRIM(LOC_CURR_AMT)||' TT NO='||Y_LIVECMGT_ID, SQLCODE, SUBSTR(SQLERRM, 1, 500));
      WHEN utl_file.INTERNAL_ERROR THEN
        DBMS_OUTPUT.PUT_LINE('Internal Error');
        ERROR_LOGGER ('UPLOAD_CMS_VISA', 'Internal Error','FILE_NAME='|| FILE_REC.FILENAME||' TRANS_TIME='|| TRIM(TRANS_TIME)||' LOC_CURR_AMT='||TRIM(LOC_CURR_AMT)||' TT NO='||Y_LIVECMGT_ID, SQLCODE, SUBSTR(SQLERRM, 1, 500));
      WHEN VALUE_ERROR THEN
        DBMS_OUTPUT.PUT_LINE('Value error');
        ERROR_LOGGER ('UPLOAD_CMS_VISA', 'Value error','FILE_NAME='|| FILE_REC.FILENAME||' TRANS_TIME='|| TRIM(TRANS_TIME)||' LOC_CURR_AMT='||TRIM(LOC_CURR_AMT)||' TT NO='||Y_LIVECMGT_ID, SQLCODE, SUBSTR(SQLERRM, 1, 500));
      WHEN NO_DATA_FOUND THEN
        --ERROR_LOGGER ('UPLOAD_CMS_VISA', 'NO_DATA_FOUND','FILE_NAME='|| FILE_REC.FILENAME||' TRANS_TIME='|| TRIM(TRANS_TIME)||' LOC_CURR_AMT='||TRIM(LOC_CURR_AMT)||' TT NO='||Y_LIVECMGT_ID, SQLCODE, SUBSTR(SQLERRM, 1, 500));
        null;
        --    commit;
        --              utl_file.fcopy('CDB_UPLOAD', 'export.log', 'CDB_UPLOAD', 'export.log.processed');
        --              utl_file.frename('CSHMGMT', FILE_REC.FILENAME, 'CMS_DHA', FILE_REC.FILENAME|| '_' || SYSDATE);
        --              utl_file.frename('PHXEXPORT', 'export.log', 'PHXEXPORT', 'export.log.processed' || SYSDATE);
        EXIT;
      WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('Other Error');
        CONTINUE;
      END;
    END LOOP;

    INSERT  INTO CMS_CONTROL ( FILENAME,TABLE_RECORD_COUNT,FILE_RECORD_COUNT, SYSTEM_DATE ) VALUES ( FILE_REC.FILENAME,V_TABLE_RECORD_COUNT,V_FILE_RECORD_COUNT,  SYSDATE  );
    COMMIT;
    --UTL_FILE.FREMOVE ('CSHMGMT', FILE_REC.FILENAME);
    utl_file.fclose(f);
  END IF;
END LOOP;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN

 null;
WHEN OTHERS THEN

    ERROR_LOGGER ('UPLOAD_CMS_VISA', 'CMS_VISA_TEST', '', SQLCODE,   SUBSTR(SQLERRM, 1, 500));
 END;
 END UPLOAD_CMS_VISA;

1 个答案:

答案 0 :(得分:0)

如果要将数据从flatFile连续加载到Oracle,这些步骤可能对您有所帮助。

1-创建类似于' loadpath';

的路径

2-创建外部文件脚本或过程。

3 - 写一个.sh。它会将您的平面文件与您想要的数量相结合。然后你获得一个累积文件。

4-配置.sh脚本。它将累积文件移动到外部表文件路径。

5-最后一步配置.sh脚本以调用外部表脚本或过程。

因此,您的.sh脚本将获得大量的平面文件,将它们组合起来获取一个累积文件,将其移动到外部表格路径,然后调用您的过程。

最后,将.sh添加到crontab以安排它。