开发者

How to update table when Email is on another table?

USE [ASPDryrun]
GO

  UPDATE [dbo].[Users]
    SET [ID] = [IU].[PersNo], 
        [C开发者_StackOverflowostCenter] = [CC].[CostCenterID], 
        [OrgUnit] = [OU].[OrgUnitID]
   FROM [dbo].[Users], 
        [dbo].[Import_UserData] AS [IU], 
        [dbo].[CostCenters] AS [CC], 
        [dbo].[OrgUnits] AS [OU]
  WHERE [CC].[ID] = [IU].[CostCtr]
    AND [OU].[ID] = [IU].[OrgUnit]
    AND [IU].[CorporateEmail] IN (SELECT [Email] 
                                    FROM [dbo].[Users])

GO

I want to update the info of some users when they have the email on the other table. But this query update all users and don't know what's wrong, can you help me? Thanks


Would explicit joins work better? Just guessing, not sure what data is actually in these... Like

USE [ASPDryrun]
GO

  UPDATE [dbo].[Users]
    SET [ID] = [IU].[PersNo], 
        [CostCenter] = [CC].[CostCenterID], 
        [OrgUnit] = [OU].[OrgUnitID]
   FROM [dbo].[Users], 
        join [dbo].[Import_UserData] AS [IU] on [IU].[CorporateEmail]=users.[email]
        join [dbo].[CostCenters] AS [CC] on [CC].[ID] = [IU].[CostCtr]
        join [dbo].[OrgUnits] AS [OU] on [OU].[ID] = [IU].[OrgUnit]


Finally I could fix the problem with only another condition more. Here is the result:

USE [ASPDryrun]
GO
        UPDATE [dbo].[Users]
        SET [ID] = [IU].[PersNo],
            [CostCenter] = [CC].[CostCenterID],
            [OrgUnit] = [OU].[OrgUnitID]
        FROM [dbo].[Import_UserData] AS [IU],
             [dbo].[CostCenters] AS [CC],
             [dbo].[OrgUnits] AS [OU],
             [dbo].[Users] AS [U]
        WHERE [CC].[EricssonID] = [IU].[CostCtr]
          AND [OU].[EricssonID] = [IU].[OrgUnit]
          AND [IU].[CorporateEmail] = [U].[Email]
        GO
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜