当电子邮件在另一个表上时如何更新表?

时间:2011-03-01 16:41:25

标签: sql sql-server sql-update

USE [ASPDryrun]
GO

  UPDATE [dbo].[Users]
    SET [ID] = [IU].[PersNo], 
        [CostCenter] = [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

我想更新一些用户在另一张桌子上收到电子邮件时的信息。但是这个查询更新了所有用户并且不知道什么是错的,你能帮助我吗?感谢

2 个答案:

答案 0 :(得分:0)

显式连接会更好吗?只是猜测,不确定这些数据究竟是什么......  喜欢

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]

答案 1 :(得分:0)

最后,我可以更多地解决另一个问题。结果如下:

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