我有一個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