rMafia
rMafia

Reputation: 75

How to create multiple files with different names and content using a comma-separated list without loops?

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:

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.

  1. Is there a way in MySQL to create multiple files dynamically from a comma-separated list without using loops?
  2. If not, is there any SQL-only workaround to achieve this?

Upvotes: 0

Views: 72

Answers (0)

Related Questions