使用join更新以同步具有条件的两个表

时间:2017-05-17 14:30:24

标签: sql-server join inner-join

数据库:SQL Server 2014

我有两个列相似的表。我需要使用来自TblSource的值更新TblTarget。我必须比较几个列值,因为另一个表没有ID。

选择查询可以提供我需要的内容:

SELECT 
    b.id
    a.mDate as mmDate, 
    b.mDate as wwDate, 
    b.Name, 
    b.Nr,
    b.address
FROM TblSource b 
    INNER JOIN TblTarget a
    ON  a.mDate = b.mDate AND
        a.Name = b.Name AND
        a.Nr = b.Nr AND
        a.address = b.address 
    WHERE   
        a.info = 'needs_more' AND
        a.data = 'kaput'

当我尝试更新时:

UPDATE a
    SET a.id = b.id,
        a.mDate = b.mDate,
        a.Name = b.Name,    
        a.Nr = b.Nr,
        a.address = b.address,
        a.Surname = b.Surname,
        a.note = 'updated'
    FROM TblTarget a 
    JOIN TblSource b 
        ON  a.mDate = b.mDate AND
            a.Name = b.Name AND
            a.Nr = b.Nr AND
            a.address = b.address
        WHERE 
            a.info = 'needs_more' AND
            a.data = 'kaput' AND 
            a.note <> 'updated'

结果是:(0)受影响的行

有什么想法吗?

0 个答案:

没有答案