psql variable from query result

From: Gabor Szokoli <szocske(at)gmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: psql variable from query result
Date: 2009-08-27 14:28:32
Message-ID: de47c0230908270728h26dbc7b4jfe7e5449a06a832e@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hi,

I am looking for a way to store the result of a server-side SQL query in a
psql client side variable, so I can use the value in other psql commands.
The simplest workaround I could think up requires a unique temporary file:

SELECT oid_field FROM someTable \g(:tempfile)
\set var `cat :tempfile`
\lo_export :var :destination

What I really want to do is to extract a file from a bash script on the
client side, but the OID must be looked up with a query first.
Is there any better way?

I even tried this, but did not work:

INSERT INTO tempTable (SELECT oid_field FROM someTable);
\lo_export(:LASTOID)

I guess the documentation should say LASTOID contains the oid of the last
large object inserted via /lo_insert.

I can of course just invoke psql twice or via expect, but I can't believe
I'm the first who tries to do this..

Any ideas apprechiated!

Gabor Szokoli

Browse pgsql-novice by date

  From Date Subject
Next Message Lennin Caro 2009-08-27 14:39:38 Re: compilation error
Previous Message Bob McConnell 2009-08-27 11:06:00 Re: how can I finish my suscription?