I need help structuring a statement for the following scenario.
I am dealing with two databases that are linked. Ultimately, I creating an insert statement that will import two fields (equipmentId and clockId) into a reference table named EquipmentClock.
In order to obtain equipmentId, I have to reference a table named bEMEM which lives in another database. The bEMEM table contains a list of equipment and the jobs they are assigned to.
Originally, I was planning on setting the values in clockId to match job numbers, so my initial SQL statement was as follows:
INSERT INTO [CHSQL01\ABOUTTIME].[abtt].[dbo].[EquipmentClock] ([equipmentId],[clockId])
SELECT equipmentId, (LEFT(b.Job,5)) AS clockId
FROM [CHSQL01\ABOUTTIME].[abtt].[dbo].[Equipment] a
INNER JOIN VPTest.dbo.bEMEM b
ON a.[equipmentCode] COLLATE DATABASE_DEFAULT = b.Equipment COLLATE DATABASE_DEFAULT
WHERE b.Job IS NOT NULL
However, as I came to find out, its not that easy. It turns out that there's another ID that I must use for clockId, and it differs from what would normally be placed in the job field.
This has resulted in dragging a third table into the situation named Clock. In order to insert the correct clockId into the EquipmentClock table, I now must reference the Job field from the bEMEM table against a field named crewName in the Clock table, which will then return the correct clockId table for the insert.
I'm guessing this will result in some form of nested join, but I'm really having a hard time conceptualizing the statement. Any help is greatly appreciated.