Delimiter //
/**
 * Author:  armen
 * Created: 23 juin 2020
 */

Drop Procedure if exists LOADER_MEMBERS_METADA_INSERT//
Create Procedure LOADER_MEMBERS_METADA_INSERT (
    In batchnum int, 
    In lastId Int
)
Begin
    lmmi_main: Begin
        set @lmmi_batchnum = batchnum;
        set @lmmi_lastId = lastId;
        Call LOADER_GET_ALL_INFO_FORMATED('xml', @lmmi_batchnum, @lmmi_infoxml);

        Insert Into MEMBERS_METADATA (
            MEMBERS_ID_MEMBERS, MEMBERS_METADATA_KEY, MEMBERS_METADATA_EXTID
            , MEMBERS_METADATA_DATE, MEMBERS_METADATA_TEXT
        )
        Select
            src.ID_MEMBERS_NEW + @lmmi_lastId, 'LOADERINFO_XML', null
            , curdate(), @lmmi_infoxml
        From
            LOADER_MEMBERS src  
            Inner Join LOADER_STATUS lst  
                On src.LOADER_STATUS_ID_LOADER_STATUS = lst.ID_LOADER_STATUS  
        Where
            lst.LOADER_STATUS_ERRCOUNT = 0  
            And lst.LOADER_STATUS_BATCH_NUM =  batchnum
            And Coalesce(src.ID_MEMBERS_NEW, 0) <> 0
        ;

    End;

End //

Delimiter ;
