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: The Wils <awislon_at_rci.rogers.com>
Date: 1997/05/08
Message-ID: <33724BCB.62A2@rci.rogers.com>#1/1

Steve McDaniels wrote:
>
> won't this do it?
>
> Query A: select max(rowid) from <table>;
>
> select <keyed field> from <table> where rowid = chartorowid(<rowid from
> query A>);
>
> > Hi,
> >
> > does anybody know how to get the primary key of the last record added to
> > a table? (ORACLE 7.2, SQL)
> >
> >

Only if the primary key always ascends. If you want to know the value of 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.
Received on Thu May 08 1997 - 00:00:00 CDT

Original text of this message

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