基于distinct,stuff,path查询更新行

时间:2016-06-29 08:16:37

标签: sql sql-server sql-server-2012

Combine multiple rows into one "memo" nvarchar(max)

继续
SELECT
    DISTINCT fileref, stuff(
        (
        Select char(13)+char(10) + IsNull( D2.viaccno, '' )
        From vi_accno As D2
        Where D2.vifileref = A.FileRef
        For Xml Path(''), type
        ).value('.', 'nvarchar(max)')
        , 1, 2, '') As accno
FROM
   vtindex a

如何使用上述查询更新表1上与fileref = fileref

匹配的每个accno行

表1: id,fileref,client,addr,accno

1 个答案:

答案 0 :(得分:1)

根据给定的数据,我可以为您提供:

UPDATE t
SET accno = p.accno 
FROM [table 1] t
INNER JOIN (
SELECT
    DISTINCT fileref, stuff(
        (
        Select char(13)+char(10) + IsNull( D2.viaccno, '' )
        From vi_accno As D2
        Where D2.vifileref = A.FileRef
        For Xml Path(''), type
        ).value('.', 'nvarchar(max)')
        , 1, 2, '') As accno
FROM
   vtindex a
) as p
ON p.fileref= t.fileref