2009-06-29 58 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;