Hmm... not sure where you struggled, but it's actually straight forward:
do begin declare back_id BIGINT =0; declare cmd NVARCHAR(2000); -- find the backup id we want to base the deletion on: SELECT min(to_bigint(BACKUP_ID)) into back_id FROM SYS.M_BACKUP_CATALOG where SYS_START_TIME >= ADD_DAYS(CURRENT_TIMESTAMP, -90) and ENTRY_TYPE_NAME = 'complete data backup' and STATE_NAME = 'successful'; -- build the delete statement cmd := 'BACKUP CATALOG DELETE ALL BEFORE BACKUP_ID ' || :back_id || ' COMPLETE'; -- run the statement we just built execute immediate (:cmd); end;