SELECT NVL(Min(t1.Accnum), 0) + 1 INTO newAccnum FROM T1 t1 WHERE (t1.accnum + 1) NOT IN (SELECT t2.accnum FROM T2 t2) AND t1.accnum > 0 AND EXISTS (SELECT t3.accnum FROM T3 t3 WHERE t3.accnum = 1);