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: Get the primary key of the last record added

Re: Get the primary key of the last record added

From: Allen Kirby <akirby_at_att.com>
Date: 1997/05/09
Message-ID: <33731CD4.5705@att.com>#1/1

The Wils wrote:
This is a good idea, but has one major drawback: you've single threaded the inserts into table1 because all inserts must update the same row in pk_value (key is table_name). Now in some cases this may be ok, but if you have a lot of inserts it may not work as well.
<snip>
> Only if the primary key always ascends. If you want to know the value of

Actually, the primary key doesn't have to ascend. You just have to have another column that is updated at insert time that does ascend and select using that.
> any key added, try the following; check manual for syntax.
>
> create table pk_value(
> table_name varchar2(30),
> primary_ky varchar2(??));
>
> create trigger tr_table1 before insert on table1
> which updates pk_value with table1 and the primary key value
> (or inserts a record if one doesn't exist already)
>
> then to get the last pk value inserted,
> select primary_key from pk_value where table_name = 'TABLE1';
>
> Then all you have to do is figure out which is the 'last' record added
> in the case of simultaneous updates, assuming you have more than 1 user.
>
> Alan Wilson
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> The man who follows the crowd will usually get no further than the
> crowd. The man who walks alone is likely to find himself in places no
> one has ever been.
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

-- 
---
Allen Kirby			AT&T ITS Production Services
akirby_at_att.com			Alpharetta, GA.
Received on Fri May 09 1997 - 00:00:00 CDT

Original text of this message

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