R-nold
R-nold

Reputation: 242

The multi part identifier xxx could not be bound (Inner-Join)

I have created the following query:

SELECT vw.PackID AS NewData
FROM
(
    SELECT mt.PackID
    FROM EntityTable entities 
    INNER JOIN
    (
        SELECT TOP 1 * FROM MasterTable mt
        WHERE [Id] IN 
        (
            SELECT TOP 1 [chartID] FROM ChartTable
            Where [Code] IN
            (
                SELECT TOP 1 [Account] FROM ItemsTable it
                Where [EntityID] IN
                (
                    Select Distinct EntityID From EntityTable Where EntityID=@EntityID
                )
            )
        )
    ) mtbvw
    ON mt.Id = mtbvw.Id
) vw

I am a C# Developer and have barely touched SQL, so there is probably a better way to achieve this (With Inner Joins?) but I am getting the following errors:

The multi-part identifier "mt.Id" could not be bound.

The multi-part identifier "mcoa.1" could not be bound.

Can anyone help as to why I am getting these errors? Thank you! :)

A little bit of info regarding what this is doing:

The EntityID is getting passed into the stored procedure. The Account is then taken from ItemsTable based on the EntityID passed in. The accuont is then passed to ChartTable which gives back the first ChartID (All the ChartID's are the same for that Account so takiing the top one will not matter) and same passing this ChartID back into the MasterTable and getting out the Main ID. Thanks :)

Upvotes: 1

Views: 778

Answers (1)

Sean Lange
Sean Lange

Reputation: 33571

This is a guess based on the query provided.

Select Distinct et.EntityID 
From EntityTable et
join ItemsTable it on it.EntityID = et.EntityID
join ChartTable ct on ct.Code = it.Account
join MasterTable mt on mt.Id = ct.charID
Where et.EntityID = @EntityID

Upvotes: 1

Related Questions