How to organize the periodic sending of an error report to the mail

General scheme of work: 

  1. Running a periodic request via the sync mechanism
  2. The procedure generates the markup
  3. If the markup is not empty, then we send it to the mail via an external action inside sync.

Markup generation procedure: 

CREATE OR ALTER PROCEDURE [dbo].[as_exception_diag]
	@limit int =20,
	@s nvarchar(max) OUTPUT
AS
BEGIN

declare @t1 table (title nvarchar(max), db nvarchar(256), [desc] nvarchar(max),
	digit nvarchar(512), important int)
declare @s1 nvarchar(max)=''

declare @week int =  isnull((select count(*) from as_trace t3 where t3.code='exception' and cast(created as date) > cast(dateadd(day, -7, getdate()) as date) ), 0)
declare @yesterday int = isnull((select count(*) from as_trace t3 where t3.code='exception' and cast(created as date) = cast(dateadd(day, -1, getdate()) as date) ), 0)
declare @today int = isnull((select count(*) from as_trace t2 where t2.code='exception' and cast(created as date) = cast(getdate() as date) ), 0)
declare @lastHour int = isnull((select count(*) from as_trace t2 where t2.code='exception' and created >  dateadd(hour, -1, getdate()) ), 0)

set @s1 = @s1 + 'week - '  + cast(@week as nvarchar) + '
'
set @s1 = @s1 + 'yesterday - '  + cast(@yesterday as nvarchar) + '
'
set @s1 = @s1 + 'today - '  + cast(@today as nvarchar) + '
'
set @s1 = @s1 + 'lastHour - '  + cast(@lastHour as nvarchar) + '
'

if(@yesterday>@limit) begin

	set @s = @s+ @s1

	declare @s2 nvarchar(max)
	select @s2= STUFF((
		select '

' + username + ' ' + convert(nvarchar(128), created, 104) + ' - ' + header+ '

' +
			'
' + replace(text, '
', '
') + '
' from (
			select top 30 username, created, text, header from as_trace where code='exception' order by id desc
	) t1
	--order by db, title desc
	FOR XML PATH, TYPE).value('.[1]','nvarchar(max)' ), 1, 1, '')
	set @s = @s+ @s2
end

END;

' + username + ' ' + convert(nvarchar(128), created, 104) + ' - ' + header+ '

' +
			'

' + text + '

' from (
			select top 30 username, created, text, header from as_trace where code='exception' order by id desc
	) t1
	--order by db, title desc
	FOR XML PATH, TYPE).value('.[1]','nvarchar(max)' ), 1, 1, '')
	set @s = @s+ @s2
end

END;

@limit sets the minimum number of exceptions for yesterday, at which an email should be sent to the mail.

sync_day procedure with sending error messages: 

CREATE PROCEDURE [dbo].[falcon_nt_sync_day]
AS
BEGIN
    declare @s nvarchar(max) = ''
	exec [as_exception_diag] @limit = 20, @s=@s OUTPUT
	if(len(@s)>0) begin
        	select 'email' type, 'mail@site.ru' [to], 'Ошибки на site.ru' subject, @s body
    end
END

Falcon Space is a functional web development platform on a narrow stack MS SQL/Bootstrap. Falcon Space Gettting started
{sp-shortDemostandLinks}

Falcon Space Platform

This is a reduction in the cost of ownership

at the expense of fewer people to support

This is a quick change

while using the program

This is a modern interface

full adaptation for mobile devices

Note