PROMPT Force delete duplicate Attribute Values

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

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

  for atv in
  (
    select atv.id
    from ci_attribute_values@r212 atv
    where atv.id < ( select max(atv2.id)
                     from ci_attribute_values@r212 atv2
                     where atv.id != atv2.id
                     and atv.low_value = atv2.low_value
                     and atv.attribute_reference = atv2.attribute_reference 
			 )
    and atv.application_system_owned_by in ( select ci_appid
	    					         from ck_migration_objects
						         where ci_stage = 'APPSEL' )
  )
  loop
    ciiutl.delete_element@r212(atv.id, 'ATV');
  
    delete from sdd_elements@r212
    where el_id = atv.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;
/
