1

我的脚本如下,它将一个csv文件加载到PRODUCT_TBL,并且在此过程中发生任何错误,脚本将回滚事务并输出错误消息,但是当遇到UTL_FILE错误时它不会打印出消息,例如无效文件操作。任何帮助表示赞赏。谢谢

DECLARE
  V_error_code NUMBER;
  V_error_message VARCHAR2(255);
  V_ignore_headerlines NUMBER := 1; 
  V_eof BOOLEAN := FALSE;
  F UTL_FILE.FILE_TYPE;
  V_LINE VARCHAR2 (32767);
  V_PRD_ID PRODUCT_TBL.PRD_ID%TYPE;
  V_PATTERN PRODUCT_TBL.PATTERN%TYPE;
  V_REMARK PRODUCT_TBL.REMARK%TYPE;
  V_CREATED_BY PRODUCT_TBL.CREATED_BY%TYPE;
  V_CREATED_DATE PRODUCT_TBL.CREATED_DATE%TYPE;
  V_MODIFIED_BY PRODUCT_TBL.MODIFIED_BY%TYPE;
  V_MODIFIED_DATE PRODUCT_TBL.MODIFIED_DATE%TYPE;
BEGIN
  F := UTL_FILE.FOPEN ('DATA_DIR', 'PRODUCT_TBLv51.csv', 'R');
  IF V_ignore_headerlines > 0
  THEN
    BEGIN
      FOR i IN 1 .. V_ignore_headerlines
      LOOP
        UTL_FILE.get_line(F, V_LINE);
      END LOOP;
    EXCEPTION
      WHEN NO_DATA_FOUND
      THEN
        V_eof := TRUE;
    END;
  END IF;

  WHILE NOT V_eof
  LOOP
    BEGIN
      UTL_FILE.GET_LINE(F, V_LINE, 32767);
      IF V_LINE IS NULL THEN
        EXIT;
      END IF;
      V_PRD_ID := REGEXP_SUBSTR(V_LINE, '([^,\(]*(\([^\)]*\)[^,\(]*)*)(,|$)', 1, 1, 'i', 1);
      V_PATTERN := REGEXP_SUBSTR(V_LINE, '([^,\(]*(\([^\)]*\)[^,\(]*)*)(,|$)', 1, 2, 'i', 1);
      V_REMARK := REGEXP_SUBSTR(V_LINE, '([^,\(]*(\([^\)]*\)[^,\(]*)*)(,|$)', 1, 12, 'i', 1);
      V_CREATED_BY := REGEXP_SUBSTR(V_LINE, '([^,\(]*(\([^\)]*\)[^,\(]*)*)(,|$)', 1, 13, 'i', 1);
      V_CREATED_DATE := REGEXP_SUBSTR(V_LINE, '([^,\(]*(\([^\)]*\)[^,\(]*)*)(,|$)', 1, 14, 'i', 1);
      V_MODIFIED_BY := REGEXP_SUBSTR(V_LINE, '([^,\(]*(\([^\)]*\)[^,\(]*)*)(,|$)', 1, 15, 'i', 1);
      V_MODIFIED_DATE := REGEXP_SUBSTR(V_LINE, '([^,\(]*(\([^\)]*\)[^,\(]*)*)(,|$)', 1, 16, 'i', 1);
      INSERT INTO PRODUCT_TBL   (PRD_ID,PATTERN,REMARK,CREATED_BY,CREATED_DATE,MODIFIED_BY,MODIFIED_DATE) 
        VALUES(V_PRD_ID, V_PATTERN, V_REMARK, V_CREATED_BY, V_CREATED_DATE, V_MODIFIED_BY, V_MODIFIED_DATE);
    EXCEPTION 
      WHEN OTHERS THEN
        ROLLBACK;
        v_error_code := SQLCODE;
        v_error_message := SQLERRM;
        dbms_output.put_line(v_error_code || SQLERRM);
        EXIT;
    END;
  END LOOP;

  COMMIT;
  UTL_FILE.FCLOSE(F);

EXCEPTION
  WHEN UTL_FILE.INVALID_OPERATION THEN
     UTL_FILE.FCLOSE(F);
     dbms_output.put_line('File could not be opened or operated on as requested.');
END;
/
4

1 回答 1

0

在该部分之后添加一个EXCEPTION ... OTHERBlockUTL_FILE并查看实际通过什么样的异常来捕获它们。

EXCEPTION
  WHEN UTL_FILE.INVALID_OPERATION THEN
     UTL_FILE.FCLOSE(F);
     dbms_output.put_line('File could not be opened or operated on as requested.');
  WHEN OTHERS THEN
     dbms_output.put_line('other trouble'||SQLCODE||SQLERRM);

当您知道发生了哪件事时,您将知道如何抓住它。

于 2013-09-27T18:35:18.713 回答