如何将 SQL Server 2005 查询导出到 CSV
我想将一些 SQL Server 2005 数据导出为 CSV 格式(用引号以逗号分隔)。 我可以想到很多复杂的方法来做到这一点,但我想以正确的方式做到这一点。 我看过 bcp,但我不知道如何在字段周围加上引号(除了将它们连接到字段值,这很难看)。 我想我可以用 sqlcmd 和 -o 来做到这一点,但出于同样的原因,这看起来很难看。
有bcp的方法吗?
有没有合理的 sqlcmd 方法来做到这一点?
Management Studio 中是否内置了一些我刚刚忽略的很棒、简单的实用程序?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
在 Management Studio 中,选择数据库,右键单击并选择
任务 -> 导出数据
。 在那里,您将看到导出为不同类型格式的选项,包括 CSV、Excel 等。您还可以从查询窗口运行查询并将结果保存为 CSV。
In Management Studio, select the database, right-click and select
Tasks->Export Data
. There you will see options to export to different kinds of formats including CSV, Excel, etc.You can also run your query from the Query window and save the results to CSV.
在 Management Studio 中,将查询选项设置为输出到文件,并在选项->查询结果中将输出设置为使用逗号作为分隔符输出到文件。
In management studio, set query options to output to file, and in options->query results set output to file to output using comma as delimiter.
如果你不能使用 Management studio,我使用 sqlcmd。
这是从命令行执行此操作的快速方法。
If you can not use Management studio i use sqlcmd.
That is the fast way to do it from command line.
我必须比 Sijin 所说的多做一件事,才能使其在 SQL Server Management Studio 2005 中正确添加引号。转到
Tools->Options->Query Results->Sql Server->Results To网格
选中此选项旁边的复选框:
保存 .csv 结果时引用包含列表分隔符的字符串
注意:上述方法不适用于 SSMS 2005 Express! 据我所知,使用 SSMS 2005 Express 将结果导出到 .csv 时无法引用字段。
I had to do one more thing than what Sijin said to get it to add quotes properly in SQL Server Management Studio 2005. Go to
Tools->Options->Query Results->Sql Server->Results To Grid
Put a check next to this option:
Quote strings containing list separators when saving .csv results
Note: the above method will not work for SSMS 2005 Express! As far as I know there's no way to quote the fields when exporting results to .csv using SSMS 2005 Express.
是的,如果您只是想将查询结果保存到 CSV,那么 Management Studio 中有一个非常简单的实用程序。
右键单击结果集,选择“结果另存为”。 默认文件类型是 CSV。
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.
如果它符合您的要求,并且您经常执行此操作或希望将其构建到生产过程中,则可以在命令行上使用bcp。
这是一个链接< /a> 描述配置。
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.
对于即席查询:
以网格模式 (CTRL+D) 显示结果,运行查询,单击结果网格中的左上角框,粘贴到 Excel,另存为 CSV。 您也许可以直接粘贴到文本文件中(现在无法尝试)
或者“结果到文件”也有 CSV 选项
或“结果到文本”带有逗号分隔符
工具..选项和查询下的所有设置..选项(我想,无法检查)也
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
上设置 nocount ,使用 -w2000 将每一行保留在一行上。
在引号
set nocount on
the quotes are there, use -w2000 to keep each row on one line.
在 SQL 2005 中,这很简单:
1、打开SQL Server Management Studio,将需要的sql语句复制到TSQL中,如exec sp_whatever
2.查询->结果到网格
3.突出显示sql语句并运行它
4. 突出显示数据结果(左键单击结果网格的左上区域)
5. 现在右键单击并选择将结果另存为
6. 在“保存类型”中选择“CSV”,输入文件名,选择位置,然后单击“保存”。
简单的!
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!
在 Sql Server 2012 - Management Studio 中:
解决方案 1:
执行查询
右键单击结果窗口
从菜单中选择将结果另存为
选择 CSV
解决方案 2:
右键单击数据库
选择任务、导出数据
选择源数据库
选择目标:平面文件目标
选择文件名
选择格式 - 分隔
选择表或编写查询
选择列分隔
符 注意:
您可以选择一个文本限定符来分隔文本字段,例如引号。
如果您有一个包含逗号的字段,请不要使用逗号作为分隔符,因为它不会转义逗号。 您可以选择列分隔符,例如竖线:| 而不是逗号或制表符。 否则,请编写一个转义逗号或分隔 varchar 字段的查询。
您需要使用的转义字符或文本限定符取决于您的要求。
In Sql Server 2012 - Management Studio:
Solution 1:
Execute the query
Right click the Results Window
Select Save Results As from the menu
Select CSV
Solution 2:
Right click on database
Select Tasks, Export Data
Select Source DB
Select Destination: Flat File Destination
Pick a file name
Select Format - Delimited
Choose a table or write a query
Pick a Column delimiter
Note:
You can pick a Text qualifier that will delimit your text fields, such as quotes.
If you have a field with commas, don't use you use comma as a delimiter, because it does not escape commas. You can pick a column delimiter such as Vertical Bar: | instead of comma, or a tab character. Otherwise, write a query that escapes your commas or delimits your varchar field.
The escape character or text qualifier you need to use depends on your requirements.
我认为最简单的方法是使用 Excel。
如果您有较新版本的 Excel,您可以从 PowerPivot 引入数据,然后将此数据插入表中。
I think the simplest way to do this is from Excel.
If you have a newer version of Excel you could bring the data in from PowerPivot and then insert this data into a table.
SSIS 是做到这一点的一个很好的方法。 然后可以使用 SQL Server 代理作业来安排此操作。
SSIS is a very good way to do this. This can be then scheduled using SQL Server Agent jobs.
您可以使用以下 Node.js 模块轻松完成此操作:
https://www. npmjs.com/package/mssql-to-csv
You can use following Node.js module to do it with a breeze:
https://www.npmjs.com/package/mssql-to-csv