sql - How do I write this CTE query? -


hello want update 1 table based on value present in other table. use join write update statement , infact have written , works using join. out of curiosity want use cte. wrote following query doesn't seem work. can tell me issue is? cte require select statement @ end compulsorily? why can not write update statement?

with cte(uid, col1) (     select uid, col1         [user] ) update t2     set col1 = cte.col1         uid = cte.uid 

you still need join cte, so

with cte([uid], col1) (     select [uid], col1         [user] ) update t2     set col1 = cte.col1     t2 inner join cte cte      on t2.[uid] = cte.[uid] 

edit guess avoid 'join' keyword using old-style sql joins, isn't practice.

...         t2, cte cte  t2.[uid] = cte.[uid] 

also note can update through cte, views, long update 1 table.

with cte(usercol1, t2col1) (     select [u].col1 usercol1, t2.col1 t2col1         [user] u         inner join t2          on u.[uid] = t2.[uid] ) update cte     set t2col1 = usercol1 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -