0

我有桌子:

create table data_table(
  id number,
  csv clob
);

csv 包含如下数据:

CONST  ID  DATA_1  DATA_2 ..  DATA_N
100    1   asd     123    ..  df
100    2   fgh     346    ..  fg

我必须按 id 查找行,并更改一些 DATA 值,然后保存。最好的方法是什么?

起初我想使用 DBMS_UTILITY.comma_to_table,但发现 http://asktom.oracle.com/pls/asktom/f?p=100:11:0%3a%3a%3a%3aP11_QUESTION_ID:1415803954123

例子

前:

insert into data_table values(1, '100;1;asd;123;df;'||chr(10)||
                                 '100;2;fgh;346;fg;'||chr(10)  );

insert into data_table values(2, '101;2;fgh;346;ff;'||chr(10)||
                                 '101;3;gfd;456;gh;'}}chr(10)  );

execute csv_update(2);

从 data_table 中选择 csv;

100;1;asd;123;df;
100;2; XXX ; 000 ;fg;

101;2; XXX ; 000 ;fg;
101;3;gfd;456;gh;

4

1 回答 1

0

我的决定:

PROCEDURE update_clob_csv_row_by_id(id in VARCHAR2)
IS
  f_regexp varchar2(250) := '\d+;('||id||');[^'||CHR(10)||']*'||CHR(10);
  f_old_line varchar2(2000);
  f_new_line VARCHAR2(2000);
  f_count_line NUMBER;
BEGIN
  FOR CR IN (SELECT * FROM data_table ) LOOP
    -- find count of row with id(it can be not unique)
    SELECT nvl(REGEXP_COUNT(csv ,f_regexp), 0) COUNT_LINE
    INTO f_count_line
    FROM data_table
    WHERE id = cr.id;
    -- update all csv row
    FOR I IN 1..f_count_line LOOP
      -- find by reg_pattern the row for update 
      SELECT REGEXP_SUBSTR(EXPORT_DATA, f_regexp, 1, I) SUBSTR_LINE  
      INTO f_old_line
      FROM card_export
      WHERE id = cr.id;
      -- form new line 
      f_new_line := REPLACE_VALUE_IN_ROW(f_old_line);
      -- replace the old value with new 
      UPDATE card_export SET
      EXPORT_DATA = REPLACE(EXPORT_DATA, f_old_line, f_new_line)
      WHERE ID  = CR.ID;           
    END LOOP;
  END LOOP;
END DELETE_CARD_CONF_DATA;
于 2013-02-17T13:19:41.303 回答