3

I know this is a known issue with older versions of MySQL Workbench but I'm certain I have the latest version so that is definitely not the cause. The code is the following, I will also explain why I am using this approach.

WITH temp as
(
LOAD DATE INFILE 'File Location.csv'
IGNORE
INTO TABLE temp
FIELDS TERMINATED BY '^~'
LINES TERMINATED BY '\r\n'
IGNORE 1 ROWS
(RUN_DATE, PROC-DT, STL_DT, TRD_DT, CHG_DT, SENT_ADP_DT, ACKN_ADP_DT, ORIG_PROC_DT)
)
select * from temp; 

So, in a previous query, I loaded the entirety of the INFILE into a table that I need, but these fields in specific (all date fields) were not being populated correctly. I'm trying to create a temp table so that I may test some preprocessing logic on these fields (using the SET argument for LOAD DATA) and then hopefully inject these columns correctly into my permanent table with the appropriate logic. However, I have no idea why I'm getting this confounded error. Thanks in advance for the help.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • 1
    'I know this is a known issue with older versions of MySQL Workbench' - not with workbench but with mysql server prior to version 8 and you cannot load data infile in a cte. – P.Salmon Jan 06 '21 at 15:28

1 Answers1

3

I have several comments, in no particular order:

  • You can't create a table with LOAD DATA. You must create the table first.
  • You can't run LOAD DATA inside a CTE. The CTE can only have a SELECT inside.
  • You don't need a CTE for this.
  • Your error on 'WITH' suggests aren't using a version of MySQL that supports CTE syntax.
  • The syntax support depends on your version of MySQL Server, not MySQL Workbench. Workbench is just a client, it can connect to any version of MySQL Server. Try running the query SELECT @@version; and that will tell you the version of MySQL Server you are connected to. If it's less than version 8.0, it doesn't support CTE syntax.

You should create the table, then load data, then select from it.

CREATE TEMPORARY TABLE temp ( ... );

LOAD DATE INFILE 'File Location.csv'
IGNORE
INTO TABLE temp
FIELDS TERMINATED BY '^~'
LINES TERMINATED BY '\r\n'
IGNORE 1 ROWS
(RUN_DATE, PROC-DT, STL_DT, TRD_DT, CHG_DT, SENT_ADP_DT, ACKN_ADP_DT, ORIG_PROC_DT);

SELECT * FROM temp; 

Unlike a CTE, the temporary table will persist until your client ends its session. Then like any temporary table, it will be dropped automatically.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • This reply has been incredibly helpful thank you so much. I wish I could up vote this more than once. I figured LOAD DATA would not be supported inside the CTE, but I couldn't find anything on Google, maybe I was asking the wrong questions. As for the server version, it is version 8.0.22 so CTE syntax should be supported. Again, thank you a million for the speedy assistance! – Jesus Rodriguez Jan 06 '21 at 15:42
  • @JesusRodriguez Might be nice to upvote it once though – RiggsFolly Jan 06 '21 at 16:06
  • @RiggsFolly "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." – Jesus Rodriguez Jan 06 '21 at 16:08
  • 1
    @JesusRodriguez See shows what I know doesnt it :) – RiggsFolly Jan 06 '21 at 16:09