declare @SRT as numeric(15,2); declare @SOPS as numeric(15,2); declare @SNPA as numeric(15,2); select @SRT = SUM(CAmt) from livedb.dbo.tbl_DAILY_ATB_CMBAL Select @SOPS = [Tot] FROM [livedb].[dbo].[vw_DAILY_ATB_OPS_BALANCE] Select @SNPA = [NPABal] FROM [livedb].[dbo].[vw_DALY_ATB_NPA_BALANCE] Select @SRT as RTBal, @SOPS-@SNPA as OPSBAL IF @SRT <> @SOPS-@SNPA Begin exec msdb.dbo.sp_send_dbmail @profile_name='GlenMail', @recipients ='firstname.lastname@example.org', @subject ='Daily ATB SQL table Update Status', @body = 'The processing of the DAILY_ATB_BALANCE table was unsuccessful. The calculated End of Day balance does not equal the Operations Summary less the NPA Accounts', @query = 'SELECT @SRT as CalcBalance, @SOPS as OperSumBal, @SNPA as NPABal, @SRT-(@SOPS-@SNPA) as Variance' End
From novice to tech pro — start learning today.