Delimiter //
/**
* In batchnum Int : num du lot
* @deprecated 
*/

Drop Procedure if exists LOADER_ATTACHEDMEMBERS_COPY_UPD_ALL_ATTCHDMBR_REF//

Create Procedure LOADER_ATTACHEDMEMBERS_COPY_UPD_ALL_ATTCHDMBR_REF (
    In batchnum int
)
Begin
    lamcuaamr_main: Begin
        If batchnum = 0 Then
            Leave lamcuaamr_main;
        End if;
        Set @lamcuaamr_batchnum = batchnum;
        -- debug
--         Set @lamcuaamr_batchnum = 1;
        Set @lamcuaamr_identities = GET_LOADER_INFO('ID_ENTITIES', @lamcuaamr_batchnum);

        lamcuaamr_updtall: Begin
            Declare lamcuaamr_ref varchar(40);
            Declare lamcuaamr_cursor Cursor For
                Select
                    Distinct LATTMBR.ACCOUNTS_has_MEMBERS_REF
                From LOADER_STATUS lst
                    Inner Join
                        LOADER_ATTACHEDMEMBERS LATTMBR
                        On LATTMBR.LOADER_STATUS_ID_LOADER_STATUS = lst.ID_LOADER_STATUS
                Where
                    lst.LOADER_STATUS_BATCH_NUM = @lamcuaamr_batchnum
                    And lst.LOADER_STATUS_ERRCOUNT = 0
                    And LOADER_ATTACHEDMEMBERS_AHMREF_AUTO = 0 -- implicite
                    And LATTMBR.ACCOUNTS_has_MEMBERS_REF like 'LOADER.%'
                ;

            Open lamcuaamr_cursor;
            lamcuaamr_blk: Begin
                Declare Exit Handler for NOT FOUND 
                Begin
                    Close lamcuaamr_cursor;
                End ;

                lamcuaamr_loop: loop
                    fetch lamcuaamr_cursor Into lamcuaamr_ref;
                    Set @lamcuaamr_ref = lamcuaamr_ref;
                    call BUSINESS_ATTACHED_MEMBERS_UPDATE(@lamcuaamr_ref, @lamcuaamr_identities); 

                end loop lamcuaamr_loop;

            End ;

        End;

--         call BUSINESS_ATTACHED_MEMBERS_UPDATE(@lamcuaamr_ref, @lamcuaamr_identities); 



    End;

End //
Delimiter ;