我想发送一封基于“if body content true send / false不发送”的电子邮件 它不一定是因为如果返回的内容没有发送,则返回发送的内容。 我的代码基于以下帖子:https://dba.stackexchange.com/questions/83776/need-to-send-a-formatted-html-email-via-database-mail-in-sql-server-2008-r2
如果您发现此代码有点混乱,那么问题所有者在上述链接中接受了最佳答案。 使用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 ='aasc@stackexchange.com',
@recipients='aa.sc@outlook.com'
@subject='Query Result',
@body=@Body ,
@body_format = 'HTML' ;
答案 0 :(得分: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 ='aasc@stackexchange.com',
@recipients='aa.sc@outlook.com'
@subject='Query Result',
@body=@Body ,
@body_format = 'HTML' ;
END
它只检查天气@Body有任何长度,并且只发送邮件。您可以使用任何其他评估而不是LEN,具体取决于您的方案