带有连接/活动列的SQL Server更新

时间:2016-04-25 10:17:18

标签: sql-server

我认为这段代码更适合展示,我想做什么:

UPDATE A
SET A.[Changed] = 1
FROM [dbo].[Table1] A
INNER JOIN [Table2] E
        ON [A].[Part] = [E].[ActivePart]

UPDATE [dbo].[Table1]
    [Range] = [Range]
WHERE [Partgroup] = [Partgroup] AND [Changed] > 0

更新前:

enter image description here

更新后:

enter image description here

请帮忙。

非常感谢。

格尔茨 Vegeta_77

2 个答案:

答案 0 :(得分:0)

是的,第一部分看起来更好(即使我认为不需要WHERE,因为它涉及您加入的同一列)。然而,第二次更新不应该产生任何变化:

UPDATE [dbo].[Superliste_000]
SET [Plan-TGrp-Spanne_Stfl1] = [Plan-TGrp-Spanne_Stfl1],
    [Plan-TGrp-Spanne_Stfl2] = [Plan-TGrp-Spanne_Stfl2],
    [Plan-TGrp-Spanne_Stfl3] = [Plan-TGrp-Spanne_Stfl3],
    [Plan-TGrp-Spanne_Stfl4] = [Plan-TGrp-Spanne_Stfl4]
WHERE [Teilegruppe] = [Teilegruppe] AND [Date_change] > 0

再次使用[Plan-TGrp-Spanne_Stfl1]更新[Plan-TGrp-Spanne_Stfl1] - 这是相同的值...要么我太累了又误解了代码,或者你的更新中还有另一个小错误。 ; - )

答案 1 :(得分:0)

确定。这是解决方案:

UPDATE [Table1]
SET [Range] = (SELECT [Range] 
               FROM [Table1] A
               INNER JOIN [Table2] E
                   ON [A].[Part] = [E].[ActivePart])
WHERE [Partgroup] = (SELECT [Partgroup] 
                     FROM [Table1] B
                     INNER JOIN [Table2] F
                         ON [B].[Part] = [F].[ActivePart])

有时候,我认为要复杂化。 Thx Rene。