Re: Strange PB !?

From: Wassim <net2000_at_francemel.com>
Date: Sun, 13 Jun 1999 23:19:01 GMT
Message-ID: <7k1e91$a9q$1_at_nnrp1.deja.com>


hi,
[Quoted] thanks for help but can you write some lines on how to implement a sequence in my pl/sql code?

In article <7k09qr$be3$1_at_news1.inlink.com>,   "Russ Whiteman" <russw_at_inlink.com> wrote:
>
> Steve Cosner wrote in message <7jveev$h7g$1_at_nntp.csufresno.edu>...
> >In article <7jv0j6$l84$1_at_nnrp1.deja.com>,
> >Wassim <net2000_at_francemel.com> wrote:
> >>I've in many of my PL/SQL procedures to create a new record in a
> table
> >>just with Key code = Last key + 1, (key_code is the table primary
> key)
> >>
> >>--------------------------------------------------------------------
> ----
> >>last_key_code := 0;
> >>begin
> >>select max(key_code) into last_key_code from myTable;
> >>exception when OTHERS then last_key_code:=0; -- when myTable is
> empty !!
> >>end;
> >>
> >>htp.print(last_key_code);
> >>
> >>insert into myTable values (last_key_code+1,...);
> >>--------------------------------------------------------------------
> ----
>
> >
> >Yes, a sequence is the accepted way to insert a new key value. But
> >your way would work, as well. It is just that when there are no rows
> >in your table, the max function returns null. It does not produce an
> >exception as you are trying to trap.
> >
>
> Actually, there are circumstances in a multi-user database where using
> "max(key_code) + 1" will NOT work, i.e. what happens when someone else
> inserts a record (during the fraction of a second) between your
> calculation and your insert? Your insert fails...
>
> A sequence is by far the best way to do this. There are other ways,
> but they either do no more than duplicate the functionality of a
> sequence, or they involve locking the table so nobody else can insert.
>
>

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't. Received on Mon Jun 14 1999 - 01:19:01 CEST

Original text of this message