Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Export SQL Query to TXT Using Command Line

I want to export select * from table results to a text file from the command line in linux. How should I do this?

Thanks, Jean

like image 608
X10nD Avatar asked Nov 22 '10 08:11

X10nD


People also ask

How do I export a SQL query to a text file?

However, if you prefer to export SQL query results to a text file via a Wizard, we have your back. To begin with, right-click the database in SQL Server Management Studio or SSMS. Then, select the Import or Export data option and head to Export Data under Tasks. Next, open the SQL Server Import and Export wizard.

How do I save a SQL query result to a file?

Click Query, and then click Results to File. Enter and then execute the SQL statement. In the Save Results dialog box, specify the following settings: Save In: Select a directory in which to save the file.


1 Answers

look at link

you only need to add this to the query

select * from table INTO OUTFILE '/tmp/myfilename.txt'

you can improve this to csv file (using it in excel latter)

like :

INTO OUTFILE '/tmp/myfilename.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
like image 89
Haim Evgi Avatar answered Sep 30 '22 05:09

Haim Evgi