Reputation: 75
I have a comma-separated list of file names, and I want to create multiple text files in MySQL. Each file should contain some static text.
For example, if my input is:
'file1,file2,file3,file4'
I want to generate:
/var/www/output/file1.txt
containing "SomeContent"
/var/www/output/file2.txt
containing "SomeContent"
/var/www/output/file3.txt
containing "SomeContent"
/var/www/output/file4.txt
containing "SomeContent"
I want to achieve this without using loops.
I tried using SELECT INTO OUTFILE
like this:
SELECT "SomeContent" INTO OUTFILE "/var/www/output/file1.txt";
SELECT "SomeContent" INTO OUTFILE "/var/www/output/file2.txt";
SELECT "SomeContent" INTO OUTFILE "/var/www/output/file3.txt";
SELECT "SomeContent" INTO OUTFILE "/var/www/output/file4.txt";
This works, but I have 50+ file names, so manually writing queries for each file is not practical.
I also tried inserting the values into a temporary table and running:
SELECT "SomeContent", CONCAT('/var/www/output/', name, '.txt') AS filepath
FROM file_names
INTO OUTFILE "/var/www/output/dummy.txt";
But MySQL does not support dynamic filenames in INTO OUTFILE
, so it only writes to dummy.txt
instead of creating multiple files.
Upvotes: 0
Views: 72