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.

5

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

9 Comments

SQL Error [102] [S0001]: Incorrect syntax near '/tmp/orders.txt'. Incorrect syntax near '/tmp/orders.txt'. Incorrect syntax near '/tmp/orders.txt'.
i try it@Madhur Bhaiya
I try both ways Same Error will be come.@MadhurBhaiya
@R.Gopalakrishnan for MS SQL Server, please check this answer: stackoverflow.com/a/6354143/2469308 There is no SELECT INTO OUTFILE in MS Sql server
Thanks!!@Madhur Bhaiya
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.