Hi @kkran,
You are sending an email in the (X)HTML format. That's why it would be better if you will switch @BODY variable from NVARCHAR(MAX) to XML data type.
It will allow you to use XQuery to interrogate XML and check for any discrepancies across environments.
Please edit your question, and add @BODY variable as XML, i.e. its value as XML. And I will show you how to do it.
XQuery provides the following benefits:
- Allows to compose a proper well-formed (X)HTML "visually", instead of error prone string concatenation.
- Allows to calculate any discrepancy for three columns in question: Oracle, STAGE, and SDS.
Because a minimal reproducible example is not provided, I had to simulate your data setup.
DECLARE @LIST TABLE (
Description varchar (20)not null,
Oracle_Count varchar (max) not null,
STAGE_Count varchar(50) not null,
SDS_Count varchar (50) not null,
MOST_RECENT_LAST_UPDATED_DATE DATETIME NOT NULL
);
INSERT @LIST
(
Description,
Oracle_Count,
STAGE_Count,
SDS_Count,
MOST_RECENT_LAST_UPDATED_DATE
)
VALUES
('S Count', '254815', '254815', '254815', '2023-09-01T13:16:37'),
('L Count', '5305', '5305', '5305', '2023-08-18T07:40:59'),
('FC Count', '975', '975', '978', '2023-09-15T11:00:00'),
('Pro COUNT', '127933', '127937', '127937', '2023-08-31T12:05:04');
SELECT * FROM @LIST;
DECLARE @BODY NVARCHAR(MAX)
, @xhtmlBody XML
, @SUBJECT VARCHAR(MAX) = 'Counts - ' + CONVERT(VARCHAR(20),GETDATE(),120)
, @RECIPIENTS VARCHAR(MAX) = 'Testemail@email.com'
, @ReportRun VARCHAR(20) = CONVERT(VARCHAR(20),GETDATE(),120)
, @CNT INT
, @UNIQUE_COUNTER INT;
SET @CNT = ISNULL((SELECT COUNT(*) FROM @LIST),0);
SET @xhtmlBody = (
SELECT (
SELECT *
FROM @LIST FOR XML PATH('row'), TYPE, ROOT('root'))
.query('<html>
<head>
<meta charset="utf-8"/>
(: including embedded CSS styling :)
<style>
table <![CDATA[ {border-collapse: collapse; width: 300px;} ]]>
th <![CDATA[ {background-color: #4CAF50; color: white;} ]]>
th, td <![CDATA[ { text-align: left; padding: 8px;} ]]>
tr:nth-child(even) <![CDATA[ {background-color: #f2f2f2;} ]]>
#red <![CDATA[ {background-color: LightPink;} ]]>
</style>
</head>
<title>SDS: PROCESS Notification Error </title>
<body>
<b>Report Run: {sql:variable("@ReportRun")}</b>
<p/>
<table border="1">
<thead>
<tr>
<th>Description</th>
<th>Oracle_Count</th>
<th>STAGE_Count</th>
<th>SDS_Count</th>
<th>MOST_RECENT_LAST_UPDATED_DATE</th>
</tr>
</thead>
<tbody>
{
for $row in /root/row
return <tr>
{if ((count(distinct-values($row/*[position()=(2,3,4)])))[1] gt 1) then attribute id {"red"} else ()}
<td>{data($row/Description)}</td>
<td>{data($row/Oracle_Count)}</td>
<td>{data($row/STAGE_Count)}</td>
<td>{data($row/SDS_Count)}</td>
<td>{data($row/MOST_RECENT_LAST_UPDATED_DATE)}</td>
</tr>
}
</tbody>
</table></body></html>'));
-- just to see
SELECT @xhtmlBody;
SET @UNIQUE_COUNTER = @xhtmlBody.query('
for $row in /html/body/table/tbody/tr
return <r>{count(distinct-values($row/td[position()=(2,3,4)]))}</r>
').value('sum(data(/r))', 'INT');
-- just to see
SELECT @UNIQUE_COUNTER;
SET @BODY = TRY_CAST(@xhtmlBody AS NVARCHAR(MAX));
-- discepancy found
IF @UNIQUE_COUNTER <> @CNT BEGIN
EXEC MSDB..SP_SEND_DBMAIL
@PROFILE_NAME = 'SERVER',
@RECIPIENTS = @RECIPIENTS,
@SUBJECT = @SUBJECT,
@BODY_FORMAT = 'HTML',
@BODY = @BODY;
END;