1

I am attempting to generate a csv file including headers from a table in my MySQL 8.0 database. After a lot of searching I found the following suggest syntax:

 SELECT "ansId","assmt","ques","answerCode","details"
 FROM ayanswer
 UNION
 SELECT ansId,assmt,ques,answerCode,details
 FROM ayanswer
 INTO OUTFILE 'c:/data/test1.csv'
 FIELDS TERMINATED by ','
 OPTIONALLY ENCLOSED BY '"'
 FIELDS ESCAPED BY '\\';

However when I run this statement in mysql I get the following error: ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FIELDS ESCAPED BY '\'' at line 9

According to the MySQL Server 8.0 reference manual this syntax should be correct. So what am I doing wrong?

asked Sep 12, 2020 at 20:22

1 Answer 1

2

This seems to be a simple syntax issue. Use the below query instead which basically doesn't use FIELDS before ESCAPED by '\\' since it's already specified before TERMINATED by ','.

 SELECT "ansId","assmt","ques","answerCode","details" FROM ayanswer
 UNION
 SELECT ansId,assmt,ques,answerCode,details FROM ayanswer
 INTO OUTFILE 'c:/data/test1.csv'
 FIELDS TERMINATED by ','
 OPTIONALLY ENCLOSED BY '"'
 ESCAPED by '\\';

Supporting Resources

answered Sep 12, 2020 at 20:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.