PROMPT Force delete duplicate View Implementations

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

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

  for vwi in
  (
    select vwi.id id
    from ci_view_implementations@r212 vwi
    where vwi.id < ( select max(vwi2.id)
 			   from ci_view_implementations@r212 vwi2
                     where vwi.id != vwi2.id
                     and vwi.database_user_reference = vwi2.database_user_reference 
                     and vwi.view_definition_reference = vwi2.view_definition_reference
                   )
     and vwi.application_system_owned_by in ( select ci_appid
	    				                from ck_migration_objects
						          where ci_stage = 'APPSEL' )
  )
  loop
    ciiutl.delete_element@r212(vwi.id, 'DOI');
  
    delete from sdd_elements@r212
    where el_id = vwi.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;
/
