From: Steven Dahlin <pgdb.sldahlin@gmail.com>
To: pgsql-sql@postgresql.org
Subject: update with recursive query
Date: Thu, 14 Apr 2011 15:33:16 -0600
Message-ID: <BANLkTi=qq_OCFSPpJ-HvyZGXTc-eA-5e6Q@mail.gmail.com> (raw)
Is it possible to execute an update using recursion? I need to update a set
of records and also update their children with the same value. I tried the
following query but it gave an error at the "update schema.table tbl":
with recursive childTbl( pid,
ppid,
proc_id,
other_id )
as ( select prc.pid,
prc.ppid,
prc.proc_id,
prc.other_id
from my_schema.prc_tbl prc
where ( ( prc.proc_path like '%stuff%' )
or ( prc.proc_parameters like '%stuff%' ) )
and ( prc.other_id is null )
union all
select prcsub.pid,
prcsub.ppid,
prcsub.proc_id,
prcsub.other_id
from childTbl prcpar,
my_schema.prc_tbl prcsub
where ( prcsub.ppid = prcpar.pid )
)
update my_schema.prc_tbl prc
set other_id = 101
from childTbl
However, if I do a "select * from childTbl" it works. The docs take about
updates and talk about recursive queries with selects but nothing seems to
cover the joining of the two.
Thanks
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: pgdb.sldahlin@gmail.com
Subject: Re: update with recursive query
In-Reply-To: <BANLkTi=qq_OCFSPpJ-HvyZGXTc-eA-5e6Q@mail.gmail.com>
* 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