访问操作必须使用可更新的查询

时间:2014-06-22 01:31:46

标签: sql sql-update ms-access-2013

我一直绞尽脑汁试图让它整天都在工作,它几乎就在那里,但我得到了可怕的“操作必须使用可更新的查询”。经过多次更改和重写后,我最终得到以下内容:

UPDATE
    Users AS U
  INNER JOIN
    (SELECT
        UP.[Agent ID] AS AgentID,
        UP.[Last Name] AS LastName,
        UP.[First Name] AS FirstName,
        UP.[Team ID] AS TeamID,
        UP.[Security Profile ID] AS SecProfID,
        E.EmailID,
        UP.[Username] AS [Username],
        IIF(UP.Status = 'Active', 1,0) AS [Status],
        L.LocationID,
        Nz(DD.DateID, 1) AS HireDate,
        IIF(UP.[Rehire Status] = 'Eligible', 1, 0) AS RehireEligible,
        (SELECT 
             D.DateID 
         FROM 
             DateCodes AS D 
         WHERE
             D.DateValue = Format(Now(), "Short Date")) AS ModifiedDate
    FROM
        ((UsersPaste AS UP 
        LEFT JOIN
          DateCodes AS DD
        ON UP.[Hire Date] = DD.DateValue)
       INNER JOIN
         EmailAddresses AS E
       ON UP.[Email Address] = E.EmailAddress)
      INNER JOIN
         (SELECT 
              L.LocationID, T.TimeZone, L.Country, L.State, L.City 
          FROM 
              Locations AS L 
            INNER JOIN 
              TimeZones AS T 
            ON L.TimeZoneID = T.TimeZoneID) AS LL
      ON UP.[Time Zone] = LL.TimeZone 
        AND UP.Country = LL.Country 
        AND UP.State = LL.State 
        AND UP.City = LL.City) AS UU
  ON U.AgentID = UU.AgentID 
SET
    U.LastName = UU.LastName,
    U.FirstName = UU.FirstName,
    U.TeamID = UU.TeamID,
    U.SecProfID = UU.SecProfID,
    U.EmailID = UU.EmailID,
    U.[Username] = UU.[Username],
    U.[Status] = UU.[Status],
    U.LocationID = UU.LocationID,
    U.HireDate = UU.HireDate,
    U.RehireEligible = UU.RehireEligible,
    U.ModifiedDate = UU.ModifiedDate

现在,形成UU的内部Select查询确切地输出了Users表的设置方式。在我看来,如果我错了,请纠正我,这应该与我直接加入另一张桌子的方式完全相同。

我是否需要创建一个实际的表,并将UU形成的记录插入该表中,然后根据新表进行更新?如果必须,我会,但我宁愿不这样做。任何人都知道任何其他方式或我需要做些什么才能让它发挥作用?

1 个答案:

答案 0 :(得分:0)

在您的情况下,是的,我认为您需要将子查询转换为生成表查询

SELECT ... INTO UU FROM ...

然后将您的UPDATE查询作为

UPDATE Users AS U INNER JOIN UU ON ...

在简单的情况下,可以通过使用域聚合功能来避免“可更新查询”问题。例如,以下查询拒绝运行(“操作必须使用可更新的查询。”)

UPDATE
    Donors
    INNER JOIN
    (
        SELECT 
            DonorID,
            SUM(Amount) AS SumOfAmount
        FROM Donations
        GROUP BY DonorID
    ) AS totals
        ON Donors.ID = totals.DonorID
SET
    Donors.TotalAmount = totals.SumOfAmount

但以下查询有效

UPDATE Donors
SET Donors.TotalAmount = DSum("Amount", "Donations", "DonorID=" & ID)

但是,您的情况非常复杂,使用临时表可能是最好的方法。