How to export SQL Server 2005 query to CSV

2019-01-13 03:22发布

I want to export some SQL Server 2005 data to CSV format (comma-separated with quotes). I can think of a lot of complicated ways to do it, but I want to do it the right way. I've looked at bcp, but I can't figure out how to put the quotes around the fields (except concatenating them to the field values, which is ugly). I guess I could do it with sqlcmd and -o, but that seems ugly for the same reason.

Is there a bcp way to do it?

Is there a reasonable sqlcmd way to do it?

Is there some great, simple utility built into the Management Studio that I'm just overlooking?

13条回答
贪生不怕死
2楼-- · 2019-01-13 03:24

SSIS is a very good way to do this. This can be then scheduled using SQL Server Agent jobs.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-13 03:34

For adhoc queries:

Show results in grid mode (CTRL+D), run query, click top left hand box in results grid, paste to Excel, save as CSV. You may be able to paste directly into a text file (can't try it now)

Or "Results to file" has options too for CSV

Or "Results to text" with comma separators

All settings under Tool..Options and Query.. options (I think, can't check) too

查看更多
孤傲高冷的网名
4楼-- · 2019-01-13 03:35

In SQL 2005, this is simple: 1. Open SQL Server management studio and copy the sql statement you need into the TSQL , such as exec sp_whatever 2. Query->Results to Grid 3. Highlight the sql statement and run it 4. Highlight the data results (left-click on upper left area of results grid) 5. Now right-click and select Save Results As 6. Select CSV in the Save as type, enter a file name, select a location and click Save.

Easy!

查看更多
疯言疯语
5楼-- · 2019-01-13 03:36

Yeah, there is a very simple utility in Management Studio, if you're just looking to save query results to a CSV.

Right click on the result set, the select "Save Results As". The default file type is CSV.

查看更多
对你真心纯属浪费
6楼-- · 2019-01-13 03:36

If it fits your requirements, you can use bcp on the command line if you do this frequently or want to build it into a production process.

Here's a link describing the configuration.

查看更多
爷、活的狠高调
7楼-- · 2019-01-13 03:37

I think the simplest way to do this is from Excel.

  1. Open a new Excel file.
  2. Click on the Data tab
  3. Select Other Data Sources
  4. Select SQL Server
  5. Enter your server name, database, table name, etc.

If you have a newer version of Excel you could bring the data in from PowerPivot and then insert this data into a table.

查看更多
登录 后发表回答