2016-04-08 50 views
0

我想根據「if body body true send/false do not send」發送電子郵件 它不一定是它可以是如果有什麼返回發送如果沒有返回不發送。 我的代碼是基於一個職位:https://dba.stackexchange.com/questions/83776/need-to-send-a-formatted-html-email-via-database-mail-in-sql-server-2008-r2根據「if body body true send/false do not send」發送數據庫電子郵件

如果您發現此代碼有點混亂,它的問題所有者接受爲上述鏈接的最佳答案。 使用MS SQL Server 2014

SET @Body = (SELECT td = CONVERT(VARCHAR(10), P.Date, 120), '', 
        td = P.ID, '', 
        td = P.ID2, '', 
        td = G.ID, '', 
        td = D.ID,'', 
        td = D.Name,'', 
        td = CASE WHEN G.SubmittedDate IS NULL THEN 'New' 
           ELSE 'Dealer Submitted' 
         END, '' 
      FROM  I_CancelledGRN I 
        INNER JOIN TxnGRN G ON G.ID = I.ID 
        INNER JOIN Distributor D ON D.UID = G.DistributorUID 
        INNER JOIN POTxn P ON P.SiteUID = G.POTxn_SiteUID 
              AND P.UID = G.POTxnUID 
      WHERE IsCancelled IS NULL 
     FOR XML RAW('tr'), 
       ELEMENTS 
     ) 




SELECT @Body = @TableHead + ISNULL(@Body, '') + @TableTail 

EXEC sp_send_dbmail 
     @profile_name='DatabaseEmailProfile', 
     @copy_recipients ='[email protected]', 
     @recipients='[email protected]' 
     @subject='Query Result', 
     @[email protected] , 
     @body_format = 'HTML' ; 

回答

0

會是這樣的工作嗎?

SET @Body = (SELECT td = CONVERT(VARCHAR(10), P.Date, 120), '', 
        td = P.ID, '', 
        td = P.ID2, '', 
        td = G.ID, '', 
        td = D.ID,'', 
        td = D.Name,'', 
        td = CASE WHEN G.SubmittedDate IS NULL THEN 'New' 
           ELSE 'Dealer Submitted' 
         END, '' 
      FROM  I_CancelledGRN I 
        INNER JOIN TxnGRN G ON G.ID = I.ID 
        INNER JOIN Distributor D ON D.UID = G.DistributorUID 
        INNER JOIN POTxn P ON P.SiteUID = G.POTxn_SiteUID 
              AND P.UID = G.POTxnUID 
      WHERE IsCancelled IS NULL 
     FOR XML RAW('tr'), 
       ELEMENTS 
     ) 


IF LEN(@Body) > 0 
BEGIN 

SELECT @Body = @TableHead + ISNULL(@Body, '') + @TableTail 

EXEC sp_send_dbmail 
     @profile_name='DatabaseEmailProfile', 
     @copy_recipients ='[email protected]', 
     @recipients='[email protected]' 
     @subject='Query Result', 
     @[email protected] , 
     @body_format = 'HTML' ; 

END 

它只是檢查天氣@Body有任何長度,只發送郵件,如果它。您可以使用任何其他評估而不是LEN,具體取決於您的場景

+0

不管您是否認爲這正是我最終想出的結果,而不僅僅是說明它更難以正確獲取html格式:-(但現在可以,非常感謝您的答覆:-) – SSMSJ

相關問題