2013-02-06 245 views
0

CSV工作,我有表:與甲骨文

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行,並改變一些數據值,並保存。 做什麼是最好的方法?

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

例如

BEFORE:

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; ; fg;

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

+0

你能表現出一定的樣本輸入和輸出數據?您是否嘗試根據CSV文件中的數據更新表格? –

+0

爲什麼你不使用外部表? –

+0

我一直以爲我可以創建外部表僅用於文件/ – yaroslavTir

回答

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;