views:

709

answers:

2

How can a trigger be written which is invoked after updating a particular column in the table and creates a CSV file with the contents of same table?

I am using Oracle 10g.

A: 
CREATE [OR REPLACE] TRIGGER trigger_name

{BEFORE/AFTER/INSTEAD OF}triggering_event

[WHEN trigger_condition]

[FOR EACH ROW]

trigger_body;

from http://www.exforsys.com/tutorials/oracle-10g/oracle-10g-triggers-and-triggering-events.html

claferri
+1 I rarely see people actually refencing the palce from where they paste their answers. Good of you.!
Mohit Nanda
A: 

This should do it (of course, modify to fit your table/columns first):

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