     end;
    if existingPatientNum is not null then 
         execute immediate ' update ' || tempPidTableName ||' set patient_num = patient_id, process_status_flag = ''P''
-        where patient_id = :x and not exists (select 1 from patient_mapping pm where pm.patient_ide = patient_map_id
+        where patient_id = :x and patient_id_source = ''HIVE'' and not exists (select 1 from patient_mapping pm where pm.patient_ide = patient_map_id
         and pm.patient_ide_source = patient_map_id_source)' using disPatientId;
    else 
         -- generate new patient_num i.e. take max(patient_num) + 1 

             ,encounter_map_id_status,update_date,download_date,import_date,sourcesystem_cd) 
             values(:x,''HIVE'',:y,''HIVE'',:z,:w,:v,''P'',''A'',sysdate,sysdate,sysdate,''edu.harvard.i2b2.crc'')' using maxEncounterNum,maxEncounterNum,maxEncounterNum,patientMapId,patientMapIdSource; 
            execute immediate ' update ' || tempEidTableName ||' set encounter_num =  :x , process_status_flag = ''P'' 
            where encounter_id = :y and  not exists (select 1 from 
            encounter_mapping em where em.encounter_ide = encounter_map_id
            and em.encounter_ide_source = encounter_map_id_source)' using maxEncounterNum, disEncounterId;
            
       end if ;
    

        execute immediate ' update ' || tempPidTableName ||' set patient_num = patient_id, process_status_flag = ''P'' where 
        patient_id = :x and patient_id_source = ''HIVE'' and not exists (select 1 from patient_mapping pm where pm.patient_ide = patient_map_id
        and pm.patient_ide_source = patient_map_id_source)' using disPatientId;
   end if;
    
   -- test if record fectched
   -- dbms_output.put_line(' HIVE ');
