簡體   English   中英

如何在SQL中引發異常后繼續while循環

[英]How to continue while loop after exception raised in SQL

在下面的代碼中引發異常后,如何繼續while循環?

DECLARE    
  v_blob_data       BLOB;    
  v_blob_len        NUMBER;    
  v_position        NUMBER;    
  v_raw_chunk       RAW(10000);    
  v_char      CHAR(1);    
  c_chunk_len   number       := 1;    
  v_line        VARCHAR2 (32767)        := NULL;    
  v_data_array      wwv_flow_global.vc_arr2;    
  v_rows number;    
  v_sr_no number := 1;  
  v_first_line_done boolean := false;  
  v_error_cd number :=0;  
  v_quote_pos1 NUMBER;  
  v_quote_pos2 NUMBER;  
  v_enclosed_str VARCHAR(200);
  v_errmsg VARCHAR2(4000);
BEGIN
  delete from TEMP_MM_UPDATE where username = :P1_USER_ID;

  -- Read data from wwv_flow_files</span>    
  select    
    blob_content    
    into v_blob_data    
    from wwv_flow_files    
    where name = :P2_FILE_UPLOAD; 

  v_blob_len := dbms_lob.getlength(v_blob_data);    
  v_position := 1; 

  -- Read and convert binary to char</span>  
  WHILE ( v_position <= v_blob_len )    
  LOOP 
    begin  
        v_raw_chunk := dbms_lob.substr(v_blob_data,c_chunk_len,v_position);    
        v_char :=  chr(hex_to_decimal(rawtohex(v_raw_chunk)));    
        v_line := v_line || v_char;    
        v_position := v_position + c_chunk_len;

      -- When a whole line is retrieved </span>   
      IF v_char = CHR(10) THEN
        LOOP  
          --Make sure there's something to replace  
          IF INSTR(v_line, '"', 1, 1) = 0 THEN  
            EXIT; -- If nothing to replace, exit loop and don't try  
          END IF;  
          --Find the position of the first and second quotes in the line of text  
          v_quote_pos1 := INSTR(v_line, '"', 1, 1);  
          v_quote_pos2 := INSTR(v_line, '"', 1, 2);  
          --Extract the inner string  
          v_enclosed_str := SUBSTR(v_line, v_quote_pos1 + 1, v_quote_pos2 - v_quote_pos1 - 1);  
          --perform the replacement  
          v_line := SUBSTR(v_line, 0, v_quote_pos1 - 1) || REPLACE(v_enclosed_str, ',', '<') || SUBSTR(v_line, v_quote_pos2 + 1);  
        END LOOP; 

        -- Convert comma to : to use wwv_flow_utilities </span>  
        v_line := REPLACE (v_line, ',', ':');  
        v_line := REPLACE (v_line, '<', ',');  
        v_line := REPLACE (trim(v_line), '-', NULL);  
        --v_line := REPLACE (trim(v_line), '"', NULL);  
        -- Convert each column separated by : into array of data </span>    
        v_data_array := wwv_flow_utilities.string_to_table (v_line);  
        --Check to see if the row of column headers has already been parsed through  
        IF(v_first_line_done != true)THEN   
          v_first_line_done := true;  
          --Check column order in spreadsheet  
          IF(v_data_array(1)    LIKE '%Username%' AND
              v_data_array(2)  LIKE '%NDN%' AND
              v_data_array(3)  LIKE '%PCFN%') THEN   
            v_error_cd := 0;  
            v_line := NULL;  
          ELSE  
            v_error_cd := 1;  
          END IF;  
        --If first line is done and the column order is correct then  
        ELSIF(v_first_line_done = true AND v_error_cd = 0) THEN   
          -- Insert data into target table </span>    
          EXECUTE IMMEDIATE 'insert into TEMP_MM_UPDATE   
          (USERNAME,
           RPT_FLAG,
           PCFN)
          values (:1,:2,:3)'   
           USING   
            v_data_array(1),   
            v_data_array(2),   
            v_data_array(3);    
           -- Clear out    
            v_line := NULL; v_sr_no := v_sr_no + 1; 
        END IF;  
      END IF;
    exception
      WHEN OTHERS then
        v_errmsg := SQLERRM;
        insert into temp_mm_update (username,error_desc)
        values (:P1_USER_ID, v_errmsg);
  END;
  END LOOP;

  DELETE FROM WWV_FLOW_FILES where name = :P2_FILE_UPLOAD;
  DELETE FROM TEMP_MM_UPDATE WHERE USERNAME IS NULL AND PCFN IS NULL;  
  IF(v_error_cd = 1) THEN  
    INSERT INTO temp_mm_update (USERNAME, ERROR_DESC)  
    VALUES (:P1_USER_ID, 'Error. Please check column order in spreadsheet.');  
  END IF;
EXCEPTION
  WHEN NO_DATA_FOUND THEN
    insert into temp_mm_update (username,error_desc)
    values (:P1_USER_ID, 'No Data Found.');
  WHEN OTHERS then
    v_errmsg := SQLERRM;
    insert into temp_mm_update (username,error_desc)
    values (:P1_USER_ID, v_errmsg);
END;

當我在上面的循環中放入異常時,該過程似乎從未完成,當我嘗試刪除表或類似的東西時,最終收到“ NOWAIT”錯誤。

如果我在循環后立即取出“ BEGIN”並在循環內也取出異常,則代碼可以正常工作,但是我希望能夠指定每條記錄出了什么問題,而不是處理正確的記錄然后在其后停止出現一條記錄,該記錄在一個僅接受6的列中具有9個值。

有人能幫忙嗎?

謝謝,

史蒂文

編輯:
我改變了這一行:
ELSIF(v_first_line_done = true AND v_error_cd = 0)

ELSIF(v_first_line_done = true AND v_error_cd = 0 AND(v_data_array(1)不為null或v_data_array(1)不像'%-%'))
並且該過程完成了,但是當涉及引發和異常的記錄時,以下所有記錄都具有相同的異常。

我的CSV文件示例:
用戶名NDN PCFN
DOOL0008 X 808461
DOOL0008 X 886332
DOOL0008 X 887223
DOOL0008 X 887992
DOOL0008 X 904959
DOOL0008 X 904959
DOOL0008 X 904968
DOOL0008 X 904968

因此,例如,如果列MM(其數據類型為VARCHAR2(1))在第三條記錄上的值為“ XX”,並且將引發該值過大的異常,則對於記錄4-8,同樣的異常也會被提出。

通過玩弄我的代碼,我發現了哪里出了問題。
我需要將以下行添加到我的異常代碼塊中:

v_line := NULL; v_sr_no := v_sr_no + 1; 

最終代碼:

  DECLARE    
  v_blob_data       BLOB;    
  v_blob_len        NUMBER;    
  v_position        NUMBER;    
  v_raw_chunk       RAW(10000);    
  v_char      CHAR(1);    
  c_chunk_len   number       := 1;    
  v_line        VARCHAR2 (32767)        := NULL;    
  v_data_array      wwv_flow_global.vc_arr2;    
  v_rows number;    
  v_sr_no number := 1;  
  v_first_line_done boolean := false;  
  v_error_cd number :=0;  
  v_quote_pos1 NUMBER;  
  v_quote_pos2 NUMBER;  
  v_enclosed_str VARCHAR(200);
  v_errmsg VARCHAR2(4000);
BEGIN
  delete from TEMP_MM_UPDATE where username = :P1_USER_ID;

  -- Read data from wwv_flow_files</span>    
  select    
    blob_content    
    into v_blob_data    
    from wwv_flow_files    
    where name = :P2_FILE_UPLOAD; 

  v_blob_len := dbms_lob.getlength(v_blob_data);    
  v_position := 1; 

  -- Read and convert binary to char</span>  
  WHILE ( v_position <= v_blob_len )    
  LOOP 
    begin  
        v_raw_chunk := dbms_lob.substr(v_blob_data,c_chunk_len,v_position);    
        v_char :=  chr(hex_to_decimal(rawtohex(v_raw_chunk)));    
        v_line := v_line || v_char;    
        v_position := v_position + c_chunk_len;

      -- When a whole line is retrieved </span>   
      IF v_char = CHR(10) THEN
        LOOP  
          --Make sure there's something to replace  
          IF INSTR(v_line, '"', 1, 1) = 0 THEN  
            EXIT; -- If nothing to replace, exit loop and don't try  
          END IF;  
          --Find the position of the first and second quotes in the line of text  
          v_quote_pos1 := INSTR(v_line, '"', 1, 1);  
          v_quote_pos2 := INSTR(v_line, '"', 1, 2);  
          --Extract the inner string  
          v_enclosed_str := SUBSTR(v_line, v_quote_pos1 + 1, v_quote_pos2 - v_quote_pos1 - 1);  
          --perform the replacement  
          v_line := SUBSTR(v_line, 0, v_quote_pos1 - 1) || REPLACE(v_enclosed_str, ',', '<') || SUBSTR(v_line, v_quote_pos2 + 1);  
        END LOOP; 

        -- Convert comma to : to use wwv_flow_utilities </span>  
        v_line := REPLACE (v_line, ',', ':');  
        v_line := REPLACE (v_line, '<', ',');  
        v_line := REPLACE (trim(v_line), '-', NULL);  
        --v_line := REPLACE (trim(v_line), '"', NULL);  
        -- Convert each column separated by : into array of data </span>    
        v_data_array := wwv_flow_utilities.string_to_table (v_line);  
        --Check to see if the row of column headers has already been parsed through  
        IF(v_first_line_done != true)THEN   
          v_first_line_done := true;  
          --Check column order in spreadsheet  
          IF(v_data_array(1)    LIKE '%Username%' AND
              v_data_array(2)  LIKE '%NDN%' AND
              v_data_array(3)  LIKE '%PCFN%') THEN   
            v_error_cd := 0;  
            v_line := NULL;  
          ELSE  
            v_error_cd := 1;  
          END IF;  
        --If first line is done and the column order is correct then  
        ELSIF(v_first_line_done = true AND v_error_cd = 0) THEN   
          -- Insert data into target table </span>    
          EXECUTE IMMEDIATE 'insert into TEMP_MM_UPDATE   
          (USERNAME,
           RPT_FLAG,
           PCFN)
          values (:1,:2,:3)'   
           USING   
            v_data_array(1),   
            v_data_array(2),   
            v_data_array(3);    
           -- Clear out    
            v_line := NULL; v_sr_no := v_sr_no + 1; 
        END IF;  
      END IF;
    exception
      WHEN OTHERS then
        v_errmsg := SQLERRM;
        insert into temp_mm_update (username,error_desc)
        values (:P1_USER_ID, v_errmsg);
v_line := NULL; v_sr_no := v_sr_no + 1; 
  END;
  END LOOP;

  DELETE FROM WWV_FLOW_FILES where name = :P2_FILE_UPLOAD;
  DELETE FROM TEMP_MM_UPDATE WHERE USERNAME IS NULL AND PCFN IS NULL;  
  IF(v_error_cd = 1) THEN  
    INSERT INTO temp_mm_update (USERNAME, ERROR_DESC)  
    VALUES (:P1_USER_ID, 'Error. Please check column order in spreadsheet.');  
  END IF;
EXCEPTION
  WHEN NO_DATA_FOUND THEN
    insert into temp_mm_update (username,error_desc)
    values (:P1_USER_ID, 'No Data Found.');
  WHEN OTHERS then
    v_errmsg := SQLERRM;
    insert into temp_mm_update (username,error_desc)
    values (:P1_USER_ID, v_errmsg);
END;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM