Friday, August 29, 2014

MySQL trigger write data to file

Solution 1:

CREATE TRIGGER `insert_cust_fedex_trackernum_lead` AFTER INSERT ON `cust_fedex_trackernum` FOR EACH ROW BEGIN
IF NEW.invoiceNum != '' AND NEW.invoiceNum IS NOT NULL AND NEW.leadTracker = NEW.trackerNum THEN
INSERT INTO cust_fedex_trackernum_lead (idInvoice, idDealer, invoiceNum, trackerNum, created) SELECT tITRec.lId, tITRec.lVenCusId , NEW.invoiceNum, NEW.trackerNum, NOW() FROM tITRec WHERE tITRec.sSource1 = NEW.invoiceNum AND tITRec.nJournal = 8 AND tITRec.bReversed = 0 LIMIT 0, 1;
SELECT NOW() INTO OUTFILE 'd:\\test.txt' LINES TERMINATED BY '\n';
END IF;
END;

Note: If you use INTO DUMPFILE instead of INTO OUTFILE, MySQL writes only one row into the file, without any column or line termination and without performing any escape processing. This is useful if you want to store a BLOB value in a file.

Solution 2:

MySQL UDF (User-defined function)

You can add functions as native (built-in) MySQL functions. Native functions are compiled into the mysqld server and become available on a permanent basis.

http://www.mysqludf.org/

No comments: