-- A5M2で実行時に[SQL] - [スラッシュ(/)のみの行でSQLを区切る]に変えてから実行する
-- $$から始まる文字は後からREPLACEする文字を示す独自ルール
-- 当プロシージャは、同一セッション内での並列処理を実行することができない
-- 実行者の権限でストアドプロシージャを実行するために、「SQL SECURITY INVOKER」を付与している
CREATE PROCEDURE `internal02`.`crm_distribution_Call2_vod__c`()
SQL SECURITY INVOKER
BEGIN

-- 振り分けスキーマ
DECLARE distribution_schema VARCHAR(20);
-- 振り分けカラム
DECLARE target_column_value VARCHAR(100);
-- 振り分け先テーブルID
DECLARE temp_table_id VARCHAR(18);
-- カーソルフェッチステータス
DECLARE fetch_done BOOLEAN DEFAULT FALSE;

-- カーソル設定
DECLARE table_cursor CURSOR FOR SELECT Id, RecordTypeId FROM internal02.crm_Call2_vod__c;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET fetch_done = TRUE;

-- エラー処理
DECLARE EXIT HANDLER FOR SQLEXCEPTION
    BEGIN
		GET DIAGNOSTICS CONDITION 1
			@error_state = RETURNED_SQLSTATE, @error_msg = MESSAGE_TEXT;
		ROLLBACK;
        SET @error_msg = (
            CASE
            WHEN LENGTH(@error_msg) > 128 THEN CONCAT(SUBSTRING(@error_msg, 1, 125), '...')
            ELSE @error_msg
            END
        );
		SIGNAL SQLSTATE '45000'
		SET MYSQL_ERRNO = @error_state, MESSAGE_TEXT = @error_msg;
	END;

START TRANSACTION;

-- UPSERT STATEMENT設定
SET @upsert_statement_base =
    'INSERT INTO $$distribution_schema$$.crm_Call2_vod__c
		(Id, OwnerId, IsDeleted, Name, RecordTypeId, CreatedDate, CreatedById, LastModifiedDate, LastModifiedById,
        SystemModstamp, LastActivityDate, MayEdit, IsLocked, LastViewedDate, LastReferencedDate, Call_Comments_vod__c,
        Sample_Card_vod__c, Add_Detail_vod__c, Property_vod__c, Account_vod__c, zvod_Product_Discussion_vod__c,
        Status_vod__c, Parent_Address_vod__c, Account_Plan_vod__c, zvod_SaveNew_vod__c, Next_Call_Notes_vod__c,
        Pre_Call_Notes_vod__c, Mobile_ID_vod__c, zvod_Account_Credentials_vod_c_vod__c,
        zvod_Account_Preferred_Name_vod_c_vod__c, zvod_Account_Sample_Status_vod_c_vod__c, zvod_Attendees_vod__c,
        zvod_Key_Messages_vod__c, zvod_Detailing_vod__c, zvod_Expenses_vod__c, zvod_Followup_vod__c, zvod_Samples_vod__c,
        zvod_Save_vod__c, zvod_Submit_vod__c, zvod_Delete_vod__c, Activity_Type__c, Significant_Event__c, Location_vod__c,
        Subject_vod__c, Unlock_vod__c, Call_Datetime_vod__c, Disbursed_To_vod__c, Disclaimer_vod__c, Request_Receipt_vod__c,
        Signature_Date_vod__c, Signature_vod__c, Territory_vod__c, Submitted_By_Mobile_vod__c, Call_Type_vod__c,
        Add_Key_Message_vod__c, Address_vod__c, Attendees_vod__c, Attendee_Type_vod__c, Call_Date_vod__c,
        Detailed_Products_vod__c, No_Disbursement_vod__c, Parent_Call_vod__c, User_vod__c, Contact_vod__c,
        zvod_Entity_vod__c, Medical_Event_vod__c, Mobile_Created_Datetime_vod__c, Mobile_Last_Modified_Datetime_vod__c,
        License_vod__c, Is_Parent_Call_vod__c, Entity_Display_Name_vod__c, Override_Lock_vod__c, Last_Device_vod__c,
        Ship_Address_Line_1_vod__c, Ship_Address_Line_2_vod__c, Ship_City_vod__c, Ship_Country_vod__c,
        Ship_License_Expiration_Date_vod__c, Ship_License_Status_vod__c, Ship_License_vod__c, Ship_State_vod__c,
        Ship_To_Address_vod__c, Ship_Zip_vod__c, Ship_To_Address_Text_vod__c, CLM_vod__c, zvod_CLMDetails_vod__c,
        Is_Sampled_Call_vod__c, zvod_Surveys_vod__c, Presentations_vod__c, Entity_Reference_Id_vod__c,
        Error_Reference_Call_vod__c, Duration_vod__c, Color_vod__c, Allowed_Products_vod__c, zvod_Attachments_vod__c,
        Sample_Card_Reason_vod__c, ASSMCA_vod__c, Address_Line_1_vod__c, Address_Line_2_vod__c, City_vod__c,
        DEA_Address_Line_1_vod__c, DEA_Address_Line_2_vod__c, DEA_Address_vod__c, DEA_City_vod__c,
        DEA_Expiration_Date_vod__c, DEA_State_vod__c, DEA_Zip_4_vod__c, DEA_Zip_vod__c, DEA_vod__c, Ship_Zip_4_vod__c,
        State_vod__c, Zip_4_vod__c, Zip_vod__c, Sample_Send_Card_vod__c, zvod_Address_vod_c_DEA_Status_vod_c_vod__c,
        Signature_Page_Image_vod__c, Credentials_vod__c, Salutation_vod__c, zvod_Account_Call_Reminder_vod_c_vod__c,
        MSJ_Meeting_Duration__c, MSJ_Double_Visit_AM__c, zvod_Business_Account_vod__c, Product_Priority_1_vod__c,
        Product_Priority_2_vod__c, Product_Priority_3_vod__c, Product_Priority_4_vod__c, Product_Priority_5_vod__c,
        zvod_More_Actions_vod__c, zvod_Call_Conflict_Status_vod__c, Signature_Timestamp_vod__c, Expense_Amount_vod__c,
        Total_Expense_Attendees_Count_vod__c, Attendee_list_vod__c, Expense_Post_Status_vod__c, Attendee_Post_Status_vod__c,
        Expense_System_External_ID_vod__c, Incurred_Expense_vod__c, Assigner_vod__c, Assignment_Datetime_vod__c,
        zvod_Call_Objective_vod__c, Signature_Location_Longitude_vod__c, Signature_Location_Latitude_vod__c,
        Location_Services_Status_vod__c, MSJ_Double_Visit_Other__c, MSJ_Comment__c, MSJ_For_Reporting__c,
        MSJ_Number_of_Attendees__c, MSJ_Main_Dept__c, Planned_Type_vjh__c, Cobrowse_URL_Participant_vod__c,
        MSJ_Activity_Method_Text__c, MSJ_Activity_Method__c, MSJ_Classification__c, MSJ_Double_Visit_MSL__c,
        MSJ_MSL_Comment_for_MR__c, MSJ_APD__c, Medical_Inquiry_vod__c, Suggestion_vod__c, MSJ_Call_Type_MSJ__c, MSJ_Prescription_Request__c,
        MSJ_Patient_Follow__c, Child_Account_Id_vod__c, Child_Account_vod__c, Location_Id_vod__c, Location_Name_vod__c,
        MSJ_Comments_about_technology__c, Remote_Meeting_vod__c, Veeva_Remote_Meeting_Id_vod__c, MSJ_Activity_Type_Report__c,
        MSJ_Activity_Type__c, MSJ_Activity__c, MSJ_Comments__c, MSJ_Therapy__c, MSJ_Time_Hrs__c, EMDS_CO_Reference__c,
        EMDS_Call_Sub_Type__c, EMDS_Call_Type__c, EMDS_Call_Unsuccessful__c, EMDS_Congress_Type__c, EMDS_Date_of_Service__c,
        EMDS_Fertility_DisInterest__c, EMDS_Fertility_Interest__c, EMDS_Installed_Equipment__c, EMDS_Pipeline_Stage_Value__c,
        EMDS_Pipeline_Stage__c, EMDS_Pipeline__c, EMDS_Reason_for_Call__c, EMDS_Training_Completed__c, MSJ_BrainStorming__c,
        MSJ_SIPAGL_1A__c, MSJ_SIPAGL_1B__c, MSJ_SIPAGL_2__c, MSJ_SIPAGL_3__c, MSJ_SIPAGL_4A__c, MSJ_SIPAGL_5A__c,
        MSJ_SIPAGL_comment__c, MSJ_SIPAGL_4B__c, MSJ_SIPAGL_5B__c, Location_Text_vod__c, Call_Channel_vod__c,
        MSJ_Scientific_Interaction__c, MSJ_Activity_Email_Reply__c, MSJ_Interaction_Duration__c, MSJ_SIPAGL_1A_date__c,
        MSJ_CoPromotion__c, Call_Channel_Formula_vod__c, Meeting_Request_vod__c, Phone_vod__c, Detail_Section_Attribute_vod__c, Remote_Meeting_Type_vod__c,
        file_name, file_row_cnt, delete_flg, ins_user, ins_date, upd_user, upd_date)
    SELECT
        Id, OwnerId, IsDeleted, Name, RecordTypeId, CreatedDate, CreatedById, LastModifiedDate, LastModifiedById,
        SystemModstamp, LastActivityDate, MayEdit, IsLocked, LastViewedDate, LastReferencedDate, Call_Comments_vod__c,
        Sample_Card_vod__c, Add_Detail_vod__c, Property_vod__c, Account_vod__c, zvod_Product_Discussion_vod__c,
        Status_vod__c, Parent_Address_vod__c, Account_Plan_vod__c, zvod_SaveNew_vod__c, Next_Call_Notes_vod__c,
        Pre_Call_Notes_vod__c, Mobile_ID_vod__c, zvod_Account_Credentials_vod_c_vod__c,
        zvod_Account_Preferred_Name_vod_c_vod__c, zvod_Account_Sample_Status_vod_c_vod__c, zvod_Attendees_vod__c,
        zvod_Key_Messages_vod__c, zvod_Detailing_vod__c, zvod_Expenses_vod__c, zvod_Followup_vod__c, zvod_Samples_vod__c,
        zvod_Save_vod__c, zvod_Submit_vod__c, zvod_Delete_vod__c, Activity_Type__c, Significant_Event__c, Location_vod__c,
        Subject_vod__c, Unlock_vod__c, Call_Datetime_vod__c, Disbursed_To_vod__c, Disclaimer_vod__c, Request_Receipt_vod__c,
        Signature_Date_vod__c, Signature_vod__c, Territory_vod__c, Submitted_By_Mobile_vod__c, Call_Type_vod__c,
        Add_Key_Message_vod__c, Address_vod__c, Attendees_vod__c, Attendee_Type_vod__c, Call_Date_vod__c,
        Detailed_Products_vod__c, No_Disbursement_vod__c, Parent_Call_vod__c, User_vod__c, Contact_vod__c,
        zvod_Entity_vod__c, Medical_Event_vod__c, Mobile_Created_Datetime_vod__c, Mobile_Last_Modified_Datetime_vod__c,
        License_vod__c, Is_Parent_Call_vod__c, Entity_Display_Name_vod__c, Override_Lock_vod__c, Last_Device_vod__c,
        Ship_Address_Line_1_vod__c, Ship_Address_Line_2_vod__c, Ship_City_vod__c, Ship_Country_vod__c,
        Ship_License_Expiration_Date_vod__c, Ship_License_Status_vod__c, Ship_License_vod__c, Ship_State_vod__c,
        Ship_To_Address_vod__c, Ship_Zip_vod__c, Ship_To_Address_Text_vod__c, CLM_vod__c, zvod_CLMDetails_vod__c,
        Is_Sampled_Call_vod__c, zvod_Surveys_vod__c, Presentations_vod__c, Entity_Reference_Id_vod__c,
        Error_Reference_Call_vod__c, Duration_vod__c, Color_vod__c, Allowed_Products_vod__c, zvod_Attachments_vod__c,
        Sample_Card_Reason_vod__c, ASSMCA_vod__c, Address_Line_1_vod__c, Address_Line_2_vod__c, City_vod__c,
        DEA_Address_Line_1_vod__c, DEA_Address_Line_2_vod__c, DEA_Address_vod__c, DEA_City_vod__c,
        DEA_Expiration_Date_vod__c, DEA_State_vod__c, DEA_Zip_4_vod__c, DEA_Zip_vod__c, DEA_vod__c, Ship_Zip_4_vod__c,
        State_vod__c, Zip_4_vod__c, Zip_vod__c, Sample_Send_Card_vod__c, zvod_Address_vod_c_DEA_Status_vod_c_vod__c,
        Signature_Page_Image_vod__c, Credentials_vod__c, Salutation_vod__c, zvod_Account_Call_Reminder_vod_c_vod__c,
        MSJ_Meeting_Duration__c, MSJ_Double_Visit_AM__c, zvod_Business_Account_vod__c, Product_Priority_1_vod__c,
        Product_Priority_2_vod__c, Product_Priority_3_vod__c, Product_Priority_4_vod__c, Product_Priority_5_vod__c,
        zvod_More_Actions_vod__c, zvod_Call_Conflict_Status_vod__c, Signature_Timestamp_vod__c, Expense_Amount_vod__c,
        Total_Expense_Attendees_Count_vod__c, Attendee_list_vod__c, Expense_Post_Status_vod__c, Attendee_Post_Status_vod__c,
        Expense_System_External_ID_vod__c, Incurred_Expense_vod__c, Assigner_vod__c, Assignment_Datetime_vod__c,
        zvod_Call_Objective_vod__c, Signature_Location_Longitude_vod__c, Signature_Location_Latitude_vod__c,
        Location_Services_Status_vod__c, MSJ_Double_Visit_Other__c, MSJ_Comment__c, MSJ_For_Reporting__c,
        MSJ_Number_of_Attendees__c, MSJ_Main_Dept__c, Planned_Type_vjh__c, Cobrowse_URL_Participant_vod__c,
        MSJ_Activity_Method_Text__c, MSJ_Activity_Method__c, MSJ_Classification__c, MSJ_Double_Visit_MSL__c,
        MSJ_MSL_Comment_for_MR__c, MSJ_APD__c, Medical_Inquiry_vod__c, Suggestion_vod__c, MSJ_Call_Type_MSJ__c, MSJ_Prescription_Request__c,
        MSJ_Patient_Follow__c, Child_Account_Id_vod__c, Child_Account_vod__c, Location_Id_vod__c, Location_Name_vod__c,
        MSJ_Comments_about_technology__c, Remote_Meeting_vod__c, Veeva_Remote_Meeting_Id_vod__c, MSJ_Activity_Type_Report__c,
        MSJ_Activity_Type__c, MSJ_Activity__c, MSJ_Comments__c, MSJ_Therapy__c, MSJ_Time_Hrs__c, EMDS_CO_Reference__c,
        EMDS_Call_Sub_Type__c, EMDS_Call_Type__c, EMDS_Call_Unsuccessful__c, EMDS_Congress_Type__c, EMDS_Date_of_Service__c,
        EMDS_Fertility_DisInterest__c, EMDS_Fertility_Interest__c, EMDS_Installed_Equipment__c, EMDS_Pipeline_Stage_Value__c,
        EMDS_Pipeline_Stage__c, EMDS_Pipeline__c, EMDS_Reason_for_Call__c, EMDS_Training_Completed__c, MSJ_BrainStorming__c,
        MSJ_SIPAGL_1A__c, MSJ_SIPAGL_1B__c, MSJ_SIPAGL_2__c, MSJ_SIPAGL_3__c, MSJ_SIPAGL_4A__c, MSJ_SIPAGL_5A__c,
        MSJ_SIPAGL_comment__c, MSJ_SIPAGL_4B__c, MSJ_SIPAGL_5B__c, Location_Text_vod__c, Call_Channel_vod__c,
        MSJ_Scientific_Interaction__c, MSJ_Activity_Email_Reply__c, MSJ_Interaction_Duration__c, MSJ_SIPAGL_1A_date__c,
        MSJ_CoPromotion__c, Call_Channel_Formula_vod__c, Meeting_Request_vod__c, Phone_vod__c, Detail_Section_Attribute_vod__c, Remote_Meeting_Type_vod__c,
        file_name, file_row_cnt, delete_flg, ins_user, ins_date, upd_user, upd_date
    FROM
        internal02.crm_Call2_vod__c AS internaltb
    WHERE
        internaltb.Id = ?
    ON DUPLICATE KEY UPDATE
        OwnerId = internaltb.OwnerId, IsDeleted = internaltb.IsDeleted, Name = internaltb.Name,
        RecordTypeId = internaltb.RecordTypeId, CreatedDate = internaltb.CreatedDate, CreatedById = internaltb.CreatedById,
        LastModifiedDate = internaltb.LastModifiedDate, LastModifiedById = internaltb.LastModifiedById,
        SystemModstamp = internaltb.SystemModstamp, LastActivityDate = internaltb.LastActivityDate,
        MayEdit = internaltb.MayEdit, IsLocked = internaltb.IsLocked, LastViewedDate = internaltb.LastViewedDate,
        LastReferencedDate = internaltb.LastReferencedDate, Call_Comments_vod__c = internaltb.Call_Comments_vod__c,
        Sample_Card_vod__c = internaltb.Sample_Card_vod__c, Add_Detail_vod__c = internaltb.Add_Detail_vod__c,
        Property_vod__c = internaltb.Property_vod__c, Account_vod__c = internaltb.Account_vod__c,
        zvod_Product_Discussion_vod__c = internaltb.zvod_Product_Discussion_vod__c, Status_vod__c = internaltb.Status_vod__c,
        Parent_Address_vod__c = internaltb.Parent_Address_vod__c, Account_Plan_vod__c = internaltb.Account_Plan_vod__c,
        zvod_SaveNew_vod__c = internaltb.zvod_SaveNew_vod__c, Next_Call_Notes_vod__c = internaltb.Next_Call_Notes_vod__c,
        Pre_Call_Notes_vod__c = internaltb.Pre_Call_Notes_vod__c, Mobile_ID_vod__c = internaltb.Mobile_ID_vod__c,
        zvod_Account_Credentials_vod_c_vod__c = internaltb.zvod_Account_Credentials_vod_c_vod__c,
        zvod_Account_Preferred_Name_vod_c_vod__c = internaltb.zvod_Account_Preferred_Name_vod_c_vod__c,
        zvod_Account_Sample_Status_vod_c_vod__c = internaltb.zvod_Account_Sample_Status_vod_c_vod__c,
        zvod_Attendees_vod__c = internaltb.zvod_Attendees_vod__c, zvod_Key_Messages_vod__c = internaltb.zvod_Key_Messages_vod__c,
        zvod_Detailing_vod__c = internaltb.zvod_Detailing_vod__c, zvod_Expenses_vod__c = internaltb.zvod_Expenses_vod__c,
        zvod_Followup_vod__c = internaltb.zvod_Followup_vod__c, zvod_Samples_vod__c = internaltb.zvod_Samples_vod__c,
        zvod_Save_vod__c = internaltb.zvod_Save_vod__c, zvod_Submit_vod__c = internaltb.zvod_Submit_vod__c,
        zvod_Delete_vod__c = internaltb.zvod_Delete_vod__c, Activity_Type__c = internaltb.Activity_Type__c,
        Significant_Event__c = internaltb.Significant_Event__c, Location_vod__c = internaltb.Location_vod__c,
        Subject_vod__c = internaltb.Subject_vod__c, Unlock_vod__c = internaltb.Unlock_vod__c,
        Call_Datetime_vod__c = internaltb.Call_Datetime_vod__c, Disbursed_To_vod__c = internaltb.Disbursed_To_vod__c,
        Disclaimer_vod__c = internaltb.Disclaimer_vod__c, Request_Receipt_vod__c = internaltb.Request_Receipt_vod__c,
        Signature_Date_vod__c = internaltb.Signature_Date_vod__c, Signature_vod__c = internaltb.Signature_vod__c,
        Territory_vod__c = internaltb.Territory_vod__c, Submitted_By_Mobile_vod__c = internaltb.Submitted_By_Mobile_vod__c,
        Call_Type_vod__c = internaltb.Call_Type_vod__c, Add_Key_Message_vod__c = internaltb.Add_Key_Message_vod__c,
        Address_vod__c = internaltb.Address_vod__c, Attendees_vod__c = internaltb.Attendees_vod__c,
        Attendee_Type_vod__c = internaltb.Attendee_Type_vod__c, Call_Date_vod__c = internaltb.Call_Date_vod__c,
        Detailed_Products_vod__c = internaltb.Detailed_Products_vod__c, No_Disbursement_vod__c = internaltb.No_Disbursement_vod__c,
        Parent_Call_vod__c = internaltb.Parent_Call_vod__c, User_vod__c = internaltb.User_vod__c,
        Contact_vod__c = internaltb.Contact_vod__c, zvod_Entity_vod__c = internaltb.zvod_Entity_vod__c,
        Medical_Event_vod__c = internaltb.Medical_Event_vod__c, Mobile_Created_Datetime_vod__c = internaltb.Mobile_Created_Datetime_vod__c,
        Mobile_Last_Modified_Datetime_vod__c = internaltb.Mobile_Last_Modified_Datetime_vod__c, License_vod__c = internaltb.License_vod__c,
        Is_Parent_Call_vod__c = internaltb.Is_Parent_Call_vod__c, Entity_Display_Name_vod__c = internaltb.Entity_Display_Name_vod__c,
        Override_Lock_vod__c = internaltb.Override_Lock_vod__c, Last_Device_vod__c = internaltb.Last_Device_vod__c,
        Ship_Address_Line_1_vod__c = internaltb.Ship_Address_Line_1_vod__c, Ship_Address_Line_2_vod__c = internaltb.Ship_Address_Line_2_vod__c,
        Ship_City_vod__c = internaltb.Ship_City_vod__c, Ship_Country_vod__c = internaltb.Ship_Country_vod__c,
        Ship_License_Expiration_Date_vod__c = internaltb.Ship_License_Expiration_Date_vod__c,
        Ship_License_Status_vod__c = internaltb.Ship_License_Status_vod__c, Ship_License_vod__c = internaltb.Ship_License_vod__c,
        Ship_State_vod__c = internaltb.Ship_State_vod__c, Ship_To_Address_vod__c = internaltb.Ship_To_Address_vod__c,
        Ship_Zip_vod__c = internaltb.Ship_Zip_vod__c, Ship_To_Address_Text_vod__c = internaltb.Ship_To_Address_Text_vod__c,
        CLM_vod__c = internaltb.CLM_vod__c, zvod_CLMDetails_vod__c = internaltb.zvod_CLMDetails_vod__c,
        Is_Sampled_Call_vod__c = internaltb.Is_Sampled_Call_vod__c, zvod_Surveys_vod__c = internaltb.zvod_Surveys_vod__c,
        Presentations_vod__c = internaltb.Presentations_vod__c, Entity_Reference_Id_vod__c = internaltb.Entity_Reference_Id_vod__c,
        Error_Reference_Call_vod__c = internaltb.Error_Reference_Call_vod__c, Duration_vod__c = internaltb.Duration_vod__c,
        Color_vod__c = internaltb.Color_vod__c, Allowed_Products_vod__c = internaltb.Allowed_Products_vod__c,
        zvod_Attachments_vod__c = internaltb.zvod_Attachments_vod__c, Sample_Card_Reason_vod__c = internaltb.Sample_Card_Reason_vod__c,
        ASSMCA_vod__c = internaltb.ASSMCA_vod__c, Address_Line_1_vod__c = internaltb.Address_Line_1_vod__c,
        Address_Line_2_vod__c = internaltb.Address_Line_2_vod__c, City_vod__c = internaltb.City_vod__c,
        DEA_Address_Line_1_vod__c = internaltb.DEA_Address_Line_1_vod__c, DEA_Address_Line_2_vod__c = internaltb.DEA_Address_Line_2_vod__c,
        DEA_Address_vod__c = internaltb.DEA_Address_vod__c, DEA_City_vod__c = internaltb.DEA_City_vod__c,
        DEA_Expiration_Date_vod__c = internaltb.DEA_Expiration_Date_vod__c, DEA_State_vod__c = internaltb.DEA_State_vod__c,
        DEA_Zip_4_vod__c = internaltb.DEA_Zip_4_vod__c, DEA_Zip_vod__c = internaltb.DEA_Zip_vod__c, DEA_vod__c = internaltb.DEA_vod__c,
        Ship_Zip_4_vod__c = internaltb.Ship_Zip_4_vod__c, State_vod__c = internaltb.State_vod__c, Zip_4_vod__c = internaltb.Zip_4_vod__c,
        Zip_vod__c = internaltb.Zip_vod__c, Sample_Send_Card_vod__c = internaltb.Sample_Send_Card_vod__c,
        zvod_Address_vod_c_DEA_Status_vod_c_vod__c = internaltb.zvod_Address_vod_c_DEA_Status_vod_c_vod__c,
        Signature_Page_Image_vod__c = internaltb.Signature_Page_Image_vod__c, Credentials_vod__c = internaltb.Credentials_vod__c,
        Salutation_vod__c = internaltb.Salutation_vod__c,
        zvod_Account_Call_Reminder_vod_c_vod__c = internaltb.zvod_Account_Call_Reminder_vod_c_vod__c,
        MSJ_Meeting_Duration__c = internaltb.MSJ_Meeting_Duration__c, MSJ_Double_Visit_AM__c = internaltb.MSJ_Double_Visit_AM__c,
        zvod_Business_Account_vod__c = internaltb.zvod_Business_Account_vod__c, Product_Priority_1_vod__c = internaltb.Product_Priority_1_vod__c,
        Product_Priority_2_vod__c = internaltb.Product_Priority_2_vod__c, Product_Priority_3_vod__c = internaltb.Product_Priority_3_vod__c,
        Product_Priority_4_vod__c = internaltb.Product_Priority_4_vod__c, Product_Priority_5_vod__c = internaltb.Product_Priority_5_vod__c,
        zvod_More_Actions_vod__c = internaltb.zvod_More_Actions_vod__c,
        zvod_Call_Conflict_Status_vod__c = internaltb.zvod_Call_Conflict_Status_vod__c,
        Signature_Timestamp_vod__c = internaltb.Signature_Timestamp_vod__c, Expense_Amount_vod__c = internaltb.Expense_Amount_vod__c,
        Total_Expense_Attendees_Count_vod__c = internaltb.Total_Expense_Attendees_Count_vod__c,
        Attendee_list_vod__c = internaltb.Attendee_list_vod__c, Expense_Post_Status_vod__c = internaltb.Expense_Post_Status_vod__c,
        Attendee_Post_Status_vod__c = internaltb.Attendee_Post_Status_vod__c,
        Expense_System_External_ID_vod__c = internaltb.Expense_System_External_ID_vod__c,
        Incurred_Expense_vod__c = internaltb.Incurred_Expense_vod__c, Assigner_vod__c = internaltb.Assigner_vod__c,
        Assignment_Datetime_vod__c = internaltb.Assignment_Datetime_vod__c, zvod_Call_Objective_vod__c = internaltb.zvod_Call_Objective_vod__c,
        Signature_Location_Longitude_vod__c = internaltb.Signature_Location_Longitude_vod__c,
        Signature_Location_Latitude_vod__c = internaltb.Signature_Location_Latitude_vod__c,
        Location_Services_Status_vod__c = internaltb.Location_Services_Status_vod__c,
        MSJ_Double_Visit_Other__c = internaltb.MSJ_Double_Visit_Other__c, MSJ_Comment__c = internaltb.MSJ_Comment__c,
        MSJ_For_Reporting__c = internaltb.MSJ_For_Reporting__c, MSJ_Number_of_Attendees__c = internaltb.MSJ_Number_of_Attendees__c,
        MSJ_Main_Dept__c = internaltb.MSJ_Main_Dept__c, Planned_Type_vjh__c = internaltb.Planned_Type_vjh__c,
        Cobrowse_URL_Participant_vod__c = internaltb.Cobrowse_URL_Participant_vod__c, MSJ_Activity_Method_Text__c = internaltb.MSJ_Activity_Method_Text__c,
        MSJ_Activity_Method__c = internaltb.MSJ_Activity_Method__c, MSJ_Classification__c = internaltb.MSJ_Classification__c,
        MSJ_Double_Visit_MSL__c = internaltb.MSJ_Double_Visit_MSL__c, MSJ_MSL_Comment_for_MR__c = internaltb.MSJ_MSL_Comment_for_MR__c,
        MSJ_APD__c = internaltb.MSJ_APD__c, Medical_Inquiry_vod__c = internaltb.Medical_Inquiry_vod__c, Suggestion_vod__c = internaltb.Suggestion_vod__c,
        MSJ_Call_Type_MSJ__c = internaltb.MSJ_Call_Type_MSJ__c, MSJ_Prescription_Request__c = internaltb.MSJ_Prescription_Request__c,
        MSJ_Patient_Follow__c = internaltb.MSJ_Patient_Follow__c, Child_Account_Id_vod__c = internaltb.Child_Account_Id_vod__c,
        Child_Account_vod__c = internaltb.Child_Account_vod__c, Location_Id_vod__c = internaltb.Location_Id_vod__c,
        Location_Name_vod__c = internaltb.Location_Name_vod__c, MSJ_Comments_about_technology__c = internaltb.MSJ_Comments_about_technology__c,
        Remote_Meeting_vod__c = internaltb.Remote_Meeting_vod__c, Veeva_Remote_Meeting_Id_vod__c = internaltb.Veeva_Remote_Meeting_Id_vod__c,
        MSJ_Activity_Type_Report__c = internaltb.MSJ_Activity_Type_Report__c, MSJ_Activity_Type__c = internaltb.MSJ_Activity_Type__c,
        MSJ_Activity__c = internaltb.MSJ_Activity__c, MSJ_Comments__c = internaltb.MSJ_Comments__c, MSJ_Therapy__c = internaltb.MSJ_Therapy__c,
        MSJ_Time_Hrs__c = internaltb.MSJ_Time_Hrs__c, EMDS_CO_Reference__c = internaltb.EMDS_CO_Reference__c,
        EMDS_Call_Sub_Type__c = internaltb.EMDS_Call_Sub_Type__c, EMDS_Call_Type__c = internaltb.EMDS_Call_Type__c,
        EMDS_Call_Unsuccessful__c = internaltb.EMDS_Call_Unsuccessful__c, EMDS_Congress_Type__c = internaltb.EMDS_Congress_Type__c,
        EMDS_Date_of_Service__c = internaltb.EMDS_Date_of_Service__c, EMDS_Fertility_DisInterest__c = internaltb.EMDS_Fertility_DisInterest__c,
        EMDS_Fertility_Interest__c = internaltb.EMDS_Fertility_Interest__c, EMDS_Installed_Equipment__c = internaltb.EMDS_Installed_Equipment__c,
        EMDS_Pipeline_Stage_Value__c = internaltb.EMDS_Pipeline_Stage_Value__c, EMDS_Pipeline_Stage__c = internaltb.EMDS_Pipeline_Stage__c,
        EMDS_Pipeline__c = internaltb.EMDS_Pipeline__c, EMDS_Reason_for_Call__c = internaltb.EMDS_Reason_for_Call__c,
        EMDS_Training_Completed__c = internaltb.EMDS_Training_Completed__c, MSJ_BrainStorming__c = internaltb.MSJ_BrainStorming__c,
        MSJ_SIPAGL_1A__c = internaltb.MSJ_SIPAGL_1A__c, MSJ_SIPAGL_1B__c = internaltb.MSJ_SIPAGL_1B__c,
        MSJ_SIPAGL_2__c = internaltb.MSJ_SIPAGL_2__c, MSJ_SIPAGL_3__c = internaltb.MSJ_SIPAGL_3__c, MSJ_SIPAGL_4A__c = internaltb.MSJ_SIPAGL_4A__c,
        MSJ_SIPAGL_5A__c = internaltb.MSJ_SIPAGL_5A__c, MSJ_SIPAGL_comment__c = internaltb.MSJ_SIPAGL_comment__c,
        MSJ_SIPAGL_4B__c = internaltb.MSJ_SIPAGL_4B__c, MSJ_SIPAGL_5B__c = internaltb.MSJ_SIPAGL_5B__c,
        Location_Text_vod__c = internaltb.Location_Text_vod__c, Call_Channel_vod__c = internaltb.Call_Channel_vod__c,
        MSJ_Scientific_Interaction__c = internaltb.MSJ_Scientific_Interaction__c,
        MSJ_Activity_Email_Reply__c = internaltb.MSJ_Activity_Email_Reply__c,
        MSJ_Interaction_Duration__c = internaltb.MSJ_Interaction_Duration__c,
        MSJ_SIPAGL_1A_date__c = internaltb.MSJ_SIPAGL_1A_date__c, MSJ_CoPromotion__c = internaltb.MSJ_CoPromotion__c,
        Call_Channel_Formula_vod__c = internaltb.Call_Channel_Formula_vod__c,
        Meeting_Request_vod__c = internaltb.Meeting_Request_vod__c, Phone_vod__c = internaltb.Phone_vod__c,
        Detail_Section_Attribute_vod__c = internaltb.Detail_Section_Attribute_vod__c, Remote_Meeting_Type_vod__c = internaltb.Remote_Meeting_Type_vod__c,
        file_name = internaltb.file_name, file_row_cnt = internaltb.file_row_cnt,
        upd_user = CURRENT_USER(), upd_date = CURRENT_TIMESTAMP();';

OPEN table_cursor;

-- ループ
TableCursorLoop: LOOP
    -- スキーマ取得
    FETCH table_cursor INTO temp_table_id, target_column_value;

    -- 終了条件ループ抜き
    IF fetch_done THEN
        LEAVE TableCursorLoop;
    END IF;

    SET @distribution_schema = internal02.get_distribution_to_schema('crm_Call2_vod__c', 'RecordTypeId', target_column_value);
    SET @temp_table_id = temp_table_id;

    SET @upsert_statement = REPLACE(@upsert_statement_base, "$$distribution_schema$$", @distribution_schema);

    -- UPSERT実行
    PREPARE stmt FROM @upsert_statement;
    EXECUTE stmt USING @temp_table_id;
    DEALLOCATE PREPARE stmt;

END LOOP;
CLOSE table_cursor;
COMMIT;

END