2017-05-26 97 views
0

我有一个TSQL脚本,用于选择数据并生成电子邮件。 我的脚本非常复杂,但我已将其简化为一个简单示例来演示我的问题。SQL电子邮件格式消失CR LF

简而言之,当我的某个字段超过特定长度(21个字符)时,我插入到VAR_Body变量中的CR和LF就会丢失。来自该字段的文本不会被截断。

预期结果

Renewal Date= 23 Jun 2017 
Existing Insurer= 123456789
Existing Insurer ID= 6007 

实际结果

Renewal Date= 23 Jun 2017 
Existing Insurer= 123456789Existing Insurer ID= 6007 

如果我把现有的保险字符串它工作的一个字符。

这是我的减少代码来演示问题。

有没有人有任何想法这里发生了什么?

我已经耗尽的东西尝试。

感谢

大卫

-- Email Variables 
Declare @VAR_Dest_EmailAddress varchar(150) 
set @VAR_Dest_EmailAddress = '[email protected]' 
Declare @VAR_Subject varchar(100) 
Declare @VAR_Body varchar(1000) 

-- Format Variables 
Declare @DateDisplayFormat int 
Set @DateDisplayFormat = 106 -- DD MMM YYYY eg 25 MAY 2017 
Declare @MoneyDisplayFormat int 
set @MoneyDisplayFormat = 1 -- Commas every three digits and two digits to the right of the decimal point. 


-- Data variables 
Declare @sPlanNumber varchar(10) 
Declare @dRenewal datetime 
Declare @sExistingInsurer varchar(50) 
Declare @lExistingInsurerID int 

-- Set the Variables 
Set @sPlanNumber = '12345' 
Set @dRenewal = '2017-06-23 00:00:00.000' 
set @sExistingInsurer = '123456789' 
set @lExistingInsurerID = '6007' 

-- Set the body of the email 
    set @VAR_Body = 'Renewal Date= ' + Convert(varchar(11),@dRenewal,@DateDisplayFormat) + CHAR(13)+ CHAR(10) 
    set @VAR_Body = @VAR_Body + 'Existing Insurer= ' + @sExistingInsurer + CHAR(13) + CHAR(10) 
    set @VAR_Body = @VAR_Body + 'Existing Insurer ID= ' + Convert(varchar(10),@lExistingInsurerID) + CHAR(13) + CHAR(10) 

-- Send the email 
EXEC msdb.dbo.sp_send_dbmail 
    @recipients = @VAR_Dest_EmailAddress, 
    @subject = @sPlanNumber, 
    @body = @VAR_BODY, 
    @profile_name = 'SQL Email Profile' ; 
+0

您是否尝试过使用'\ n \ r'作为换行符的,而不是'CHAR(13)+ CHAR(10)' – JanR

+0

好主意,除非我遗漏了一些东西,只是将\ n \ r插入到文本字符串中。 set @VAR_Body ='更新日期='+转换(varchar(11),@ dRenewal,@ DateDisplayFormat)+'\ r \ n' –

+0

您是否尝试过将其作为nvarchar? – JanR

回答

0

如果你周围有电子邮件格式不要求(TEXT VS HTML),你可以使用HTML格式的电子邮件。然后,您可以使用<br/>和其他html标签来格式化电子邮件。

EXEC msdb.dbo.sp_send_dbmail 
    @recipients = @VAR_Dest_EmailAddress, 
    @subject = @sPlanNumber, 
    @body = @VAR_BODY, 
    @body_format = 'HTML' --declare the body formatting of the email to be HTML 
    @profile_name = 'SQL Email Profile' ; 

然后,您可以格式化你的电子邮件:

set @VAR_Body = 'Renewal Date= ' + Convert(varchar(11),@dRenewal,@DateDisplayFormat) + '<br/>' 
--we can now use the html break character which will be rendered by all email clients 
set @VAR_Body = @VAR_Body + 'Existing Insurer= ' + @sExistingInsurer + '<br/>' 
set @VAR_Body = @VAR_Body + 'Existing Insurer ID= ' + Convert(varchar(10),@lExistingInsurerID) + '<br/>' 

参考可以在MSDN文档中找到here

+0

工作!我的目标是将这些数据作为数据源发送给其他应用程序。我认为该应用程序应该能够处理HTML格式的邮件。我只是检查了他们的帮助,并没有提及任何关于电子邮件格式的内容:) 所以我们可以称之为“解决问题”,但仍然是一个非常奇怪的问题。我曾考虑过这种方法,但我认为我需要添加更多的格式才能完成HTML。谢谢大卫 –