I am trying to add a key from one table to another so that I can create a one to many relationship. The table I would like to add a foreign key to currently has the employee’s first name and last name, but not ID. I would like to query the employee table to find the ID based on their first name and last name, and then add it to the injury table.
I Keep getting the following errors:
The multi-part identifier "InjuryOLD.FirstName" could not be bound.
The multi-part identifier "dbo.InjuryOLD.LastName" could not be bound.
Below is the SQL Statement:
Insert into dbo.InjuryOLD(dbo.InjuryOLD.EmpID)
Select dbo.EmployeeInformation.EmpID
From EmployeeInformation
Where exists(select dbo.EmployeeInformation.EmpID from dbo.EmployeeInformation
where dbo.EmployeeInformation.FirstName = dbo.InjuryOLD.FirstName
and dbo.EmployeeInformation.LastName = dbo.InjuryOLD.LastName)
I have ensured that the tables and columns that I am referencing have no typo’s, and I know that there is a chance that two employee could have the same name. However that is not the case in this scenario.
The
SELECTpart anINSERTstatement should always work on its ownso…
…doesn’t work because
dbo.InjuryOLDisn’t referenced in either FROM clause. You probably meant to put thedbo.InjuryOLDin the from clause of the Exists sub query instead of repeatingEmployeeInformation