SQL Server查询-选择返回行,但更新0行

时间:2019-01-17 15:17:43

标签: sql sql-server

我正在尝试学习的SQL Server 2016(开发人员版)数据库中有一个小问题。我有一个查询,当我运行一个select时,它返回的行很好,但是当我尝试在类似的列结构上更新时,它返回0行,但我无法解决它并使它发疯:(

以下返回结果:

select DISTINCT R.PersonID, R.CompleteDate, E.ExactTime, U.RunCode
FROM Results R
JOIN Run PR ON PR.ID = R.RunID AND PR.ProgrammeID = 8
JOIN Event E ON E.ID = R.CompleteEventID
JOIN Unit U ON U.ID = R.UnitID
JOIN Batches BN ON R.PersonID = BN.ID

但是以下内容更新了0行:

UPDATE E
SET ExactTime = BN.CorrectTiming
FROM Event E, Results R, Run PR, Unit U, Batches BN
WHERE R.CompleteEventID = E.ID
AND  R.RunID = PR.ID 
AND PR.ProgrammeID = 8
AND R.UnitID = U.ID
AND R.PesonID = BN.ID
AND U.RunCode = BN.RunCode
AND R.CompleteDate = BN.CorrectDate

在我重新开始重新编写之前,任何人都可以检查我的语法并查看是否存在问题吗?

干杯!

1 个答案:

答案 0 :(得分:1)

如果您能提供一些数据样本,那就太好了。

也许尝试一下:

UPDATE E
SET E.ExactTime = BN.CorrectTiming
FROM Results R
INNER JOIN Run PR ON PR.ID = R.RunID
AND PR.ProgrammeID = 8
INNER JOIN Event E ON E.ID = R.CompleteEventID
INNER JOIN Unit U ON U.ID = R.UnitID
INNER JOIN Batches BN ON R.PersonID = BN.ID;