0

I've got a table I need to split into many .csv files with each file have a group of rows based on the contents of one of the columns.

I found this solution here: Export Table Into Files Grouping By A Column, and got the query working just fine:

SELECT concat('SELECT * FROM YourTable WHERE col1 = ''',
    col1, ''' INTO OUTFILE '''c:\result-', col1, '.txt'' ', 
    'FIELDS TERMINATED BY '','' OPTIONALLY ENCLOSED BY ''"''',
    'LINES TERMINATED BY ''\n'';')
FROM YourTable
GROUP BY col1

However, when I execute this query, I get a single column of fields where each field has a select statement like this:

SELECT * FROM table WHERE `col1` = 'value' INTO OUTFILE c:\results - value.csv  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY ' "' LINES TERMINATED BY '\n';

How do I execute each of those statements?

Community
  • 1
  • 1
  • It appears your trying to execute a dynamic SQL statement. Here's one prior answer which may help:http://stackoverflow.com/questions/5591338/my-sql-dynamic-query-execute-and-get-ouput-into-a-variable-in-stored-procedure or you can take this approach http://dev.mysql.com/worklog/task/?id=2793 using execute immediate syntax – xQbert Aug 13 '13 at 17:34

1 Answers1

0

you can use your temp-folder as middle-man, and save it as an sql-file there, and then run the file:

SELECT CONCAT('UPDATE a SET b = ', c, ' WHERE d = ', e, ';') AS query
   FROM x INTO OUTFILE '/tmp/update_a.sql';
SOURCE '/tmp/update_a.sql';

easy for one-time querys in the console, for use in software, you should use something better like @xQbert sugested

Puggan Se
  • 5,738
  • 2
  • 22
  • 48
  • I didn't get what you were doing so I exported the output into a file, and then pasted the file into navicat..but I was just being stupid - I see what you're doing now, with the source line pulling the code back in. Nice! – Steve Duncan Aug 13 '13 at 19:55
  • source is nice for debuging to, usaly have a large sql in a text file, and then use source to try to run it – Puggan Se Aug 13 '13 at 20:10