Hello I want to Update 1 table based on value present in other table. I could use Join to write the update statement and infact I have written it and it works using Join. But out of curiosity I want to use CTE. I wrote the following query but it doesn’t seem to work. Can anybody tell me what the issue is? Does CTE require Select statement at the end compulsorily? Why can I not write Update statement?
WITH cte(uid, col1)
As
(
Select uid, col1
From [User]
)
Update t2
Set col1 = cte.col1
Where uid = cte.uid
You still need to join back to your CTE, like so
Edit I guess you could avoid the ‘JOIN’ keyword by using old-style SQL WHERE joins, but this isn’t really good practice.
Also note that you can also update through the CTE, like views, as long as you only update one table.