sql - PostgreSQL:how to update rows in CTE -


am running postgresql 9.2.
below given sample of huge , uglyquery

with cte as( select ....... atable  ),cte1 ( select ..... btable inner join cte using(anid) ),update_cte as( update cte set afield=(select somthing cte1) ) select * cte 


need create view final result.

while executing above getting error below.

error:  relation "cte" not exist 

i know doing bad.
hope can understand trying achieve above query.
so please suggest alternative method it.

replies appreciated.

note : the actual query

with cte as( select ....... atable ),update_cte as( update cte set afield=(select somthing cte1) ) 

you can't that.

an update may not reference cte term in postgresql, ctes materialized. aren't views on underlying data. (that's annoying, that's how is).

you can:

create temporary view someview select ... atable;  update someview set afield = ... 

if want; that'll work on newer postgresql versions support automatically updatable views. think 9.2 does.

otherwise, think want like:

with cte1 (   select ..... btable inner join cte using(anid) ) update atable  set afield=(select somthing cte1) ... clause cte ... returning *; 

but really, please don't call cte terms cte, cte1, etc. give them useful, descriptive names tell are. it's programs full of variables named a through x ... next person has maintain code, or ask help, not it.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -