1

我正在尝试更新表中的记录,这需要我使用 INNER JOIN 来查找正确的记录。

   SELECT T2.*
   FROM Schema.Table2 T2 INNER JOIN 
     Schema.Table1 T1 ON T2.Key_Column = T1.Key_Column
   WHERE T2.ID = @ID AND T1.Code = '1'

试过了

UPDATE Schema.Table2 T2
SET T2.col1 = @Var1,
    T2.col2 = @Var2,
    T2.col3 = @Var3
WHERE T2.Key_Column = (SELECT T2.*
                       FROM Schema.Table2 T2 INNER JOIN 
                       Schema.Table1 T1 ON T2.Key_Column = T1.Key_Column
                       WHERE T2.ID = @ID AND T1.Code = '1')

请帮忙!

4

2 回答 2

4

您不需要子选择:

UPDATE T2
SET T2.col1 = @Var1,
    T2.col2 = @Var2,
    T2.col3 = @Var3
FROM Schema.Table2 T2 INNER JOIN 
     Schema.Table1 T1 ON T2.Key_Column = T1.Key_Column
WHERE T2.ID = @ID AND T1.Code = '1'
于 2012-12-21T17:05:08.670 回答
3

怎么样

UPDATE Schema.Table2
        SET
            col1 = @Var1,
            col2 = @Var2,
            col3 = @Var3
    FROM
            Schema.Table2 T2
        INNER JOIN 
            Schema.Table1 T1 
                ON T2.Key_Column = T1.Key_Column
    WHERE 
            T2.ID = @ID 
        AND 
            T1.Code = '1'
于 2012-12-21T17:07:51.937 回答