X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id B7D65D1B4EB for ; Mon, 27 Oct 2003 19:54:43 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 17876-03 for ; Mon, 27 Oct 2003 15:54:14 -0400 (AST) Received: from 127.0.0.1 (unknown [195.66.192.168]) by svr1.postgresql.org (Postfix) with SMTP id 22BC5D1B56F for ; Mon, 27 Oct 2003 15:54:10 -0400 (AST) Received: (qmail 5183 invoked by uid 0); 27 Oct 2003 19:54:06 -0000 Received: from unknown (HELO shadow) (1.1.1.2) by 0 with SMTP; 27 Oct 2003 19:54:06 -0000 Content-Type: text/plain; charset="windows-1251" From: Alexander Vlasenko Reply-To: intrnl_edu@ilyichevsk.odessa.ua Organization: ND Center To: josh@agliodbs.com, Christoph Haller Subject: Re: extend INSERT by 'INSERT INTO table FETCH ... FROM cursor' syntax Date: Mon, 27 Oct 2003 21:54:05 +0200 X-Mailer: KMail [version 1.4] Cc: pgsql-sql@postgresql.org References: <200310271539.QAA10042@rodos> <200310271135.59460.josh@agliodbs.com> In-Reply-To: <200310271135.59460.josh@agliodbs.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200310272154.05356.intrnl_edu@ilyichevsk.odessa.ua> X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200310/352 X-Sequence-Number: 15752 On Monday 27 October 2003 21:35, Josh Berkus wrote: > Alexander, > > > > BEGIN; > > > DECLARE total CURSOR > > > FOR SELECT=3D20 > > > SUBSTR(datetime,1,7)||'-01 00:00:00' as month, > > > client, > > > SUM(money) > > > FROM stat > > > WHERE SUBSTR(datetime,1,7)=3D3D'2003-10' > > > GROUP BY month,client; > > > DELETE FROM stat WHERE SUBSTR(datetime,1,7)=3D3D'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 ne= ed > 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)=3D'2003-10' GROUP BY month,client; DELETE FROM stat WHERE SUBSTR(datetime,1,7)=3D'2003-10'; FETCH ALL FROM total; <=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D COMMIT; and it does work as expected. FETCH spews out already deleted rows. There is no problem with it. --=20 Alexander Vlasenko