3

How to save output of a Select query to a .json file or .txt file?using MSSQL Server

Table A contains id,name,address

I tried the query below,

SELECT * from A INTO OUTFILE '/tmp/orders.txt';

but it produced an Error,

SQL Error [156] [S0001]: Incorrect syntax near the keyword 'INTO'.
  Incorrect syntax near the keyword 'INTO'.

How do i resolve the error?. Even if it's only possible to store the data in a .txt file, it will be okay.

Thanks in advance.

Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
R.Gopalakrishnan
  • 95
  • 1
  • 1
  • 10

1 Answers1

4

As per the original question, which was tagged as MySQL: Your syntax is wrong. As per MySQL Documentation, FROM clause comes after INTO OUTFILE clause. Following should work in MySQL:

SELECT * INTO OUTFILE '/tmp/orders.txt'
FROM A

In order to get Comma-separated values in the text file, you can do the following instead (in MySQL):

SELECT * INTO OUTFILE '/tmp/orders.txt' 
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM A;

For MS SQL Server: There is no INSERT INTO OUTFILE clause available. Instead, a different solution is proposed using SQL Server Management Studio at: https://stackoverflow.com/a/6354143/2469308

Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57