SQL Server Management Studio 中是否有 SELECT ... INTO OUTFILE 等效项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6354130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:59:06  来源:igfitidea点击:

Is there a SELECT ... INTO OUTFILE equivalent in SQL Server Management Studio?

sqlsql-serversql-server-2008

提问by Legend

MySQL had a nifty command SELECT ... INTO OUTFILEthat could write the result set into a file (CSV format or some other optional format).

MySQL 有一个漂亮的命令SELECT ... INTO OUTFILE,可以将结果集写入文件(CSV 格式或其他一些可选格式)。

I am currently using SQL Server Management Studio to query an MS-SQL backend server. I have multiple SQL queries and would like to write the output result set into a file. Is there any way I could store the results from a query directly into a file?

我目前正在使用 SQL Server Management Studio 来查询 MS-SQL 后端服务器。我有多个 SQL 查询,想将输出结果集写入文件。有什么办法可以将查询的结果直接存储到文件中吗?

回答by gbn

In SSMS, "Query" menu item... "Results to"... "Results to File"

在 SSMS 中,“查询”菜单项...“结果到”...“结果到文件”

Shortcut = CTRL+shift+F

快捷键 = CTRL+shift+F

You can set it globally too

你也可以全局设置

"Tools"... "Options"... "Query Results"... "SQL Server".. "Default destination" drop down

“工具”...“选项”...“查询结果”...“SQL Server”...“默认目标”下拉菜单

Edit: after comment

编辑:评论后

In SSMS, "Query" menu item... "SQLCMD" mode

在SSMS中,“查询”菜单项...“SQLCMD”模式

This allows you to run "command line" like actions.

这允许您像操作一样运行“命令行”。

A quick test in my SSMS 2008

在我的 SSMS 2008 中进行快速测试

:OUT c:\foo.txt
SELECT * FROM sys.objects

Edit, Sep 2012

编辑,2012 年 9 月

:OUT c:\foo.txt
SET NOCOUNT ON;SELECT * FROM sys.objects

回答by Alex Aza

In SQL Management Studio you can:

在 SQL Management Studio 中,您可以:

  1. Right click on the result set grid, select 'Save Result As...' and save in.

  2. On a tool bar toggle 'Result to Text' button. This will prompt for file name on each query run.

  1. 右键单击结果集网格,选择“将结果另存为...”并保存。

  2. 在工具栏上切换“结果到文本”按钮。这将在每次查询运行时提示输入文件名。

If you need to automate it, use bcp tool.

如果您需要自动化,请使用 bcp 工具。