如何从CTE中为另一个select中的where子句选择数据

时间:2013-12-05 12:56:09

标签: sql sql-server sql-server-2008

让CTE像:

WITH atree as(SELECT [sort],[object_id],[object_type],[parent_id],[allowed],1 [lvl]
FROM @tbl 
WHERE [object_id] in (select object_id from @tbl where allowed = 1)
UNION ALL
SELECT tb.sort , tb.[object_id],tb.[object_type],tb.[parent_id],tb.[allowed], [lvl] + 1 [lvl]
FROM @tbl tb 
INNER JOIN atree at ON at.[parent_serno] = tb.[object_serno])

现在想要更新表,如:

UPDATE @tbl 
SET allowed = 1 where sort in (select distinct sort from atree)

但更新时出错。如何为CTE编写正确的更新?

0 个答案:

没有答案