Trigger for creating csv of new data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • couboys10
    New Member
    • Mar 2013
    • 1

    Trigger for creating csv of new data

    I'd like to create a trigger to export to csv the new data added to the table. I've written:

    Code:
    delimiter |
    create trigger trg_customer_csv after insert on customers
    for each row
    begin
    select new.account_code into outfile 'e:/trigfile.csv'
    fields terminated by ';'
    lines terminated by '\n' 
    from customers;
    end;
    |
    this exports the new data once for every record in the table, so I got a csv with the new account_code 361 times.

    I also really wanted to export all of the data, so have tried select new.* but nothing exported at all.

    Any help would be appreciated.
    Last edited by Rabbit; Mar 1 '13, 04:50 PM. Reason: Please use code tags when posting code.
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    "I also really wanted to export all of the data, so have tried select new.* but nothing exported at all."
    replace line 5 with:
    Code:
    select * into outfile 'e:/trigfile.csv'
    This should export all the data in the table (providing the outputfile does not exist)

    or, if you just want the complete last record:
    Code:
    select * 
    into outfile '/tmp/trigfile.csv'
    fields terminated by ';'
    lines terminated by '\n' 
    from customers
    where account_code=new.account_code;
    end;

    Comment

    Working...