1

I have created a database, a table, and entries in that table through basic SELECT and INSERT INTO commands.

To view the entries I am using the basic query:

USE test1
SELECT * FROM orders 

where test1 is Database and orders is Table name.

I can see the entries.

How can I store the results to a CSV?

With the query

SELECT * FROM orders
INTO OUTFILE '/path/to/file.csv'
FIELDS ESCAPED BY '""'
TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'" 

I am getting an error

Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'INTO'.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

You do have the option to output to csv from within management studio, you can return your grid as normal then right click and click "Save results As..."

or

Using SQLMD as per question How to export SQL Server 2005 query to CSV

sqlcmd -q "select col1,col2,col3 from table" -oc:\myfile.csv -h-1 -s","

Alternative is create it into a variable and then save that via your application e.g.

declare @csv varchar(max) = ""
select @csv += replace(column1, ',','","') + ', ' + replace(column1, ',','","') + char(13) + char(10) from orders
select @csv csv

Regards

Liam

Community
  • 1
  • 1
Liam Wheldon
  • 725
  • 1
  • 5
  • 19
-1

If you're not as familiar with SQL and would like a more graphical based approach, you could always use the Import/Export Wizard. Little more user-friendly to people who may be new to SQL.

Kris Gruttemeyer
  • 872
  • 7
  • 19