DECLARE
v_dbid number;
v_inst_num number;

cursor c2 is
select * from stats$database_instance di
where instance_number = v_inst_num
and dbid = v_dbid
and not exists (select 1
from stats$snapshot s
where s.dbid = di.dbid
and s.instance_number = di.instance_number
and s.startup_time = di.startup_time);

BEGIN

-- Get info about the Database and instance
--select dbid into v_dbid from v$database;
v_dbid := 3824701041;
--select distinct instance_number into v_inst_num from stats$database_instance where dbid = v_dbid;
v_inst_num := 1;

for rec in c2 loop
delete from stats$database_instance where snap_id=rec.snap_id;
end loop;
commit;
if SQL%NOTFOUND then
dbms_output.put_line('No hanging SQL to delete');
else
dbms_output.put_line('Hanging SQL deleted');
end if;

END;
/