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.
Multiple rows are inserted into a table using the INSERT ALL statement and by using the inserting the results of the select query.
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.
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.
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.
insert into table1(id,name) values ( (select max(id) from table1 ) + 1,'name2')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With