This simple stored procedure can be used to export a table to a csv file. This procedure does not include csv headers.
DROP PROCEDURE IF EXISTS table_to_csv;
DELIMITER $
CREATE PROCEDURE table_to_csv( IN table_name CHAR(40), IN path VARCHAR(100) )
BEGIN
SET @TableName = table_name;
SET @Path = path;
SET @Sql = CONCAT( "SELECT * FROM ", @TableName , " INTO OUTFILE '", @Path, @TableName, ".csv' FIELDS ENCLOSED BY '\"' TERMINATED BY ',' );
PREPARE stm1 FROM @Sql;
EXECUTE stm1;
END ;
$
DELIMITER ;
CALL table_to_csv('table_name', 'file_path/');
No comments:
Post a Comment