仅当记录匹配时更新一个 table 中的记录
Update records in one table from another only if records match
我希望用 Patientdemographics2 中名为 custom 的列更新 Patientdemographics 中名为 custom 的列,但前提是 FirstName LastName 和 DateofBirth 列在两个表中都匹配。
Update PatientDemographics
Set PatientDemographics.custom = PatientDemographics2.custom
FROM PatientDemographics INNER JOIN
PatientDemographics2 ON
Patientdemographics.FirstName = Patientdemographics2.FirstName and
Patientdemographics.LastName = Patientdemographics2.LastName and
Patientdemographics.DateofBirth = Patientdemographics.DateofBirth
where Patientdemographics.FirstName = Patientdemographics2.FirstName and
Patientdemographics.LastName = Patientdemographics2.LastName and
Patientdemographics.DateofBirth = Patientdemographics.DateofBirth
您在 ON
子句的最后一个条件中有错字:
Patientdemographics.DateofBirth = Patientdemographics.DateofBirth
应该是:
Patientdemographics.DateofBirth = Patientdemographics2.DateofBirth
还有一个 useless WHERE
子句,因为它的所有条件都已经应用在 ON
子句中。
也使用别名使代码更简单和更易读:
Update p
Set p.custom = p2.custom
FROM PatientDemographics AS p INNER JOIN PatientDemographics2 AS p2
ON
p.FirstName = p2.FirstName and
p.LastName = p2.LastName and
p.DateofBirth = p2.DateofBirth
我希望用 Patientdemographics2 中名为 custom 的列更新 Patientdemographics 中名为 custom 的列,但前提是 FirstName LastName 和 DateofBirth 列在两个表中都匹配。
Update PatientDemographics
Set PatientDemographics.custom = PatientDemographics2.custom
FROM PatientDemographics INNER JOIN
PatientDemographics2 ON
Patientdemographics.FirstName = Patientdemographics2.FirstName and
Patientdemographics.LastName = Patientdemographics2.LastName and
Patientdemographics.DateofBirth = Patientdemographics.DateofBirth
where Patientdemographics.FirstName = Patientdemographics2.FirstName and
Patientdemographics.LastName = Patientdemographics2.LastName and
Patientdemographics.DateofBirth = Patientdemographics.DateofBirth
您在 ON
子句的最后一个条件中有错字:
Patientdemographics.DateofBirth = Patientdemographics.DateofBirth
应该是:
Patientdemographics.DateofBirth = Patientdemographics2.DateofBirth
还有一个 useless WHERE
子句,因为它的所有条件都已经应用在 ON
子句中。
也使用别名使代码更简单和更易读:
Update p
Set p.custom = p2.custom
FROM PatientDemographics AS p INNER JOIN PatientDemographics2 AS p2
ON
p.FirstName = p2.FirstName and
p.LastName = p2.LastName and
p.DateofBirth = p2.DateofBirth