Posts Tagged ‘help’
Wanted: MySQL Magic Chanting April 12, 2010 | 09:30 am

Here’s my problem. I’m working on integrating the EPA MOVES model, which means I’m living on MySQL. I need to load large amounts of data to the database, so I’m using the MySQL “LOAD DATA INFILE” command. The problem is that while the data file format is always the same, the tables that I need to load are going to differ, as are the file locations.

Complicating the issue is that I’m calling this from GAMS, which means any kind of advanced programming stunts (like parsing the file or dynamically generating SQL on the client) are pretty much out.

So I’d like to do something like this on the server:

LOAD DATA INFILE @data_file INTO TABLE @table_name

Except that MySQL really dislikes the idea of using variables to specify the table name.

“No problem,” I think to myself, “I’ll generate dynamic SQL on the server using PREPARE.” (Yes, I do think in hyperlinks.) A bit of cutesy abuse of CONCAT and PREPARE later, I’m ready to roll.

The catch? ERROR 1295: MySQL doesn’t support LOAD DATA in prepared statements. Nerts.

At this point, I’m seriously considering automatically generating a stored procedure load_data_TABLENAME for every table name in the database, and that’s making me feel sad deep inside. An alternative is to add a layer of indirection via a call to .Net, but the result will split some of the database logic which I’d like to keep consolidated on the server, not to mention complicating a system that I’d much prefer to simplify.

Any suggestions on how to make this work?