Chit Khine Chit Khine - 7 months ago 12
SQL Question

Oracle, inserting rows by using Max+1 of the existing field

Hi I am pretty new to oracle.
I have to insert a row using a value of (max + 1) from the existing field.
Does anyone has any idea on how to achieve that?

Answer

Do you mean like this?

SQL> create table t (col) as (select 4 from dual union all select 9 from dual);

Table created.

SQL> select * from t;

       COL
----------
         4
         9

2 rows selected.

SQL> insert into t values (1 + (select max(col) from t));

1 row created.

SQL> select * from t;

       COL
----------
         4
         9
        10

3 rows selected.