Re: re-using RETURNING

From: Emanuel Calvo Franco <postgres(dot)arg(at)gmail(dot)com>
To: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: re-using RETURNING
Date: 2009-11-12 14:07:15
Message-ID: f205bb120911120607m2d4622d2ib328a904dd7f2405@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

2009/11/12 A. Kretschmer <andreas(dot)kretschmer(at)schollglas(dot)com>:
> Hi,
>
> just to be sure, it is still (8.4) not possible to use RETURNING within an
> other INSERT?
>
> To show what i mean:
>
> test=*# insert into foo2 (id1, id2) (insert into foo(n) values ('a'),('b'),('c') returning currval('some_sequence'), id);
> ERROR:  syntax error at or near "insert"
> LINE 1: insert into foo2 (id1, id2) (insert into foo(n) values ('a')...
>
> I know, it is intended in 8.5 with the 'WITH' - clause, but not yet in 8.4, right?
>
> (and there are no other RDBMS which can do that?)

Well there is a little trik in actual versions...

use these options:
\a
\t
\o /tmp/archivo.csv

then
DELETE FROM tabla WHERE entero =13 RETURNING *;

(remember that you can throught this query from shell command line, with
psql options)

(next step, delete the last line of the file archivo.csv: DELETE 9890)

create a clon - void table (this will be your log table, if you have
already created this
, avoid this step):
postgres=# CREATE TABLE tabla_2 AS SELECT * FROM tabla WHERE 1=0;
SELECT

Then you can go with this
postgres=# COPY tabla_2 FROM '/tmp/returnes.csv' DELIMITER '|';
COPY 19780
postgres=# SELECT * from tabla_2 limit 1;
entero
--------
13
(1 row)

Obviusly, you can automatize these steps in a shell script.

Hope you enjoy it.

--
Emanuel Calvo Franco
DBA at: www.siu.edu.ar
www.emanuelcalvofranco.com.ar

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jérôme Etévé 2009-11-12 14:11:32 Re: What is the simpliest text search configuration?
Previous Message Scott Marlowe 2009-11-12 13:52:18 Re: DB Restart