PROMPT Force Delete funtion attribute usages that have invalid 'funent_reference' 

DECLARE
   bug_no varchar2(8) := 'CK908208';
BEGIN

  update ck_migration_objects set ci_created = 'N' where ci_stage = 'CKPREMIG' and ci_name = bug_no;

  for funatt in 
  (
    select funatt.id id
    from   ci_function_attribute_usages@r212 funatt      
    where  not exists ( select 1
                        from   ci_function_entity_usages@r212 funent
				        where  funent.id = funatt.funent_reference) 
    and funatt.application_system_owned_by in ( select ci_appid
	    					            from ck_migration_objects
						            where ci_stage = 'APPSEL' )
  )
  loop
    ciiutl.delete_element@r212(funatt.id, 'FUNATT');
  
    delete from sdd_structure_elements@r212
    where str_id = funatt.id;

  end loop; 
  
  update ck_migration_objects set ci_created = 'Y' where ci_stage = 'CKPREMIG' and ci_name = bug_no;
   
  commit;
  
EXCEPTION 
  when others then
     dbms_output.put_line (' ** Exception Raised ** ');
     dbms_output.put_line ('...ERROR: '||substrb(sqlerrm, 1, 200));
     raise;
	 
end;
/

