select id + 1
into v_id
from table
where id = (select max(id)
from table)
for update;
insert into table (id) values (v_id);
the select locks the table so no other user gets the same value.
the insert and subsequent commit releases the lock.
If you are doing 8.1.6 or higher you can use an anonymous transaction.
Quote:
Originally posted by hhiggs
Hi, I looking for code that will generate sequential values to populate a primary key column. I cannot use a sequence therefore it must be done programatically. There will be continuous inserts and deletes on this table and I want to be able to start the id column with 1 every time the table has been truncated. Any help would be greatly appreciated.
|