0

How can I add a Rownum id to a set of data, I want this id to be unique and when I make a new insert this Rownum will increase. SQL, PL/SQL

For example I have this data:

Name    Age
Anne    26
Mary    45

I want to add a Rownum id that starts from 1 and increase for each row. The result expected is

Id    Name     Age

1      Anne    26

2      Mary    45

When I insert a new row the id will be 3

Id    Name     Age

1      Anne    26

2      Mary    45

3      Jhon    41

I think I need to do the maximum of Rownum for that but I can't find the perfect SQL/ PL/SQL code.

SELECT NVL(MAX(ID),0) FROM TABLE;
MT0
  • 143,790
  • 11
  • 59
  • 117
Anne.07
  • 31
  • 2
  • 2
    Don't use `MAX` to generate sequence numbers as you will get duplicate values of two sessions run the same query at the same time and then when you try to insert that value one session will get an error. Instead, as per the linked duplicate, use a sequence or an `IDENTITY` column. – MT0 May 05 '23 at 10:23

0 Answers0