2009-06-29 76 views
0

中的數據內容創建CSV文件如何在更新表中的特定列後調用觸發器並創建一個內容相同的CSV文件表?我正在使用Oracle 10gOracle 10g用於寫入觸發器的問題,將使用表

+0

你想要的csv文件被寫入到哪裏? – 2009-06-29 07:29:39

+0

對不起,但我認爲這是一個非常糟糕的主意。您可能遇到並需要處理的一些問題 - 例如, (a)如果用戶執行更新,然後回滾,則將使用表中從未存在的數據創建csv文件; (b)在某些情況下(由於鎖定問題),如果在表中的多行中運行更新,觸發器可能會多次運行(Oracle可能需要回滾並重新啓動)。 t.b.c. – 2009-06-30 01:11:29

回答

0

這應該這樣做(當然,修改首先滿足您的表/列):

create or replace trigger create_csv 
after update on table_name for each row 
declare 
    file_handle text_io.file_type; 
    cursor c_table_name is 
    select foo, bar, baz 
     from table_name 
    ; 
begin 
    file_handle := text_io.fopen('path/to/csv/file'); 
    for table_row in c_table_name loop 
    text_io.put_line(
     file_handle, 
     replace(table_row.foo, ',', '\,')||','|| 
     replace(table_row.bar, ',', '\,')||','|| 
     replace(table_row.baz, ',', '\,') 
    ); 
    end loop; 
    text_io.fclose(file_handle); 
end;