Delimiter //
/**
 * Find the id of the given sequence
 * 
 */
Drop Procedure If Exists LOADER_REFDATALABEL_NORMALIZE_MAP_ID_ENT_REF//
Create Procedure LOADER_REFDATALABEL_NORMALIZE_MAP_ID_ENT_REF(
    In batchnum Int
)
Begin
    

    Set @idEntities = GET_LOADER_INFO('ID_ENTITIES', batchnum);
    Update
        LOADER_STATUS S
        Inner Join
            LOADER_REFDATALABEL ARD
            On ARD.LOADER_STATUS_ID_LOADER_STATUS = S.ID_LOADER_STATUS
		Inner Join ENTITIES_REF_TYPES ERT
			On ARD.LOADER_REFDATA_TYPE = ERT.ENTITIES_REF_TYPES_CODE
        Inner Join ENTITIES_REF ENTR
            On ENTR.ENT_REF_TYPE_ID_ENT_REF_TYPES = ERT.ID_ENTITIES_REF_TYPES
            And ENTR.ENTITIES_ID_ENTITIES = @idEntities
            And ENTR.ENTITIES_REF_SEQUENCE= ARD.ENTITIES_REF_SEQUENCE
    Set
        ARD.ENTITIES_REF_ID_ENTITIES_REF = ENTR.ID_ENTITIES_REF
    Where
        S.LOADER_STATUS_BATCH_NUM = batchnum
    ;

End//
Delimiter ;