TSQL - 将查询导出到 xls /xslx / csv

发布于 2024-10-10 09:12:28 字数 154 浏览 3 评论 0原文

我在 TSQL 中有一个复杂的动态查询,我想将其导出到 Excel。 [结果表包含文本长度超过 255 个字符的字段,如果重要的话]

我知道我可以使用 Management Studio 菜单导出结果,但我想通过代码自动执行此操作。你知道怎么做吗?

提前致谢。

I have a complicated dynamic query in TSQL that I want to export to Excel.
[The result table contains fields with text longer than 255 chars, if it matters]

I know I can export result using the Management Studio menus but I want to do it automatically by code. Do you know how?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梅窗月明清似水 2024-10-17 09:12:29

您可以查看sp_send_dbmail。这允许您在查询运行后发送一封电子邮件,其中包含结果集的附加 CSV。显然,此方法的可行性取决于结果集的大小。

链接文档中的示例:

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'AdventureWorks2008R2 Administrator',
@recipients = '[email protected]',
@query = 'SELECT COUNT(*) FROM AdventureWorks2008R2.Production.WorkOrder
              WHERE DueDate > ''2006-04-30''
              AND  DATEDIFF(dd, ''2006-04-30'', DueDate) < 2' ,
@subject = 'Work Order Count',
@attach_query_result_as_file = 1 ;

You could have a look at sp_send_dbmail. This allows you to send an email from your query after it's run, containing an attached CSV of the resultset. Obviously the viability of this method would be dependent on how big your resultset is.

Example from the linked document:

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'AdventureWorks2008R2 Administrator',
@recipients = '[email protected]',
@query = 'SELECT COUNT(*) FROM AdventureWorks2008R2.Production.WorkOrder
              WHERE DueDate > ''2006-04-30''
              AND  DATEDIFF(dd, ''2006-04-30'', DueDate) < 2' ,
@subject = 'Work Order Count',
@attach_query_result_as_file = 1 ;
薄暮涼年 2024-10-17 09:12:29

一种方法是使用 bcp,您可以从命令行调用它 - 查看该参考文献中的示例,特别是查看有关 -t 参数的信息,您可以使用它来设置字段终止符(对于 CSV)。 指定字段和行终止符上有此链接参考。

或者,直接使用 TSQL,您可以使用 OPENROWSET 如 Pinal Dave 此处所述

更新:
回复:2008 64Bit & OPENROWSET - 我不知道这一点,快速挖掘抛出 MSDN 论坛上的此内容,并给出了链接。有什么帮助吗?

除此之外,其他选项包括编写 SSIS 包或使用 SQL CLR 在 .NET 中编写导出过程以直接从 SQL 调用。或者,您可以通过 xp_cmdshell 从 TSQL 调用 bcp - 您必须启用它,但这将打开 SQL Server 可能的“攻击面”。我建议查看此讨论

One way is to use bcp which you can call from the command line - check out the examples in that reference, and in particular see the info on the -t argument which you can use to set the field terminator (for CSV). There's this linked reference on Specifying Field and Row Terminators.

Or, directly using TSQL you could use OPENROWSET as explained here by Pinal Dave.

Update:
Re;: 2008 64Bit & OPENROWSET - I wasn't aware of that, quick dig throws up this on MSDN forums with a link given. Any help?

Aside from that, other options include writing an SSIS package or using SQL CLR to write an export procedure in .NET to call directly from SQL. Or, you could call bcp from TSQL via xp_cmdshell - you have to enable it though which will open up the possible "attack surface" of SQL Server. I suggest checking out this discussion.

醉生梦死 2024-10-17 09:12:29

我需要接受动态查询并将结果保存到磁盘,以便我可以通过 Web 应用程序下载它。

插入数据源对我来说并不成功,因为我一直在努力让它工作。

最终我将查询从 SSMS 发送到 powershell
在这里阅读我的帖子
如何创建通过在 R2008 sql 服务器上运行现有的存储过程或该过程的 sql 语句来在服务器上创建一个文档

单引号是一个问题,一开始我没有修剪我的查询并将其写在一行上,所以它sql studio 中有换行符,这实际上很重要。

I needed to accept a dynamic query and save the results to disk so I can download it through the web application.

insert into data source didn't work out for me because of continued effort in getting it to work.

Eventually I went with sending the query to powershell from SSMS
Read my post here
How do I create a document on the server by running an existing storedprocedure or the sql statement of that procedure on a R2008 sql server

Single quotes however was a problem and at first i didn't trim my query and write it on one line so it had line breaks in sql studio which actually matters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文