插入前删除现有数据

时间:2012-10-08 10:52:30

标签: oracle plsql oracle11g plsqldeveloper

我想在插入基于ids之前删除现有数据。因此,如果传入文件中有id,则应删除现有ID,然后输入数据。像这样:

之前的数据库表:

--------------------------------
| ID  |  DATA                  |
--------------------------------
| 1   |  This is data for ID 1 |
| 2   |  This is data for ID 2 |
| 3   |  This is data for ID 3 |
--------------------------------

收件人文件:

-----------------------------------------
| ID  |  DATA                           |
-----------------------------------------
| 1   |  This is new data for ID 1      |
| 2   |  This is new data for ID 2      |
| 4   |  This is new data for ID 4      |
| 1   |  This is also new data for ID 1 |
-----------------------------------------

DATABASE TABLE AFTER:

-----------------------------------------
| ID  |  DATA                           |
-----------------------------------------
| 1   |  This is new data for ID 1      |
| 2   |  This is new data for ID 2      |
| 4   |  This is new data for ID 4      |
| 1   |  This is also new data for ID 1 |
| 3   |  This is data for ID 3          |
-----------------------------------------

这是我的程序。

CREATE OR REPLACE PROCEDURE absence_records_in AS
   l_v_file      UTL_FILE.file_type;
   l_filename    VARCHAR2(128);
   l_buffer      VARCHAR2(4096);
   l_start       BINARY_INTEGER := 1;
   l_pos         BINARY_INTEGER;
   TYPE typ_cols IS TABLE OF VARCHAR2(4096) INDEX BY BINARY_INTEGER;
   tbl_cols      typ_cols;
   l_col_no      BINARY_INTEGER;
   l_count       BINARY_INTEGER := 0;
BEGIN
   l_filename := 'records.csv';
   l_v_file := UTL_FILE.fopen('DIR_FENIX_IN', l_filename, 'R');
   UTL_FILE.get_line(l_v_file, l_buffer);
   LOOP
       BEGIN
           UTL_FILE.get_line(l_v_file, l_buffer);
           IF l_buffer IS NULL THEN
               EXIT;
           END IF;
           l_col_no := 1;
           l_start  := 1;
           WHILE INSTR(l_buffer, ';', 1, l_col_no) !=0 
           LOOP
               l_pos := INSTR(l_buffer, ';', 1, l_col_no);
               tbl_cols(l_col_no) := SUBSTR( l_buffer, l_start, l_pos - l_start);
               l_start  := l_pos + 1;
               l_col_no := l_col_no + 1;
           END LOOP;
           l_start := l_pos + 1;
           tbl_cols(l_col_no) := SUBSTR( l_buffer, l_start);

           DELETE FROM absence_records WHERE id = tbl_cols(1)
           LOG ERRORS INTO in_errors('File: '||l_filename||' => delete operation')  REJECT LIMIT UNLIMITED;

           l_count := l_count + 1;
               EXCEPTION WHEN NO_DATA_FOUND THEN
           EXIT;
       END;
       BEGIN
           UTL_FILE.get_line(l_v_file, l_buffer);
           IF l_buffer IS NULL THEN
               EXIT;
           END IF;
           l_col_no := 1;
           l_start  := 1;
           WHILE INSTR(l_buffer, ';', 1, l_col_no) !=0 
           LOOP
               l_pos := INSTR(l_buffer, ';', 1, l_col_no);
               tbl_cols(l_col_no) := SUBSTR( l_buffer, l_start, l_pos - l_start);
               l_start  := l_pos + 1;
               l_col_no := l_col_no + 1;
           END LOOP;
           l_start := l_pos + 1;
           tbl_cols(l_col_no) := SUBSTR( l_buffer, l_start);

           INSERT INTO absence_records (id, data)
           VALUES (tbl_cols(1), tbl_cols(2))
           LOG ERRORS INTO in_errors('File: '||l_filename|| ' => insert operation')  REJECT LIMIT UNLIMITED;

           l_count := l_count + 1;
               EXCEPTION WHEN NO_DATA_FOUND THEN
           EXIT;
       END;
   END LOOP;
   UTL_FILE.fclose(l_v_file);        
END absence_records_in;

简而言之,我需要启动一个循环,它只删除比较的数据,然后是另一个插入数据的循环。 有任何想法吗?在此先感谢: - )

3 个答案:

答案 0 :(得分:0)

上面稍微不同的转折是合并/插入数据。 看看如何在Oracle中合并数据:Oracle: how to UPSERT (update or insert into a table?)

答案 1 :(得分:0)

我会按照以下方式做到这一点:

  1. 将文件中的数据读入新表TEMP
  2. 删除所有ID为TEMP的记录,例如DELETE FROM TABLE WHERE TABLE.ID IN(SELECT ID FROM TEMP)
  3. 将TEMP中的所有记录添加到TABLE

答案 2 :(得分:0)

尝试以下方法:

  1. 在具有异常处理程序的第一个BEGIN的END之后立即关闭第一个循环。
  2. 关闭文件。
  3. 再次打开文件。
  4. 在具有异常处理程序的第二个BEGIN之前立即启动另一个循环。
  5. 请注意,您可以只执行UTL_FILE.fseek(文件,0)而不是(2)和(3)。

    分享并享受。