Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cant bind correlated subquery with WHERE clause?

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

I'm trying to join some data with a correlated subquery from a linked server table.

I have...
Code:
SELECT CompanyName,FirstName,LastName,[Membership Number]
FROM dbo.Contacts c
LEFT OUTER JOIN (
			SELECT TOP 1 Adv_MemNo,Sub_Date 
			FROM [Members].[Members_Live].[dbo].Business_Register b
			WHERE b.Adv_MemNo = c.[Membership Number]
			ORDER BY Sub_Date ASC
			) AS FirstNBCS
			ON c.[Membership Number] = FirstNBCS.Adv_MemNo

But it won't run with
The multi-part identifier "c.Membership Number" could not be bound.

Any ideas?

Thanks,
1DMF.

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
Well I changed it to
Code:
SELECT CompanyName,FirstName,LastName,[Membership Number],(
			SELECT TOP 1 Sub_Date 
			FROM [Members].[Members_Live].[dbo].Business_Register b
			WHERE b.Adv_MemNo = c.[Membership Number]
			ORDER BY Sub_Date ASC
			) AS FirstNBCS
FROM dbo.Contacts c

And it works, so am I not able to use correlated column names in the FROM clause, only the SELECT clause?

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
Well I changed it to
Code:
SELECT CompanyName,FirstName,LastName,[Membership Number],(
			SELECT TOP 1 Sub_Date 
			FROM [Members].[Members_Live].[dbo].Business_Register b
			WHERE b.Adv_MemNo = c.[Membership Number]
			ORDER BY Sub_Date ASC
			) AS FirstNBCS
FROM dbo.Contacts c

And it works, so am I not able to use correlated column names in the FROM clause, only the SELECT clause?

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
I had a look at Wiki and it seems to use it in the WHERE or as I have found is OK in the SELECT clause but not the FROM, is that due to the processing order of a SQL statement and as the FROM clause is processed first it needs to create the Cartesian product of all tables in the FROM clause before it can process any correlation?

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top