 
declare
bid     alias for $1;
eid     alias for $2;
msg text;
rowcnt  int;
begin
-- Confirm snap IDs
PERFORM 1 from edb$snap where edb_id = bid;
if not found then
     raise exception 'Begin snapshot identifier % does not exist', bid::text;
END IF;
PERFORM 1 from edb$snap where edb_id = eid;
if not found then
     raise exception 'End snapshot identifier % does not exist', eid::text;
END IF;
delete from edb$system_waits where edb_id between bid and eid;
delete from edb$session_waits where edb_id between bid and eid; 
delete from edb$session_wait_history where edb_id between bid and eid; 
delete from edb$snap where edb_id between bid and eid; 
-- added 01/07/2008 - P. Steinheuser
delete from edb$stat_database where edb_id between bid and eid; 
delete from edb$stat_all_tables where edb_id between bid and eid; 
delete from edb$statio_all_tables where edb_id between bid and eid; 
delete from edb$stat_all_indexes where edb_id between bid and eid; 
delete from edb$statio_all_indexes where edb_id between bid and eid; 
get diagnostics rowcnt = ROW_COUNT;
if rowcnt = 0  then
  msg := 'Snapshots not found.';
else
    msg := 'Snapshots in range '||bid||' to '||eid||' deleted.';
end if;
-- msg := 'Rows deleted = '||rowcnt;
  return msg;
exception
when others then
raise notice '%', SQLERRM;
msg := 'Function failed.';
return msg;
end;
   