Storing sequence numbers for later use

From: nolan(at)celery(dot)tssi(dot)com
To: pgsql-general(at)postgresql(dot)org (pgsql general list)
Subject: Storing sequence numbers for later use
Date: 2003-04-18 15:31:16
Message-ID: 20030418153116.8839.qmail@celery.tssi.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> It is possible to assign the result of an function call to a script
> local variable in psql ?

It is kind of ugly, but try this:

> INSERT INTO Address (city) values ('Berlin');

\set ADD1 = :LASTOID

> INSERT INTO Address (city) values ('Paris');

\set ADD2 = :LASTOID

>Insert into Address select
> INSERT INTO Invoice (payeeAdress, invoiceeAdress, grossTotal) values
> (pa_id, ia_id, 100.0);

Now you can write your insert as follows:

INSERT INTO Invoice (payeeAdress, invoiceeAdress, grossTotal)
SELECT A.ID, B.ID, 100.0 from Address AS A, Address AS B
where A.OID = :ADD1 and B.OID = :ADD2;

For performance reasons, you will probably need to build this index:

CREATE INDEX add_oids on Address(OID);
--
Mike Nolan

Browse pgsql-general by date

  From Date Subject
Next Message Dennis Gearon 2003-04-18 15:41:41 Re: default locale considered harmful? (was Re: Using
Previous Message Ken Williams 2003-04-18 15:12:40 Re: Weird "template1" errors on CREATE DATABASE