pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How do I get the rec-id from an insert.
4+ messages / 3 participants
[nested] [flat]

* How do I get the rec-id from an insert.
@ 2002-08-28 03:40 Chris Ruprecht <chris@cdrbill.com>
  2002-08-29 21:00 ` Re: How do I get the rec-id from an insert. Clinton Adams <clinton@vote-smart.org>
  0 siblings, 1 reply; 4+ messages in thread

From: Chris Ruprecht @ 2002-08-28 03:40 UTC (permalink / raw)
  To: pgsql-php

Hi all,

I have 3 tables:

a:
a.recid
a.name

b:
b.recid
b.address
b.city

c:
c.recid
c.arecid
c.brecid


each has a default sequence on their respective recid - which is the primary 
index.

when I do an "insert into a ...", it automagically assigns recid a new, unique 
value, same goes for b.

But to create c, I need to know the recid from a and b, so I can connect the 
two.
Is there any way to get the recid from a and b after they have been inserted 
like in "insert into a ... and give me the recid you're going to use, back"?

I know, I can do a "netxval('...')" stuff and then insert the records with 
values in recid, but that requires two extra calls to the DB per transaction.

Best regards,
Chris



-- 
Chris Ruprecht
Network Grunt and bitpusher extraordinaire



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: How do I get the rec-id from an insert.
  2002-08-28 03:40 How do I get the rec-id from an insert. Chris Ruprecht <chris@cdrbill.com>
@ 2002-08-29 21:00 ` Clinton Adams <clinton@vote-smart.org>
  2002-08-29 22:58   ` Re: How do I get the rec-id from an insert. Bruno Wolff III <bruno@wolff.to>
  0 siblings, 1 reply; 4+ messages in thread

From: Clinton Adams @ 2002-08-29 21:00 UTC (permalink / raw)
  To: Chris Ruprecht <chris@cdrbill.com>; pgsql-php

Chris -

You don't really need to know the key values - make it one query:

insert into c values (nextval('crecid'), currval('arecid'), currval('brecid'))


On Tue August 27 2002 21:40, Chris Ruprecht graced us with the following -
> Hi all,
>
> I have 3 tables:
>
> a:
> a.recid
> a.name
>
> b:
> b.recid
> b.address
> b.city
>
> c:
> c.recid
> c.arecid
> c.brecid
>
>
> each has a default sequence on their respective recid - which is the
> primary index.
>
> when I do an "insert into a ...", it automagically assigns recid a new,
> unique value, same goes for b.
>
> But to create c, I need to know the recid from a and b, so I can connect
> the two.
> Is there any way to get the recid from a and b after they have been
> inserted like in "insert into a ... and give me the recid you're going to
> use, back"?
>
> I know, I can do a "netxval('...')" stuff and then insert the records with
> values in recid, but that requires two extra calls to the DB per
> transaction.
>
> Best regards,
> Chris




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: How do I get the rec-id from an insert.
  2002-08-28 03:40 How do I get the rec-id from an insert. Chris Ruprecht <chris@cdrbill.com>
  2002-08-29 21:00 ` Re: How do I get the rec-id from an insert. Clinton Adams <clinton@vote-smart.org>
@ 2002-08-29 22:58   ` Bruno Wolff III <bruno@wolff.to>
  2002-08-30 00:52     ` Re: How do I get the rec-id from an insert. Clinton Adams <clinton@vote-smart.org>
  0 siblings, 1 reply; 4+ messages in thread

From: Bruno Wolff III @ 2002-08-29 22:58 UTC (permalink / raw)
  To: Clinton Adams <clinton@vote-smart.org>; +Cc: Chris Ruprecht <chris@cdrbill.com>; pgsql-php

On Thu, Aug 29, 2002 at 15:00:15 -0600,
  Clinton Adams <clinton@vote-smart.org> wrote:
> Chris -
> 
> You don't really need to know the key values - make it one query:
> 
> insert into c values (nextval('crecid'), currval('arecid'), currval('brecid'))

Is it documented that the above is reliable? I don't remember ever reading
that expressions are evaluated in the order they appear in a select
statement.



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: How do I get the rec-id from an insert.
  2002-08-28 03:40 How do I get the rec-id from an insert. Chris Ruprecht <chris@cdrbill.com>
  2002-08-29 21:00 ` Re: How do I get the rec-id from an insert. Clinton Adams <clinton@vote-smart.org>
  2002-08-29 22:58   ` Re: How do I get the rec-id from an insert. Bruno Wolff III <bruno@wolff.to>
@ 2002-08-30 00:52     ` Clinton Adams <clinton@vote-smart.org>
  0 siblings, 0 replies; 4+ messages in thread

From: Clinton Adams @ 2002-08-30 00:52 UTC (permalink / raw)
  To: Bruno Wolff III <bruno@wolff.to>; +Cc: pgsql-php

Bruno -

Three sequences are involved in the INSERT. If one sequence were involved, 
then the expressions would be evaluated left to right, at least from what I 
can tell through my tests.  Not something I would ever use...or rely on, for 
that matter.


On Thu August 29 2002 16:58, Bruno Wolff III graced us with the following -
> > insert into c values (nextval('crecid'), currval('arecid'),
> > currval('brecid'))
>
> Is it documented that the above is reliable? I don't remember ever reading
> that expressions are evaluated in the order they appear in a select
> statement.







^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2002-08-30 00:52 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-08-28 03:40 How do I get the rec-id from an insert. Chris Ruprecht <chris@cdrbill.com>
2002-08-29 21:00 ` Clinton Adams <clinton@vote-smart.org>
2002-08-29 22:58   ` Bruno Wolff III <bruno@wolff.to>
2002-08-30 00:52     ` Clinton Adams <clinton@vote-smart.org>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox