我有一个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' ;
您是否尝试过使用'\ n \ r'作为换行符的,而不是'CHAR(13)+ CHAR(10)' – JanR
好主意,除非我遗漏了一些东西,只是将\ n \ r插入到文本字符串中。 set @VAR_Body ='更新日期='+转换(varchar(11),@ dRenewal,@ DateDisplayFormat)+'\ r \ n' –
您是否尝试过将其作为nvarchar? – JanR