Re: how to determine OID of the row I just inserted???

From: evon600c <jcelle(at)deolis(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: how to determine OID of the row I just inserted???
Date: 2003-02-28 14:52:22
Message-ID: MPG.18c99613618a7008989681@news.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

hi, i think you should just use the pg_lastoid function available in C
or PHP.
in sql you can reference the last oid with :OID

In article <20030206140855(dot)A06E21CB1D9(at)koshin(dot)dsl(dot)systemec(dot)nl>,
jules(dot)alberts(at)arbodienst-limburg(dot)nl says...
> Hello everybody,
>
> Here's a question I have asked some time ago and Google tells me I'm
> not the only one with this problem, but I haven't found a solution yet
> :-(. I have a setup like this:
>
> customer {id serial, name varchar, address bigint}
> person {id serial, name varchar, address bigint}
> address {id serial, street varchar}
>
> customer.address points to address.id
> person.address points to address.id
>
> So, addresses are stored in a seperate table, customer.address and
> person.address should have a value that exists in address.id. When I
> add an address for customer X, I must do something like:
>
> insert into address (street) values ('Penny Lane');
> update customer set address = ??? where name = 'X';
>
> If I would do this in pgsql there would be no problem, because the OID
> is echoed when the update succeeds. But I use PHP or pl/pgsql (others
> have exactly the same problem with JDBC) and I know of no way to solve
> this. Something like lastval() IMHO is way too risky. I need something
> like a return value:
>
> catchOID = returnQueryOID('insert into address (street)
> values ('Penny Lane'));
> update customer set address = 'select id from address where
> oid = catchOID' where name = 'X';
>
> Sorry for any syntax errors in my examples (it's half semi code), I
> hope you understand the problem. If somebody knows a good solution, IMO
> this would be something to put in a FAQ, because exactly the same
> question arises when you do an insert on a table where the primary key
> is generated automatically and you want to show the result after the
> row is updated (which is very common).
>
> TIA for any help, I really need a solution...
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
> (send "unregister YourEmailAddressHere" to majordomo(at)postgresql(dot)org)
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Neil Conway 2003-02-28 15:08:46 Re: Locking rows
Previous Message Dennis Gearon 2003-02-28 14:51:45 views and multiple tables?