2017-05-26 94 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。謝謝大衛 –