在CSV文件 - 文件名中发送查询结果
我正在处理代码,该代码将通过电子邮件发送我的查询结果。通常它可以正常工作,但是我有一个问题:有什么办法可以在文件名中包含当前日期?而不是:
@query_attachment_filename = 'report.csv'
我想拥有类似的东西:
report_getdate()\_.csv
我正在使用SQL Server Management Studio 2016 相关主题:如何以CSV格式发送CSV格式?<<< /a>
我的查询看起来像这样,而且效果很好。只需更改文件的名称,
任何帮助即可。谢谢
DECLARE @AccountSite VARCHAR(255)
DECLARE @Query VARCHAR(MAX)
SET @AccountSite = '[sep= ' + CHAR(13) + CHAR(10) + 'AccountSite]'
SET @Query = 'SELECT
[AccountSite] ' + @AccountSite + '
,[Account Name]
,[Title]
,[Status]
,[Opened Date]
,[Closed Date]
,[Geo]
,[Country]
,[Region]
,[Marketing Name]
,[Case Number]
FROM [Reporting].[dbo].[Rep]'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'BI Server'
,@recipients = '[email protected]'
,@query = @Query
,@subject = 'Report'
,@body = 'Attached is the latest extract of the report'
,@query_attachment_filename = 'report.csv'
,@query_result_separator = ' '
,@attach_query_result_as_file = 1
,@query_result_no_padding= 1
,@exclude_query_output =1
,@append_query_error = 0
,@query_result_header = 1
,@query_result_width = 32767;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在调用 SP 之前定义报告名称
。在此之后,您可以将: 替换
为:
102
会将日期格式设置为YYYY.MM.DD
,例如其他格式请参阅:docs注意:某些格式的文件名会包含非法字符,例如 ie 101,其格式为
mm/dd/yyyy
。但/
在 Windows 下的文件名中无效。You need to define the report name before calling the SP
After this, you can replace:
by something like:
The
102
will format the date asYYYY.MM.DD
, for other formats see: docsNOTE: Some format will contain illegal characters for filenames, like i.e. 101, which formats as
mm/dd/yyyy
. But the/
is not valid within a filename under Windows.