我正在尝试使用另一个数据库中另一个表的信息更新一个数据库中的表。最重要的是,我需要第三个表来建立两者之间的联系。这就是我所拥有的:
Update
MT.dbo.[tblPatientEmployers]
Set
MT.dbo.[tblPatientEmployers].[PatientOccupation] = RTRIM(LTRIM(T.Field3)),
MT.dbo.[tblPatientEmployers].[PatientEmploymentStatusCode] = RTRIM(LTRIM(T.Field4)),
MT.dbo.[tblPatientEmployers].[PatientEmploymentFromDate] = RTRIM(LTRIM(T.Field5)),
MT.dbo.[tblPatientEmployers].[PatientEmploymentThruDate] = RTRIM(LTRIM(T.Field6)),
MT.dbo.[tblPatientEmployers].[PatientEmployerPhoneNumber] = RTRIM(LTRIM(T.Field7)),
MT.dbo.[tblPatientEmployers].[PatientNationalEmployerID] = RTRIM(LTRIM(T.Field8)),
MT.dbo.[tblPatientEmployers].[PatientEmployerName] = RTRIM(LTRIM(T.Field9)),
MT.dbo.[tblPatientEmployers].[PatientEmployerAddress1] = RTRIM(LTRIM(T.Field11)),
MT.dbo.[tblPatientEmployers].[PatientEmployerAddress2] = RTRIM(LTRIM(T.Field12)),
MT.dbo.[tblPatientEmployers].[PatientEmployerCity] = RTRIM(LTRIM(T.Field13)),
MT.dbo.[tblPatientEmployers].[PatientEmployerStateCode] = RTRIM(LTRIM(T.Field15)),
MT.dbo.[tblPatientEmployers].[PatientEmployerZipCode] = RTRIM(LTRIM(T.Field16))
from [MT_Work].[dbo].[impHL7Xref] as T
inner join [MT_Work].[dbo].[impHL7Xref]
ON MT.dbo.tblPatients.[MedicalRecordNumber] = [MT_Work].[dbo].[impHL7Xref].MRN
WHERE T.RecordType = 'PATEMP'
导致错误消息: Msg 4104,Level 16,State 1,Line 20 多部分标识符" MT.dbo.tblPatients.MedicalRecordNumber"无法受约束。
答案 0 :(得分:0)
您的问题是您从未告诉过SQL Server您的表格是如何相关的。您需要FROM子句中的每个表才能使其工作:下次编写UPDATE语句时,首先尝试编写一个SELECT语句,该语句将提取所需的所有值并显示它们。这有助于确保您拥有正确的连接。
假设我正在读你的条件,你正在寻找以下内容:
UPDATE PE
SET
PE.[PatientOccupation] = RTRIM(LTRIM(T.Field3)),
PE.[PatientEmploymentStatusCode] = RTRIM(LTRIM(T.Field4)),
PE.[PatientEmploymentFromDate] = RTRIM(LTRIM(T.Field5)),
PE.[PatientEmploymentThruDate] = RTRIM(LTRIM(T.Field6)),
PE.[PatientEmployerPhoneNumber] = RTRIM(LTRIM(T.Field7)),
PE.[PatientNationalEmployerID] = RTRIM(LTRIM(T.Field8)),
PE.[PatientEmployerName] = RTRIM(LTRIM(T.Field9)),
PE.[PatientEmployerAddress1] = RTRIM(LTRIM(T.Field11)),
PE.[PatientEmployerAddress2] = RTRIM(LTRIM(T.Field12)),
PE.[PatientEmployerCity] = RTRIM(LTRIM(T.Field13)),
PE.[PatientEmployerStateCode] = RTRIM(LTRIM(T.Field15)),
PE.[PatientEmployerZipCode] = RTRIM(LTRIM(T.Field16))
FROM
[MT_Work].[dbo].[impHL7Xref] T
INNER JOIN
MT.dbo.tblPatients P ON
T.MRN = P.MedicalRecordNumber
INNER JOIN
MT.dbo.tblPatientEmployers PE ON
P.ID = PE.PatientID
WHERE T.RecordType = 'PATEMP'
请注意其工作方式:我们很高兴为您提供Stack Overflow,但新用户可以做的最好的事情是learn how to ask a good question。在您的情况下,您包括SQL(这很棒),但没有包含有关三个表之间的关系以及您尝试完成的内容的详细信息。