Question

I'm trying to format the file name for outfile using row data.

I've tried the following with no success:

SELECT t1.field1
FROM table1 t1
LIMIT 1
INTO OUTFILE CONCAT( '/my_path/', t1.field2, '.txt' );

Can anyone recommend alternatives?

Thanks

Was it helpful?

Solution

In MySQL it is not possible to specify the outfile as an expression/variable. The only way I was able to do this was to prepare the statement (and in my case, as part of a stored procedure):

SET @query = CONCAT("SELECT t1.field1 FROM table1 t1 LIMIT 1 INTO OUTFILE /my_path/", myvar, ".txt" );
PREPARE statement FROM @query;
EXECUTE statement;

I don't think you can reference a column/field from your source table in this manner however.

You would have to do an initial SELECT statement to get the value of t1.field1 (perhaps using SELECT ... INTO syntax), and then use this variable with the syntax above

OTHER TIPS

You can also do it at command prompt:

mysql -h172.29.0.1 -uroot -pPassWd -e"SELECT t1.field1 FROM table1 t1 LIMIT 1" > '/my_path/', t1.field2, '.txt'

(untested)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top