使用两个子集查询更新表列

时间:2017-03-29 11:19:37

标签: sql-server-2012

我正在使用一个查询,我必须更新一列,因为我有两个查询子集,如下所示:

select a.AuthorId as 'Source_Author',b.AuthorId as 'Target_Author' from (
select d.documentnodeid,AuthorId from wv_blogdata
inner join CMS_Document d on wv_blogdata.BlogDataID = d.DocumentForeignKeyValue
Where DocumentCulture ='en-US') a
INNER JOIN
(select d.documentnodeid,AuthorId from wv_blogdata
inner join CMS_Document d on wv_blogdata.BlogDataID = d.DocumentForeignKeyValue
Where DocumentCulture ='de-DE') b
ON a.documentnodeid =b.documentnodeid

使用此查询我的价值如下: sql result

现在我必须用源作者更新目标作者。

1 个答案:

答案 0 :(得分:1)

使用公用表表达式

;WITH cteUpdate
AS(
    select a.AuthorId as 'Source_Author',b.AuthorId as 'Target_Author' 
    FROM
    (
            SELECT d.documentnodeid,AuthorId from wv_blogdata
            INNER join CMS_Document d on wv_blogdata.BlogDataID = d.DocumentForeignKeyValue
            WHERE DocumentCulture ='en-US'
    ) a
    INNER JOIN
    (       SELECT d.documentnodeid,AuthorId from wv_blogdata
            INNER join CMS_Document d on wv_blogdata.BlogDataID = d.DocumentForeignKeyValue
            WHERE DocumentCulture ='de-DE'
    ) b
    ON a.documentnodeid =b.documentnodeid
)
UPDATE
    cteUpdate
SET
    cteUpdate.Target_Author = cteUpdate.Source_Author