DO $$ 
begin
    IF (EXISTS (SELECT 1 FROM public.T_ENTITY_IDS where EntName = 'RDS.EXPORT_FRLLO_NEEDS_ELEMENT' and FieldName = 'PrimaryKey'))
    THEN
    	update public.T_ENTITY_IDS set id = Coalesce((select max(ExportFrlloNeedsElementID) + 1 from RDS.T_EXPORT_FRLLO_NEEDS_ELEMENT), 0) where EntName = 'RDS.EXPORT_FRLLO_NEEDS_ELEMENT' and FieldName = 'PrimaryKey';
    ELSE
        
    	INSERT INTO public.T_ENTITY_IDS
               (EntName
               ,FieldName
               ,id)
         VALUES
               ('RDS.EXPORT_FRLLO_NEEDS_ELEMENT'
               ,'PrimaryKey'
               ,Coalesce((select max(ExportFrlloNeedsElementID) + 1 from RDS.T_EXPORT_FRLLO_NEEDS_ELEMENT), 0));
    
    END IF;
    
    IF (EXISTS (SELECT 1 FROM public.T_ENTITY_IDS where EntName = 'RDS.EXPORT_FRLLO_NEEDS_EQUIP' and FieldName = 'PrimaryKey'))
    THEN
    	update public.T_ENTITY_IDS set id = Coalesce((select max(ExportFrlloNeedsEquipID) + 1 from RDS.T_EXPORT_FRLLO_NEEDS_EQUIP), 0) where EntName = 'RDS.EXPORT_FRLLO_NEEDS_EQUIP' and FieldName = 'PrimaryKey';
    ELSE
        
    	INSERT INTO public.T_ENTITY_IDS
               (EntName
               ,FieldName
               ,id)
         VALUES
               ('RDS.EXPORT_FRLLO_NEEDS_EQUIP'
               ,'PrimaryKey'
               ,Coalesce((select max(ExportFrlloNeedsEquipID) + 1 from RDS.T_EXPORT_FRLLO_NEEDS_EQUIP), 0));
    
    END IF;
end;
$$
