Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How get next number some like sequence – Oracle PLSQL

Hello in table I have data not use sequence (Row where num = 4 is deleted), How get number 4 for next insert? After insert row where num = 4, next num is 6…

eg.

Select 1 as num from dual
union all
Select 2 as num from dual
union all
Select 3 as num from dual
union all
Select 5 as num from dual

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this one out:

WITH dat AS
(
SELECT 1 AS NUM
  FROM DUAL
 UNION ALL
SELECT 2 AS NUM
  FROM DUAL
 UNION ALL
SELECT 3 AS NUM
  FROM DUAL
 UNION ALL
SELECT 5 AS NUM
  FROM DUAL
)
SELECT d1.num + 1 AS next_val
  FROM dat d1
 WHERE NOT EXISTS (SELECT NULL
                     FROM dat d2
                    WHERE d2.num = d1.num + 1)
 ORDER BY next_val;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading