views:

224

answers:

2

Hello, 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

+1  A: 

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

Mark Streatfield
It was not exactly what I was expecting but I've come to realize it may be the only way.
superspace
+2  A: 

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)

shantanuo