10

I have a problem loading data into specific columns of an table. The CSV file is build dynamic with the default fields ID, LAST_REFRESH, ALIAS1 and may contain ALIAS2 to ALIAS8. Current CSV only contains ALIAS1-4

The MySQL table contains the columns ID, LAST_REFRESH, ALIAS1-ALIAS8. My code for the first file already fails. Code after variables are set is:

LOAD DATA LOCAL INFILE 'C:\\temp\\\OSS001'
INTO TABLE REJECTS (ID, REFRESH_DATE, ALIAS1, ALIAS2, ALIAS3, ALIAS4)
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES

But unfortunately i still receive the following error:

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 TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'

Anybody knows what i'm doing wrong?

2 Answers 2

16

The column names have to be specified last. Read more about it here.

LOAD DATA LOCAL INFILE 'C:\\temp\\\OSS001'
INTO TABLE REJECTS 
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(ID, REFRESH_DATE, ALIAS1, ALIAS2, ALIAS3, ALIAS4, ALIAS5, ALIAS6, ALIAS7, ALIAS8)
Sign up to request clarification or add additional context in comments.

2 Comments

I think you may have to catch this in your application logic. Not on database layer.
Already did it. I added a variable in the application that group_concats the alias fields :)
2

Remove the columns:

LOAD DATA LOCAL INFILE 'C:\\temp\\\OSS001'
INTO TABLE REJECTS FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.