sp_send_dbmail 附件编码
我在 SQL2005 中使用 sp_send_dbmail 发送一封电子邮件,并将结果放在附件中。 发送附件时,它是 UCS-2 编码的,我希望它是 ANSI 或 UTF-8。
这是 SQL
EXEC msdb.dbo.sp_send_dbmail
@recipients = '[email protected]'
, @query = 'DECLARE @string_to_trim varchar(60);SET @string_to_trim = ''1234''; select rtrim(@string_to_trim), ''tom'''
, @query_result_header=0
, @subject = 'see attach'
, @body= 'temp body'
, @profile_name= N'wksql01tAdmin'
, @body_format = 'HTML'
,@query_result_separator = ','
,@query_attachment_filename = 'results.csv'
,@query_no_truncate = '0'
,@attach_query_result_as_file = 1
我在互联网上看到一些评论说这个问题已在 sql2005 SP2 中修复,但发现情况并非如此。
I am using sp_send_dbmail in SQL2005 to send an email with the results in an attachment. When the attachment is sent it is UCS-2 Encoded, I want it to be ANSI or UTF-8.
Here is the SQL
EXEC msdb.dbo.sp_send_dbmail
@recipients = '[email protected]'
, @query = 'DECLARE @string_to_trim varchar(60);SET @string_to_trim = ''1234''; select rtrim(@string_to_trim), ''tom'''
, @query_result_header=0
, @subject = 'see attach'
, @body= 'temp body'
, @profile_name= N'wksql01tAdmin'
, @body_format = 'HTML'
,@query_result_separator = ','
,@query_attachment_filename = 'results.csv'
,@query_no_truncate = '0'
,@attach_query_result_as_file = 1
I have seen some comments on the internet that this is fixed with sql2005 SP2, but do not find it to be the case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了使文件为 ANSI/UTF-8,请
使用此行以及其他变量更改位于
msdb
中的 sp_send_dbmail:@ANSI_Attachment BIT = 0
ie
然后将此行添加到对 sp_send_dbmail 的调用中:
然后它应该为您提供 ansi 附件而不是 unicode。
In order to have the file be ANSI/UTF-8
alter the sp_send_dbmail that lives in the
msdb
with this line along with the other variables:@ANSI_Attachment BIT = 0
i.e.
and then add this line to your call to sp_send_dbmail:
then it should give you an ansi attachment instead of unicode.
我认为解决您所看到的问题的唯一方法是使用 BCP 将数据转储到平面文件,然后附加该文件。 抱歉我无法提供更多帮助。 :(
I think the only way to get around what you are seeing is to use BCP to dump the data to a flat file and then attach that file. Sorry I couldn't be more help. :(
经过对 SQL Server 2008 R2 的一些研究:
添加到
sp_send_dbmail
:<前><代码>@ANSI_Attachment 位 = 0
执行为“dbo”
替换
<前><代码>IF(@AttachmentsExist = 1)
开始
…………
结尾
与:
<前><代码>IF(@AttachmentsExist = 1)
开始
如果(@ANSI_Attachment = 1)
开始
--将临时附件复制到sysmail_attachments
插入sysmail_attachments(mailitem_id,文件名,文件大小,附件)
选择@mailitem_id,文件名,文件大小,
转换(varbinary(最大),
substring( -- 从 unicode 中删除 BOM 标记
转换(varchar(最大),CONVERT(nvarchar(最大),附件)),
2、数据长度(附件)/2
)
)
来自 sysmail_attachments_transfer
WHERE uid = @temp_table_uid
结束否则开始
--将临时附件复制到sysmail_attachments
插入sysmail_attachments(mailitem_id,文件名,文件大小,附件)
选择@mailitem_id,文件名,文件大小,附件
来自 sysmail_attachments_transfer
WHERE uid = @temp_table_uid
结尾
结尾
替换
After some research on SQL Server 2008 R2:
Add to
sp_send_dbmail
:Replace
with: