Delimiter //
/**
 * Make insertions in the CONTACTS_PHONE temoprary table. 
 * Used LOADER_CONTACTS_NORMALIZE_MAP_PHONE_PROVIDED();
 *
 * @author Jean-François
 */ 
Drop Procedure If Exists LOADER_CONTACTS_NORMALIZE_MAP_PHONE_PROVIDED_DOINSERT//

Create Procedure LOADER_CONTACTS_NORMALIZE_MAP_PHONE_PROVIDED_DOINSERT (
      In batch_num Int
    , In my_table Varchar(80)
    , In my_col Varchar(80)
    , In my_type Int
    , In my_state Int Unsigned
)
Begin

    call LOADER_CONTACTS_NORMALIZE_MAP_PHONE_PROVIDED_CHECKTYPE(batch_num, my_col); 

    Set @query = Concat('Insert Into `', my_table, '` ',
        '(
            LOADER_STATUS_ID_LOADER_STATUS
			, LOADER_CONTACTS_ID_LOADER_CONTACTS
            , DF_PHONE_TYP_ID_DF_PHONE_TYP
            , DF_CHECK_STAT_ID_DF_CHECK_STAT
            , CONTACTS_PHONE_NUMBER
            , CONTACTS_PHONE_NUMERIC_ONLY
            , CONTACTS_PHONE_PREFERENCE
            , CONTACTS_PHONES_TRASHED
            , CONTACTS_PHONES_LAST_UPDATE
        ) ',
        'Select
            C.LOADER_STATUS_ID_LOADER_STATUS
			, C.ID_LOADER_CONTACTS
            , Coalesce(', my_col, '_ID_DF_PHONE_TYP, ?) -- valeur TYPE
            , ? -- valeur STATE
            , C.', my_col ,' as CONTACTS_PHONE_NUMBER
            , CASH_BUSINESS_CONVERT_PHONE_NUMBER_TO_NUMERIC(C.', my_col ,') as CONTACTS_PHONE_NUMERIC_ONLY
            , NULL
            , 0
            , now()
        From
            LOADER_CONTACTS C
            Inner Join LOADER_STATUS S
                On S.ID_LOADER_STATUS = C.LOADER_STATUS_ID_LOADER_STATUS
        Where
            S.LOADER_STATUS_BATCH_NUM = ? -- valeur batch_num
            AND COALESCE(C.', my_col ,', '''') != '''';');

-- , Coalesce(', my_col, '_ID_DF_PHONE_TYP, P.DF_PHONE_TYP_ID_DF_PHONE_TYP, ?) -- valeur TYPE
-- , P.CONTACTS_PHONES_LAST_UPDATE, P.CONTACTS_PHONES_LAST_UPDATE
--             Left Join CONTACTS_PHONES P
--                 On P.CONTACTS_ID_CONTACTS = C.ID_CONTACTS
--                 AND P.CONTACTS_PHONE_NUMBER = C.', my_col ,'


        -- AND  P.ID_CONTACTS_PHONES IS NULL
        -- GET_LOADER_INFO('', batchnum);


    Set @lcfdi_my_type = my_type;
    Set @lcfdi_my_state = my_state;
    Set @lcfdi_my_batchnum = batch_num;
    Prepare stmt From @query;
    execute stmt using @lcfdi_my_type, @lcfdi_my_state, @lcfdi_my_batchnum;	
    Deallocate prepare stmt;

End //
Delimiter ;