Sage Line 500/1000 Period End Email Check

As part of Sage Line 500/1000 Period End we found it easier and more audit friendly to have an automated email fire via SQL.
My colleague Jim wrote the below (Hey Bob!), I then changed it around to match the sections in which we ran period end.
This made it easier to determine if period ends had completed (You know what happens when during Period End a user suddenly calls starts screaming and disrupts your flow) and could be shown to auditors to match up with when Period End was run.
In the below example we have 3 Sage companies, one which doesn’t used the Fixed Assets Module.

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:          Jim Everson
-- Create date: ??
-- Description:     Period End Checklist
-- =============================================
-- Exec dbo.period_end_check
-- =============================================
-- Change Log
-- =============================================
-- 1.0.1 - CJS - 29/10/2021 - Amended formatting to try and make the report easier to read
-- =============================================
CREATE PROCEDURE [dbo].[period_end_check]
AS
BEGIN
       -- SET NOCOUNT ON added to prevent extra result sets from
       -- interfering with SELECT statements.
       SET NOCOUNT ON;
    -- Insert statements for procedure here
SELECT '<Sage Company 1>' AS company, system_key, key_value, description
INTO #bob
FROM <Sage Company 1>.scheme.sysdirm
WHERE system_key IN ('POPERIOD', 'STPERIOD', 'CBPERIOD', 'PLPERIOD', 'SLPERIOD', 'NLPERIOD')
INSERT INTO #bob
       SELECT '<Sage Company 1>' as company, 'FAPERIOD' AS system_key, 'N/A' AS key_value, 'Current Fixed Asset Period number/Year' AS description
INSERT INTO #bob
       SELECT '<Sage Company 2>' AS company, system_key, key_value, description
       FROM <Sage Company 2>.scheme.sysdirm
       WHERE system_key IN ('POPERIOD', 'STPERIOD', 'CBPERIOD', 'PLPERIOD', 'SLPERIOD', 'FAPERIOD', 'NLPERIOD')
INSERT INTO #bob
       SELECT '<Sage Company 3>' AS company, system_key, key_value, description
       FROM <Sage Company 3>.scheme.sysdirm
       WHERE system_key IN ('POPERIOD', 'STPERIOD', 'CBPERIOD', 'PLPERIOD', 'SLPERIOD', 'FAPERIOD', 'NLPERIOD')

DECLARE @tableHTML  NVARCHAR(MAX) ;
SET @tableHTML =
    N'<H1>Period End Report</H1>' +
    N'<B>Friday Period End</B><br/>(At end of period end process <b>ALL</b> should be in the new period)' +
    N'<table border="1">' +
    N'<tr><th>Company</th><th>System Key</th>' +
    N'<th>Key Value</th><th>Description</th></tr>' +
    CAST ( ( SELECT td = company, '',
                    td = system_key, '',
                    td = key_value, '',
                    td = description, ''
              FROM #bob WHERE system_key IN ('POPERIOD', 'STPERIOD', 'PLPERIOD', 'SLPERIOD')
              ORDER BY system_key,company ASC FOR XML PATH('tr'), TYPE
    ) AS NVARCHAR(MAX) ) +
    N'</table>'+
    N'<B>Fixed Assets Period End</B><br/>(Closure Day Varies)'+
    N'<table border="1">' +
       N'<tr><th>Company</th><th>System Key</th>' +
    N'<th>Key Value</th><th>Description</th></tr>' +
    CAST ( ( SELECT td = company, '',
                    td = system_key, '',
                    td = key_value, '',
                    td = description, ''
              FROM #bob WHERE system_key IN ('FAPERIOD')
              ORDER BY company, system_key ASC FOR XML PATH('tr'), TYPE
    ) AS NVARCHAR(MAX) ) +
    N'</table>'+
    N'<B>Cash Management Period End</B><br/>(Monday After Friday Closure)' +
    N'<table border="1">' +
       N'<tr><th>Company</th><th>System Key</th>' +
    N'<th>Key Value</th><th>Description</th></tr>' +
    CAST ( ( SELECT td = company, '',
                    td = system_key, '',
                    td = key_value, '',
                    td = description, ''
              FROM #bob WHERE system_key IN ('CBPERIOD')
              ORDER BY company, system_key ASC FOR XML PATH('tr'), TYPE
    ) AS NVARCHAR(MAX) ) +
    N'</table>'+
    N'<B>Nominal Ledger Period End</B>' +
    N'<table border="1">' +
       N'<tr><th>Company</th><th>System Key</th>' +
    N'<th>Key Value</th><th>Description</th></tr>' +
    CAST ( ( SELECT td = company, '',
                    td = system_key, '',
                    td = key_value, '',
                    td = description, ''
              FROM #bob WHERE system_key IN ('NLPERIOD')
              ORDER BY company, system_key ASC FOR XML PATH('tr'), TYPE
    ) AS NVARCHAR(MAX) ) +
    N'</table>'
       ;
EXEC msdb.dbo.sp_send_dbmail @recipients='<email address>',
    @subject = 'Period End Status Check',
    @body = @tableHTML,
    @body_format = 'HTML' ;
END
GO