January 23, 2014 at 2:32 pm
I thought it didn't matter what order the JOIN statements are in a query, but if I join TABLE_B before Table_C, I get an error. If I join Table_B last, I get results. What am I missing ?
SELECTdistinct
Col1, col2, col3, col4
from InputTable EAP
join DB..Table_A M on MT_GoToMeetingID = WebinarID
join DB..Table_B Mbr on MEP.Mem_ID = Mbr.MEM_ID
join DB..Table_C MEP on email = MEP.memepf_email1
--join DB..Table_B Mbr on MEP.Mem_ID = Mbr.MEM_ID 'B' commented out
'The multi-part identifier "MEP.Mem_ID" could not be bound.'
SELECTdistinct
Col1, col2, col3, col4
from InputTable EAP
join DB..Table_A M on MT_GoToMeetingID = WebinarID
--join DB..Table_B Mbr on MEP.Mem_ID = Mbr.MEM_ID 'B' commented out
join DB..Table_C MEP on email = MEP.memepf_email1
join DB..Table_B Mbr on MEP.Mem_ID = Mbr.MEM_ID
--Returns Normal Results
January 23, 2014 at 2:46 pm
That's because when SQL Server is "reading" the code, it won't find a reference to Table_B alias until it gets to Table_B.
January 23, 2014 at 2:46 pm
homebrew01 (1/23/2014)
I thought it didn't matter what order the JOIN statements are in a query, but if I join TABLE_B before Table_C, I get an error. If I join Table_B last, I get results. What am I missing ?
SELECTdistinct
Col1, col2, col3, col4
from InputTable EAP
join DB..Table_A M on MT_GoToMeetingID = WebinarID
join DB..Table_B Mbr on MEP.Mem_ID = Mbr.MEM_ID
join DB..Table_C MEP on email = MEP.memepf_email1
--join DB..Table_B Mbr on MEP.Mem_ID = Mbr.MEM_ID 'B' commented out
'The multi-part identifier "MEP.Mem_ID" could not be bound.'
SELECTdistinct
Col1, col2, col3, col4
from InputTable EAP
join DB..Table_A M on MT_GoToMeetingID = WebinarID
--join DB..Table_B Mbr on MEP.Mem_ID = Mbr.MEM_ID 'B' commented out
join DB..Table_C MEP on email = MEP.memepf_email1
join DB..Table_B Mbr on MEP.Mem_ID = Mbr.MEM_ID
--Returns Normal Results
In your first query (the one with the error) you are attempting to join MEP.Mem_ID to Mbr.MEM_ID but MEP has not been defined yet.
-- Itzik Ben-Gan 2001
January 23, 2014 at 2:47 pm
Luis Cazares (1/23/2014)
That's because when SQL Server is "reading" the code, it won't find a reference to Table_B alias until it gets to Table_B.
Beat me by 3 seconds!
-- Itzik Ben-Gan 2001
January 24, 2014 at 3:06 am
Code validation takes place before any of the other optimizer steps, so it has to read correctly as the others have stated. And the order can matter if you have very complex set of joins because the optimizer may not have much time to try to rearrange join order, so it usually makes sense to try to help it out and put the joins in a logical order. It usually rearranges things at will.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply