Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.server -> Re: Get the primary key of the last record added
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
![]() |
![]() |