I have many tables with a trigger attached like this
BEFORE INSERT
ON BRULES
...snip...
if inserting and :new.ID is NULL then
SELECT BRULES_SEQ.nextval into :new.ID FROM DUAL;
end if;
Due to an interesting coding style the application does inserts on some tables with a null ID which fires the trigger and uses the existing sequence. For other tables it gets the ID of the newest record and adds 1. In this case the sequence is not used and gets out of synch with the number of records in the table.
How can I tell if the next value of a sequence is valid for an insert? I could read all triggers for the sequence name and then do some dynamic SQL to compare the maximum ID and the current value of the sequence but it seems a bit clunky.