Delimiter //
/**
 * Consolides logs
 * 
 * @author Jean-François
 */
Drop Procedure If Exists LOADER_LOGS_CONSOLIDATION//
Create Procedure LOADER_LOGS_CONSOLIDATION()
Begin

    Insert Into 
        LOADER_LOGS (
            LOADER_STATUS_ID_LOADER_STATUS
            , LOADER_LOGS_SRC_TABLE_ID
            , LOADER_LOGS_BATCH_NUMM
            , LOADER_LOGS_ERRCOUNT
            , LOADER_LOGS_ERRMSGS
            , LOADER_LOGS_WARNCOUNT
            , LOADER_LOGS_WARNMSGS
            , LOADER_LOGS_TYPE
        )
        Select
            S.ID_LOADER_STATUS
            , S.SRC_TABLE_ID
            , S.LOADER_STATUS_BATCH_NUM
            , S.LOADER_STATUS_ERRCOUNT
            , S.LOADER_STATUS_ERRMSGS
            , S.LOADER_STATUS_WARNCOUNT
            , S.LOADER_STATUS_WARNMSGS
            , I.LOADER_INFO_VALUE
        From
            LOADER_STATUS S 
        Left Join
            LOADER_INFO I
            On I.LOADER_INFO_BATCH_NUM = S.LOADER_STATUS_BATCH_NUM
            And I.LOADER_INFO_NAME = 'LOADER_TYPE'
        Where
            Coalesce(S.LOADER_STATUS_ERRCOUNT, 0) > 0
            Or Coalesce(S.LOADER_STATUS_WARNCOUNT, 0) > 0
    Union All
        Select
            Null
            , Null
            , Null
            , SBC.LOADER_BALANCECONTROL_ACCOUNTS_STATUS_ERRCOUNT
            , SBC.LOADER_BALANCECONTROL_ACCOUNTS_STATUS_ERRMSGS
            , Null
            , Null
            , 'BALANCECONTROL'
        From
            LOADER_BALANCECONTROL_ACCOUNTS_STATUS SBC
        Where
            Coalesce(SBC.LOADER_BALANCECONTROL_ACCOUNTS_STATUS_ERRCOUNT, 0) > 0
    ;

End//
Delimiter ;