Stored Procedures [dbo].[UpdateErrorCodeStatisticsInBulk]
Properties
PropertyValue
ANSI Nulls OnYes
Quoted Identifier OnYes
Parameters
NameData TypeMax Length (Bytes)
@inErrorCodeListIntListmax
Permissions
TypeActionOwning Principal
GrantExecuteMssExec
SQL Script
/**
*    
*    This procedure is called to track the number of times an error is logged
*    during transaction generation from BATProcess.  See the stored proc
*    UpdateErrorCodeStatistics when you record an error via spBATRecordError
*    or via spStorageRecordError.
*    
*    Error Codes:
*    Do we really care if this guy can't log an error?
*    
*    Input Parameters:
*    @param @inECPriKeyList        The list of ErrorCode.ECPriKey that were just
*                                recorded in some error log table, like BATErrorLog.
*/


CREATE PROCEDURE [dbo].[UpdateErrorCodeStatisticsInBulk]
    @inErrorCodeList IntList readonly
as
set nocount on;

-- There is nothing to do if @inErrorCodeList is empty.
if( exists( select top 1 1 from @inErrorCodeList ) )
begin
    declare @nowOffset datetimeoffset = sysdatetimeoffset()

    declare @theSummarizedErrorData table
    (
        ErrorCodeFID int not null,
        ErrorCount int not null
    )

    -- Summarize all of the "valid" error codes in the list.  They all
    -- "should" be valid error codes, one would think.
    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

    -- Add any error codes we have not previously tracked to our
    -- ErrorCodeStatistics table.
    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

    -- Now, update the ErrorCodeStatistics table since all error codes
    -- sent into us are now in the ErrorCodeStatistics table.
    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
Uses