我有一个脚本,它使用一堆 with 子句来得出一些结果,然后我想将该结果写入表中。我只是无法理解它,有人可以指出我正确的方向吗?
这是一个简化的示例,表明我想要做什么:
with comp as (
select *, 42 as ComputedValue from mytable where id = 1
)
update t
set SomeColumn = c.ComputedValue
from mytable t
inner join comp c on t.id = c.id
实际情况有相当多的 with 子句,它们都互相引用,因此任何实际使用 with 子句的建议都比将其重构为嵌套子查询更受青睐。
最佳答案
如果有人追随我来到这里,这就是对我有用的答案。
注意:请在使用之前阅读评论,这并不完整。 我可以给出的更新查询的最佳建议是切换到 SqlServer ;)
update mytable t
set z = (
with comp as (
select b.*, 42 as computed
from mytable t
where bs_id = 1
)
select c.computed
from comp c
where c.id = t.id
)
祝你好运
GJ
关于sql - 使用 with 子句更新语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5380559/