
[dbo].[UpdateErrorCodeStatisticsInBulk]
CREATE PROCEDURE [dbo].[UpdateErrorCodeStatisticsInBulk]
@inErrorCodeList IntList readonly
as
set nocount on;
if( exists( select top 1 1 from @inErrorCodeList ) )
begin
declare @nowOffset datetimeoffset = sysdatetimeoffset()
declare @theSummarizedErrorData table
(
ErrorCodeFID int not null,
ErrorCount int not null
)
insert into @theSummarizedErrorData
(
ErrorCodeFID,
ErrorCount
)
select
ErrorCodeFID = ErrorCodeList.Item,
ErrorCount = count(*)
from @inErrorCodeList as ErrorCodeList
inner join ErrorCode on ErrorCode.ECPriKey = ErrorCodeList.Item
group by ErrorCodeList.Item
insert into ErrorCodeStatistics
(
ErrorCodeFID,
LoggedCount,
FirstError,
LastError
)
select
ErrorCodeFID = SummarizedErrorData.ErrorCodeFID,
LoggedCount = 0,
FirstError = @nowOffset,
LastError = @nowOffset
from @theSummarizedErrorData as SummarizedErrorData
except
select
ErrorCodeFID = ErrorCodeStatistics.ErrorCodeFID,
LoggedCount = 0,
FirstError = @nowOffset,
LastError = @nowOffset
from ErrorCodeStatistics
update ErrorCodeStatistics set
LoggedCount = LoggedCount + SummarizedErrorData.ErrorCount,
LastError = @nowOffset
from @theSummarizedErrorData as SummarizedErrorData
inner join ErrorCodeStatistics on ErrorCodeStatistics.ErrorCodeFID = SummarizedErrorData.ErrorCodeFID
end
GO
GRANT EXECUTE ON [dbo].[UpdateErrorCodeStatisticsInBulk] TO [MssExec]
GO