Delimiter //
/**
 * Account type is provided : we search the given category for 
 * the current entity. If it not exists, we throw an error. Else, if 
 * it exists, we assign it to the account
 * 
 * @param int batchnum
 * @author Benoit
 * @ticket #4142
 */ 
Drop Procedure If Exists LOADER_ACCOUNTS_NORMALIZE_MAP_CATEG_PROVIDED//
Create Procedure LOADER_ACCOUNTS_NORMALIZE_MAP_CATEG_PROVIDED(
    In batchnum Int
)
Begin

    Set @id_entity = GET_LOADER_INFO('ID_ENTITIES', batchnum);

    Update
        LOADER_STATUS LS
        Inner Join LOADER_ACCOUNTS LA
            On LA.LOADER_STATUS_ID_LOADER_STATUS = LS.ID_LOADER_STATUS
        Left Join DF_ACC_CATEG AC
            On AC.DF_ACC_CATEG_CODE = LA.ACCOUNTS_CATEG
            And AC.ENTITIES_ID_ENTITIES = @id_entity
    Set
        LA.DF_ACC_CATEG_ID_DF_ACC_CATEG = AC.ID_DF_ACCOUNTS_CATEGORIES
        , LS.LOADER_STATUS_ERRCOUNT = 
            (Case When AC.ID_DF_ACCOUNTS_CATEGORIES Is Null
                Then LS.LOADER_STATUS_ERRCOUNT + 1
                Else LS.LOADER_STATUS_ERRCOUNT
            End)
        , LS.LOADER_STATUS_ERRMSGS = 
            (Case When AC.ID_DF_ACCOUNTS_CATEGORIES Is Null
                Then LOADER_CONCAT_ERR(LS.LOADER_STATUS_ERRMSGS, Concat('Catégorie de compte inexistante : "', LA.ACCOUNTS_CATEG, '"'))
                Else LS.LOADER_STATUS_ERRMSGS
            End)
    Where
        LS.LOADER_STATUS_BATCH_NUM = batchnum 
        And Coalesce(LA.ACCOUNTS_CATEG, '') != '';
End//
Delimiter ;