如何从Multikey表更新T-SQL中的多个列

时间:2013-04-02 09:09:26

标签: sql sql-server tsql join sql-update

我在从另一个表值更新Table时遇到问题。

假设我有:

TableA(keyA,keyB,fieldA,fieldB)

TableB(keyA,keyB,fieldA,fieldB,fieldC)

我想更新TableA中的表,这些行在表B中与TableB值进行比较。

我试过这种方式:

UPDATE TableA
SET FieldA = 
(
SELECT FieldA
FROM TableA W INNER JOIN TableB as V
    ON  W.keyA = V.keyA AND
        W.keyB = V.keyB
WHERE W.KeyA = V.KeyA AND
        W.keyB = V.KeyB AND 
) 
FROM TableA PK INNER JOIN TableB  AS I ON
TableA.keyA = TableB.keyA AND TableA.keyB = TableB.keyB

1 个答案:

答案 0 :(得分:4)

您的查询非常复杂。你可以进一步简单地进入这个,

UPDATE  a
SET     a.fieldA = b.fieldA,
        a.fieldB = b.fieldB
FROM    TableA a
        INNER JOIN TableB b
            ON  a.keyA = b.keyA AND
                a.keyB = b.keyB