Necessary Modification: There are some tables you do not want to monitor for various reasons. To remove these tables from the results set, get their table ID from vol.Tables and insert them into the UnwantedTables temporary table.
--Remove Rarely loaded tables from load analysisINSERT INTO #UnWantedTables(TableID)SELECT 131UNION ALLSELECT 135UNION ALLSELECT 118UNION ALLSELECT 123
USE [ODS]GODROP PROCEDURE IF EXISTS [dbo].[usp_LoadTableLoadReportingTable]GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE PROCEDURE [dbo].[usp_LoadTableLoadReportingTable] ASBEGINCREATE TABLE #UnWantedTables(TableID INT)CREATE CLUSTERED INDEX CIDX_UNWANTEDTABLES_TABLEID ON #UnWantedTables(TableID)DECLARE @MostRecentDate DATETIMEDECLARE @MostRecentDateInReportingTable DATESELECT @MostRecentDate =MAX(DateOfCurrentObservation) FROM [vol].[LoadObservations]SELECT @MostRecentDateInReportingTable =MAX(DateOfCurrentObservation) FROM rpt.TableLoadReportINSERT INTO #UnWantedTables(TableID)--Get rid of tables with no recordsSELECT t.TableIDFROM [vol].[Tables] tJOIN [vol].[LoadObservations] loON t.TableID = lo.TableIDGROUP BY t.TableIDHAVING AVG(CAST(lo.[RowCount] AS BIGINT)) =0UNION--Get rid of tables with no load varianceSELECT t.TableIDFROM [vol].[Tables] tJOIN [vol].[LoadObservations] loON t.TableID = lo.TableIDGROUP BY t.TableIDHAVING AVG(CAST(lo.CurrentThreeSDLevel AS BIGINT)) =0--Remove Rarely loaded tables from load analysis--INSERT INTO #UnWantedTables(TableID)--SELECT 131--UNION ALL--SELECT 135--UNION ALL--SELECT 118--UNION ALL--SELECT 123IF ISNULL(@MostRecentDateInReportingTable,'19000101') <>CAST(CURRENT_TIMESTAMP AS DATE)BEGININSERT INTO rpt.TableLoadReport(TableName, DateOfCurrentObservation, DateOfLastObservation, [RowCount], ChangeFromLastObservation, CurrentThreeSDLevel,AsOf)SELECT t.TableName, lo.DateOfCurrentObservation, lo.DateOfLastObservation, lo.[RowCount], lo.ChangeFromLastObservation, lo.CurrentThreeSDLevel,CAST(CURRENT_TIMESTAMP AS DATE)FROM vol.Tables tJOIN vol.LoadObservations loON t.TableID = lo.TableIDWHERE lo.CurrentThreeSDLevel IS NOT NULLAND t.TableID NOT IN (SELECT TableId FROM #UnWantedTables)ORDER BY t.TableName, lo.DateOfCurrentObservation ASCENDDROP TABLE #UnWantedTablesENDGO