X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 6D78D9DCCE0 for ; Tue, 29 Nov 2005 12:14:21 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 02778-01 for ; Tue, 29 Nov 2005 12:14:18 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from candle.pha.pa.us (candle.pha.pa.us [64.139.89.126]) by postgresql.org (Postfix) with ESMTP id 8002E9DCCCE for ; Tue, 29 Nov 2005 12:14:17 -0400 (AST) Received: (from pgman@localhost) by candle.pha.pa.us (8.11.6/8.11.6) id jATGDnm08350; Tue, 29 Nov 2005 11:13:49 -0500 (EST) From: Bruce Momjian Message-Id: <200511291613.jATGDnm08350@candle.pha.pa.us> Subject: Re: 'AS' in 'DELETE/UPDATE' In-Reply-To: <902550887.20051129210156@hlebprom.ru> To: engineer@hlebprom.ru Date: Tue, 29 Nov 2005 11:13:49 -0500 (EST) CC: pgsql-sql@postgresql.org X-Mailer: ELM [version 2.4ME+ PL121 (25)] MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.03 required=5 tests=[AWL=0.030] X-Spam-Score: 0.03 X-Spam-Level: X-Archive-Number: 200511/276 X-Sequence-Number: 23527 TODO has: o Allow an alias to be provided for the target table in UPDATE/DELETE This is not SQL-spec but many DBMSs allow it. so we want to add this capability some day. --------------------------------------------------------------------------- engineer@hlebprom.ru wrote: > Hi. > > Maybe I miss something but I can't use 'AS' with 'DELETE' (7.4.3) > Example: > > db=# SELECT * FROM temp1 ; > host_id | user_id | raw | uniq > ---------+---------+-----+------ > 2 | 1 | 125 | 85 > 2 | 2 | 100 | 50 > (2 rows) > > And there is temp2 just like temp1. > > db=# DELETE FROM temp1 AS t1 WHERE EXISTS (SELECT 1 FROM temp2 AS t2 > WHERE t2.host_id = t1.host_id AND t2.user_id = t1.user_id); > ERROR: syntax error at or near "AS" at character 19 > > but > > db=# DELETE FROM temp1 WHERE EXISTS (SELECT 1 FROM temp2 AS t2 WHERE > t2.host_id = temp1.host_id AND t2.user_id = temp1.user_id); > DELETE 1 > db=# SELECT * FROM temp1 ; > host_id | user_id | raw | uniq > ---------+---------+-----+------ > 2 | 1 | 125 | 85 > (1 row) > > It make me supply full name of table... > > > Another example with UPDATE > > db=# UPDATE referer_total AS ref SET ref.raw = ref.raw + u.raw, > ref.uniq = ref.uniq + u.uniq FROM temp1 AS u > WHERE u.user_id = ref.user_id AND ref.referer = u.referer; > ERROR: syntax error at or near "AS" at character 22 > > db=# UPDATE referer_total SET > db-# referer_total.raw = referer_total.raw + u.raw, > db-# referer_total.uniq = referer_total.uniq + u.uniq > db-# FROM temp1 AS u WHERE u.user_id = referer_total.user_id > db-# AND referer_total.referer = u.referer; > ERROR: syntax error at or near "." at character 46 > > So it make me rename temp1's "raw" to something else ("r"), "uniq" > too; and 'AS' not possible too. And finally, working version: > > UPDATE referer_total SET raw = raw + r, uniq = uniq + u > FROM temp1 AS u WHERE > u.user_id = referer_total.user_id AND > referer_total.referer = u.referer; > > > It looks strange, are there any limitations or something else that > make it not possible to use 'AS' in 'DELETE' and 'UPDATE'? Of course > with described workarounds I can eliminate that problems, but I want > to know is it so in 8.x? Or why, if it right behaviour? > > -- > engineer > > > ---------------------------(end of broadcast)--------------------------- > TIP 6: explain analyze is your friend > -- Bruce Momjian | http://candle.pha.pa.us pgman@candle.pha.pa.us | (610) 359-1001 + If your life is a hard drive, | 13 Roberts Road + Christ can be your backup. | Newtown Square, Pennsylvania 19073