pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alexander Vlasenko <intrnl_edu@ilyichevsk.odessa.ua>
To: josh@agliodbs.com, Christoph Haller <ch@rodos.fzk.de>
Cc: pgsql-sql@postgresql.org
Subject: Re: extend INSERT by 'INSERT INTO table FETCH ... FROM cursor' syntax
Date: Mon, 27 Oct 2003 21:54:05 +0200
Message-ID: <200310272154.05356.intrnl_edu@ilyichevsk.odessa.ua> (raw)
In-Reply-To: <200310271135.59460.josh@agliodbs.com>
References: <200310271539.QAA10042@rodos>
	<200310271135.59460.josh@agliodbs.com>

On Monday 27 October 2003 21:35, Josh Berkus wrote:
> Alexander,
>
> > > BEGIN;
> > > DECLARE total CURSOR
> > >  FOR SELECT=20
> > >   SUBSTR(datetime,1,7)||'-01 00:00:00' as month,
> > >   client,
> > >   SUM(money)
> > >  FROM stat
> > >  WHERE SUBSTR(datetime,1,7)=3D'2003-10'
> > >  GROUP BY month,client;
> > > DELETE FROM stat WHERE SUBSTR(datetime,1,7)=3D'2003-10';
> > > INSERT INTO stat FETCH ALL FROM total;
> > > COMMIT;

[ BTW: quoted-printable is evil ;) ]

> > >
> > > but it does not work, chokes on FETCH ALL.
>
> Well, there's two problems with your program:
>
> 1) INSERT INTO .... FETCH ALL is not currently implemented.  You would need
> to use a loop, and insert one row at a time by value.

Exactly. I was saying that if implemented it may be useful.
My example is certainly doable without it but it quickly gets ugly
since I can't use this nifty trick.

> 2) You can't insert the rows you've just deleted from the base tables.  In
> your example, the TOTAL cursor would be empty.   I think that what you
> really want is a temp table.

Why do you think it would be empty? It is not. I tried this:

BEGIN;
DECLARE total CURSOR
 FOR SELECT
  SUBSTR(datetime,1,7)||'-01 00:00:00' as month,
  client,
  SUM(money)
 FROM stat
 WHERE SUBSTR(datetime,1,7)='2003-10'
 GROUP BY month,client;
DELETE FROM stat WHERE SUBSTR(datetime,1,7)='2003-10';
FETCH ALL FROM total;   <===================================
COMMIT;

and it does work as expected. FETCH spews out already deleted rows.
There is no problem with it.
-- 
Alexander Vlasenko



view thread (4+ messages)

Message-ID: <200310272154.05356.intrnl_edu@ilyichevsk.odessa.ua>
Permalink:  ../200310272154.05356.intrnl_edu@ilyichevsk.odessa.ua/
Also on:    postgresql.org/message-id/200310272154.05356.intrnl_edu@ilyichevsk.odessa.ua

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: intrnl_edu@ilyichevsk.odessa.ua, ch@rodos.fzk.de
  Subject: Re: extend INSERT by 'INSERT INTO table FETCH ... FROM cursor' syntax
  In-Reply-To: <200310272154.05356.intrnl_edu@ilyichevsk.odessa.ua>
  References: <200310271539.QAA10042@rodos> <200310271135.59460.josh@agliodbs.com> <200310272154.05356.intrnl_edu@ilyichevsk.odessa.ua>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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