Delimiter //
/**
    In batchnum Int : num du lot, si 0 ou null traitement de l'ensembles des lignes
*/

Drop Procedure if exists LOADER_CONTACTS_COPY//

Create Procedure LOADER_CONTACTS_COPY (
    In batchnum int
)
Begin
    
--     insertQuerys: Begin
--         call CASH_AUTOINCREMENT_VALUE('CONTACTS', @lastContactId);
-- --         call CASH_AUTOINCREMENT_VALUE('CONTACTS_INDIVIDUAL', @lastIndividualContactId);
-- 
--         call LOADER_CONTACTS_INSERT(batchnum, @lastContactId); -- must call beffore all other contact insert procedure
--         call LOADER_CONTACTS_ADDRESSES_INSERT(batchnum, @lastContactId);
--         call LOADER_CONTACTS_INDIVIDUAL_INSERT(batchnum, @lastContactId); -- must be call before LOADER_CONTACTS_BUSINESS_INSERT
--         call LOADER_CONTACTS_BUSINESS_INSERT(batchnum, @lastContactId); --  muste be call after LOADER_CONTACTS_INDIVIDUAL_INSERT
--         call LOADER_CONTACTS_EMAILS_INSERT(batchnum, @lastContactId);
--         call LOADER_CONTACTS_PHONES_INSERT(batchnum, @lastContactId);
-- 
--     end ;

    updateQuerys: Begin
        call LOADER_CONTACTS_UPDATE(batchnum);
        call LOADER_CONTACTS_ADDRESSES_UPDATE(batchnum);
        call LOADER_CONTACTS_INDIVIDUAL_UPDATE(batchnum);
        call LOADER_CONTACTS_BUSINESS_UPDATE(batchnum);
        call LOADER_CONTACTS_EMAILS_UPDATE(batchnum);
        call LOADER_CONTACTS_PHONES_UPDATE(batchnum);

    end ;

    insertQuerys: Begin
        call CASH_AUTOINCREMENT_VALUE('CONTACTS', @lastContactId);
--         call CASH_AUTOINCREMENT_VALUE('CONTACTS_INDIVIDUAL', @lastIndividualContactId);

        call LOADER_CONTACTS_INSERT(batchnum, @lastContactId); -- must call beffore all other contact insert procedure
        call LOADER_CONTACTS_ADDRESSES_INSERT(batchnum, @lastContactId);
        call LOADER_CONTACTS_INDIVIDUAL_INSERT(batchnum, @lastContactId); -- must be call before LOADER_CONTACTS_BUSINESS_INSERT
        call LOADER_CONTACTS_BUSINESS_INSERT(batchnum, @lastContactId); --  muste be call after LOADER_CONTACTS_INDIVIDUAL_INSERT
        call LOADER_CONTACTS_EMAILS_INSERT(batchnum, @lastContactId);
        call LOADER_CONTACTS_PHONES_INSERT(batchnum, @lastContactId);

    end ;


End //

Delimiter ;