title: Best way to generate unique and consecutive numbers in Oracle
body: <p>I need to generate <strong>unique and consecutive</strong> numbers (for use on an invoice), in a fast and reliable way. I currently use an Oracle sequence, but in some cases, the generated numbers are <strong>not consecutive</strong> because of exceptions that may occur.</p>
  <p>I thought a couple of solutions to manage this problem, but neither of them convincing me. What solution do you recommend?</p>
  <ol>
  <li><p>Use a select max ()</p>
  <pre><code> SELECT MAX (NVL (doc_num, 0)) +1 FROM invoices
  </code></pre>
  </li>
  <li><p>Use a table to store the last number generated for the invoice.</p>
  <pre><code> UPDATE docs_numbers
       SET last_invoice = last_invoice + 1
  </code></pre>
  </li>
  <li><p>Another Solution?</p>
  </li>
  </ol>