Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert maximum selected value as new record oracle

i'm using oracle DB . and i'm trying to select max number from table and then increment it by one then re-insert it as new record for example: insert into table1(id,name) values (select max(id) from table1 + 1,'name2');

but it gave me missing expression error.

thanks in advance.

like image 581
Samy Louize Hanna Avatar asked Feb 07 '14 16:02

Samy Louize Hanna


People also ask

Can we insert multiple values to a table at a time in Oracle?

Multiple rows are inserted into a table using the INSERT ALL statement and by using the inserting the results of the select query.

What is insert append in Oracle?

During direct-path INSERT operations, Oracle appends the inserted data after existing data in the table. Data is written directly into datafiles, bypassing the buffer cache. Free space in the existing data is not reused, and referential integrity constraints are ignored.

Can we use top keyword in Oracle?

The SQL TOP clause is used to fetch a TOP N number or X percent records from a table. Note − All the databases do not support the TOP clause. For example MySQL supports the LIMIT clause to fetch limited number of records while Oracle uses the ROWNUM command to fetch a limited number of records.


2 Answers

INSERT INTO table1( id, name )
  SELECT max(id) + 1, 'name2'
    FROM table1

will be valid syntax. This method of generating id values, is a very poor approach. It does not work in a multi-user environment since many different sessions may get the same id value. It is also much less efficient than using a sequence.

like image 175
Justin Cave Avatar answered Nov 05 '22 10:11

Justin Cave


insert into table1(id,name) values ( (select max(id) from table1 ) + 1,'name2')
like image 39
Samy Louize Hanna Avatar answered Nov 05 '22 08:11

Samy Louize Hanna