Re: Simulating autoincrement in Oracle

From: Sybrand Bakker <postbus_at_sybrandb.demon.nl>
Date: 12 Apr 2002 00:25:08 -0700
Message-ID: <a20d28ee.0204112325.76b7dd17_at_posting.google.com>


xertroyt_at_yahoo.com (John) wrote in message news:<1ea65a.0204111804.7549c01c_at_posting.google.com>...
> I recently ported some code from DB2 (which has an autoincrement
> feature for column values) and needed to simulate that somehow in
> Oracle.
>
> Here's what I came up with:
>
> create or replace trigger user.table_autoincrement
> before insert on user.table
> for each row
> begin
> if :new.col is null then
> select user.table_s.nextval
> into :new.col
> from dual;
> end if;
> end table_autoincrement;
>
> This seems to work fine, but I was wondering if anyone sees any
> gotchas.
>
> John

Depends on whether how you have configured the sequence and whether you can allow 'holes' in your numbers. In that case use 'nocache order' for your sequence.
Other than that, this is the usual solution.

--
Regards

Sybrand Bakker,
Senior Oracle DBA
Received on Fri Apr 12 2002 - 09:25:08 CEST

Original text of this message