Delimiter //
/**
 * Currency is not provided -> use the currency of the entity
 * 
 * @author Jean-François
 */
Drop Procedure If Exists LOADER_ENTRIES_MAP_CURRENCY_NOTPROVIDED//
Create Procedure LOADER_ENTRIES_MAP_CURRENCY_NOTPROVIDED(
    In batchnum Int
)
Begin
    
    Set @id_entity = GET_LOADER_INFO('ID_ENTITIES', batchnum);

    Update
        LOADER_ENTRIES E
        Inner Join LOADER_STATUS S
            On S.ID_LOADER_STATUS = E.LOADER_STATUS_ID_LOADER_STATUS
        Inner Join ENTITIES ENT
            On ENT.ID_ENTITIES = @id_entity
    Set
        E.DF_CURRENCIES_ID_DF_CURRENCIES = ENT.DF_CURRENCIES_ID_DF_CURR
    Where
        S.LOADER_STATUS_BATCH_NUM = batchnum
        And E.ID_ENTRIES_NEW Is Not Null
        And Coalesce(E.CURRENCY, '') = ''
    ;

End//
Delimiter ;