Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> Re: Alternatives to Sequences?

Re: Alternatives to Sequences?

From: Chris Hamilton <ToneCzar_at_erols.com>
Date: Mon, 27 Sep 1999 17:59:08 -0400
Message-ID: <KOjvN+ZTpwmuZNwz8I=+gworeSFT@4ax.com>


On Mon, 27 Sep 1999 21:37:28 GMT, "Kevin P. Fleming" <kfleming_at_access-laserpress.com> wrote:

>You need to create your sequences, then using BEFORE INSERT triggers on each
>table to populate the primary key columns. The triggers are very simple:
>
>1) Table named FOO.
>2) Sequence named FOO_SEQUENCE.
>
>CREATE TRIGGER FOO_INSERT
>BEFORE INSERT ON TABLE FOO
>BEGIN
> :new.keycolumn := FOO_SEQUENCE.NEXTVAL;
>END FOO_INSERT;
Yes, the idea is correct but the code won't work - you can't reference a sequence in that manner. It has to be referenced via a select or in the insert clause of a statement.

Do this instead:

create or replace trigger foo_insert
  before insert on foo
  for each row

begin

  select foo_sequence.nextval
  into :new.keycolumn
  from dual;

end;

>
><cspeer_at_my-deja.com> wrote in message news:7somf8$tfk$1_at_nnrp1.deja.com...
>> Hello everyone,
>>
>> We purchased a software program that ties into an Oracle database.
>> We noticed that there are no primary/unique keys on the tables. We are
>> adding an auto-incrementing field... And herein the problem lies:
>>
>> In order to do an insert with an auto-incrementing field, you
>> would normally create a sequence and then reference it in your INSERT
>> INTO clause. However, we do not have that option since we cannot
>> rewrite the code of the software that creates the statement. (We have
>> dutifully cursed them and they are working on fixing it.)
>>
>> What alternatives do we have to do auto-incrementation. I heard
>> from the software company, that you can create a field-type that
>> increments in Oracle, but a) I don't belive it and b) I have not found
>> any reference to it anywhere in documentation.
>>
>> So is there a field type in Oracle (even non-standard) that I can
>> use to auto-increment?
>>
>> I am beginning to think that I will have to write a Trigger/Stored
>> Procedure that will accomplish this. (ie, on insert, update the auto-
>> incrementing field with max+1).
>>
>> References to articles would greatly help (especially if there is
>> a reference on writing the trigger.)
>>
>> Thank you!
>>
>> CW Speer
>> Oracle DBA (Newbie!)
>>
>>
>> Sent via Deja.com http://www.deja.com/
>> Before you buy.



Christopher Hamilton
Oracle DBA -- Sandbox.com
chamilton_at_sandbox-inc.com
http://www.sandbox.com/ Received on Mon Sep 27 1999 - 16:59:08 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US