PROMPT Force delete Foreign Key Constraints that have an invalid 'unique_key_reference'
PROMPT or 'primary_key_reference'

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

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

  for cco in
  (
    select cco.id id
    from ci_foreign_key_constraints@r212 cco
    where unique_key_reference is not null
    and   cco.application_system_owned_by in ( select ci_appid
	    					           from ck_migration_objects
						           where ci_stage = 'APPSEL' )
    and not exists ( select 1 
                     from ci_constraints@r212 con
                     where con.id = cco.unique_key_reference               
                   )
  )
  loop
    ciiutl.delete_element@r212(cco.id, 'OCO');
  
    delete from sdd_elements@r212
    where el_id = cco.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;
/
