使用带有UPDATE的WITH..AS

时间:2014-01-21 11:05:18

标签: sql sql-server

虽然查询本身运行良好,但我无法进行存储过程编译的以下内容:

UPDATE t  
         SET   
            requestType= sp.requestType
      FROM #tempComp  AS t,
      (
          WITH CT AS
            (
                ........            )
            SELECT x, y, z
            FROM CT WHERE ab = 1
        )   AS sp  
      WHERE t.Id = sp.Id 

组织这个的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

  

组织这个的正确方法是什么?

根据the manual,CTE首先出现。

[ WITH <common_table_expression> [...n] ]
UPDATE ...

在你的情况下:

WITH CT AS
(
   ........
)
UPDATE t  
SET requestType = sp.requestType
FROM #tempComp AS t,
     ( SELECT x, y, z
       FROM CT
       WHERE ab = 1 ) AS sp  
WHERE t.Id = sp.Id