We help IT Professionals succeed at work.
Get Started

Multiple Joins

374 Views
Last Modified: 2012-05-12
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

Open in new window


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.
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE