pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
extend INSERT by 'INSERT INTO table FETCH ... FROM cursor' syntax
4+ messages / 3 participants
[nested] [flat]

* extend INSERT by 'INSERT INTO table FETCH ... FROM cursor' syntax
@ 2003-10-23 21:00 Alexander Vlasenko <intrnl_edu@ilyichevsk.odessa.ua>
  2003-10-27 16:39 ` Re: extend INSERT by 'INSERT INTO table FETCH ... FROM cursor' syntax Christoph Haller <ch@rodos.fzk.de>
  0 siblings, 1 reply; 4+ messages in thread

From: Alexander Vlasenko @ 2003-10-23 21:00 UTC (permalink / raw)
  To: pgsql-general@postgresql.org

Please CC me, I am not subscribed.

An imaginary SQL statement
INSERT INTO table FETCH ... FROM cursor;
looks almost the same as currently available
INSERT INTO table SELECT ...;

I tried it because I needed to insert a row in a table
after I DELETEd a set of rows, something like 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';
INSERT INTO stat FETCH ALL FROM total;
COMMIT;

but it does not work, chokes on FETCH ALL.

I want to sum up all the money by month, delete all the rows
(possibly thousands of them) and insert one row per client
with monthly totals.

Obviously I cannot swap order of INSERT and DELETE here.

I hesitate to post this to pgsql-hackers@postgresql.org,
do I have to? ;)

-- 
Alexander Vlasenko



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

* Re: extend INSERT by 'INSERT INTO table FETCH ... FROM cursor' syntax
  2003-10-23 21:00 extend INSERT by 'INSERT INTO table FETCH ... FROM cursor' syntax Alexander Vlasenko <intrnl_edu@ilyichevsk.odessa.ua>
@ 2003-10-27 16:39 ` Christoph Haller <ch@rodos.fzk.de>
  2003-10-27 19:35   ` Re: extend INSERT by 'INSERT INTO table FETCH ... FROM cursor' syntax Josh Berkus <josh@agliodbs.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Christoph Haller @ 2003-10-27 16:39 UTC (permalink / raw)
  To: intrnl_edu@ilyichevsk.odessa.ua; +Cc: pgsql-sql

> 
> Please CC me, I am not subscribed.
> 
> An imaginary SQL statement
> INSERT INTO table FETCH ... FROM cursor;
> looks almost the same as currently available
> INSERT INTO table SELECT ...;
> 
> I tried it because I needed to insert a row in a table
> after I DELETEd a set of rows, something like this:
> 
> 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;
> 
> but it does not work, chokes on FETCH ALL.
> 
> I want to sum up all the money by month, delete all the rows
> (possibly thousands of them) and insert one row per client
> with monthly totals.
> 
> Obviously I cannot swap order of INSERT and DELETE here.
> 
> I hesitate to post this to pgsql-hackers@postgresql.org,
> do I have to? ;)
> 
> --=20
> Alexander Vlasenko
> 
Using a temporary table to buffer the result comes to mind. 
Regards, Christoph 




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

* Re: extend INSERT by 'INSERT INTO table FETCH ... FROM cursor' syntax
  2003-10-23 21:00 extend INSERT by 'INSERT INTO table FETCH ... FROM cursor' syntax Alexander Vlasenko <intrnl_edu@ilyichevsk.odessa.ua>
  2003-10-27 16:39 ` Re: extend INSERT by 'INSERT INTO table FETCH ... FROM cursor' syntax Christoph Haller <ch@rodos.fzk.de>
@ 2003-10-27 19:35   ` Josh Berkus <josh@agliodbs.com>
  2003-10-27 19:54     ` Re: extend INSERT by 'INSERT INTO table FETCH ... FROM cursor' syntax Alexander Vlasenko <intrnl_edu@ilyichevsk.odessa.ua>
  0 siblings, 1 reply; 4+ messages in thread

From: Josh Berkus @ 2003-10-27 19:35 UTC (permalink / raw)
  To: Christoph Haller <ch@rodos.fzk.de>; intrnl_edu@ilyichevsk.odessa.ua; +Cc: pgsql-sql

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;
> > 
> > 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.

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.

-- 
-Josh Berkus
 Aglio Database Solutions
 San Francisco




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

* Re: extend INSERT by 'INSERT INTO table FETCH ... FROM cursor' syntax
  2003-10-23 21:00 extend INSERT by 'INSERT INTO table FETCH ... FROM cursor' syntax Alexander Vlasenko <intrnl_edu@ilyichevsk.odessa.ua>
  2003-10-27 16:39 ` Re: extend INSERT by 'INSERT INTO table FETCH ... FROM cursor' syntax Christoph Haller <ch@rodos.fzk.de>
  2003-10-27 19:35   ` Re: extend INSERT by 'INSERT INTO table FETCH ... FROM cursor' syntax Josh Berkus <josh@agliodbs.com>
@ 2003-10-27 19:54     ` Alexander Vlasenko <intrnl_edu@ilyichevsk.odessa.ua>
  0 siblings, 0 replies; 4+ messages in thread

From: Alexander Vlasenko @ 2003-10-27 19:54 UTC (permalink / raw)
  To: josh@agliodbs.com, Christoph Haller <ch@rodos.fzk.de>; +Cc: pgsql-sql

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



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


end of thread, other threads:[~2003-10-27 19:54 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-10-23 21:00 extend INSERT by 'INSERT INTO table FETCH ... FROM cursor' syntax Alexander Vlasenko <intrnl_edu@ilyichevsk.odessa.ua>
2003-10-27 16:39 ` Christoph Haller <ch@rodos.fzk.de>
2003-10-27 19:35   ` Josh Berkus <josh@agliodbs.com>
2003-10-27 19:54     ` Alexander Vlasenko <intrnl_edu@ilyichevsk.odessa.ua>

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