我在SQL中有一个小例程,我每天都将其用作预定作业,以便将某些事件日志消息通过电子邮件发送给我。
DECLARE @CurrentDate DATETIME,
@Body VARCHAR(MAX),
@TableHead VARCHAR(MAX),
@TableTail VARCHAR(MAX)
SET NOCOUNT ON;
SELECT @CurrentDate=DATEADD(dw, -5, GETDATE()) --Three weekdays back
SET @TableTail='</table></body></html>'
SET @TableHead = '<html><head>' +
'<style>' +
'td {border: solid black 1px;padding-left:5px;padding-right:5px;padding-top:1px;padding-bottom:1px;font-size:9pt;} ' +
'</style>' +
'</head>' +
'<body><table cellpadding=0 cellspacing=0 border=0>' +
'<tr bgcolor=#FFEFD8><td align=center><b>User Name</b></td>' +
'<td align=center><b>Date</b></td>' +
'<td align=center><b>Message</b></td></tr>';
SELECT @Body=(
SELECT ELogUserName AS [TD],
CONVERT(NVARCHAR(20),Elogdatetime,101) AS [TD],
REPLACE(Elogmessage,CHAR(10)+CHAR(13),'<br />') AS [TD]
FROM tblEventLog
WHERE ELogDateTime > @CurrentDate AND ELogEventType = 3 AND ELogUsername <> 'jameccccccfer'
ORDER BY ELogID DESC
FOR XML RAW('tr'), Elements)
SELECT @Body = @TableHead + @Body + @TableTail
-- return output
INSERT INTO tblPendingMessages
SELECT
'xxxxxxx@Gmail.com' AS Recipient,
'CPAS Error Log' AS MessageSubject,
NULL AS MessageCC,
@Body AS MessageBody,
@CurrentDate AS MessageGenerated,
NULL AS MessageSent,
NULL AS MessageUser,
'HTML' AS MessageFormat
此代码生成HTML字符串,如下所示:
<html><head><style>td {border: solid black 1px;padding-left:5px;padding-right:5px;padding-top:1px;padding-bottom:1px;font-size:9pt;} </style></head><body><table cellpadding=0 cellspacing=0 border=0><tr bgcolor=#FFEFD8><td align=center><b>User Name</b></td><td align=center><b>Date</b></td><td align=center><b>Message</b></td></tr><tr><TD>nfox</TD><TD>06/29/2012</TD><TD>Error Nbr 3075 - Syntax error (missing operator) in query expression *ProjectID=*.
 --> CPASProd.Form_frmWorkOrders.cbProjectID_AfterUpdate, #1, 
 </TD></tr><tr><TD>nfox</TD><TD>06/29/2012</TD><TD>Total number of errors: 1
 1. Cannot append. An object with that name already exists in the collection. (#3367)
 </TD></tr><tr><TD>nfox</TD><TD>06/29/2012</TD><TD>Total number of errors: 1
 1. Cannot append. An object with that name already exists in the collection. (#3367)
 </TD></tr><tr><TD>nfox</TD><TD>06/29/2012</TD><TD>Total number of errors: 1
 1. Cannot append. An object with that name already exists in the collection. (#3367)
 </TD></tr></table></body></html>
不幸的是,当由Outlook(或者更糟的是,通过gmail web客户端...)而不是换行符渲染时,我得到了HTML代码。
("<br />")
知道我做错了吗?