From: Bruce Momjian <pgman@candle.pha.pa.us>
To: engineer@hlebprom.ru
Cc: pgsql-sql@postgresql.org
Subject: Re: 'AS' in 'DELETE/UPDATE'
Date: Tue, 29 Nov 2005 11:13:49 -0500 (EST)
Message-ID: <200511291613.jATGDnm08350@candle.pha.pa.us> (raw)
In-Reply-To: <902550887.20051129210156@hlebprom.ru>
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
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 using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: pgman@candle.pha.pa.us, engineer@hlebprom.ru
Subject: Re: 'AS' in 'DELETE/UPDATE'
In-Reply-To: <200511291613.jATGDnm08350@candle.pha.pa.us>
* 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