Search code examples
sqlsql-server-2000

The multi-part identifier could not be bound again


I am trying to create a stored procedure like this,

CREATE PROCEDURE [dbo].[SP_Name]
(
    @ID varchar(50),
    @URL varchar(256)
)
AS
    SELECT DISTINCT 
       Table1.CID, Table2.Name, Table2.aID, Table2.bID,
       Table3.SchemeName, Table2.cURL
    FROM Table4
    INNER JOIN Table5 ON Table5.eID = Table1.eID
    INNER JOIN Table2 ON Table2.ID = Table1.CID
    INNER JOIN [Table3] ON Table3.aID = Table2.aID AND Table3.bID = Table2.bID
    WHERE 
        Table5.ID = @ID 
        AND Table2.cURL LIKE '%' + @URL + '%'

but I'm getting this error:

Msg 4104, Level 16, State 1, Procedure SP_Name, Line 7
The multi-part identifier "Table1.eID" could not be bound.
Msg 4104, Level 16, State 1, Procedure SP_Name, Line 7
The multi-part identifier "Table1.cID" could not be bound.
Msg 4104, Level 16, State 1, Procedure SP_Name, Line 7
The multi-part identifier "Table1.cID" could not be bound.

Even though syntax is right.


Solution

  • You have to add inner join with Table1

    SELECT DISTINCT Table1.CID, 
    Table2.Name, Table2.aID, Table2.bID,
    Table3.SchemeName, Table2.cURL
    
    FROM Table4
    INNER JOIN Table1 ON ....
    INNER JOIN Table5 ON Table5.eID = Table1.eID
    INNER JOIN Table2 ON Table2.ID = Table1.CID
    INNER JOIN [Table3] ON Table3.aID = Table2.aID AND Table3.bID = Table2.bID
    
    WHERE Table5.ID=@ID AND Table2.cURL LIKE '%' + @URL + '%'
    

    or use Table1 instead of Table4

    SELECT DISTINCT Table1.CID, 
    Table2.Name, Table2.aID, Table2.bID,
    Table3.SchemeName, Table2.cURL
    
    FROM Table1
    INNER JOIN Table1 ON ....
    INNER JOIN Table5 ON Table5.eID = Table1.eID
    INNER JOIN Table2 ON Table2.ID = Table1.CID
    INNER JOIN [Table3] ON Table3.aID = Table2.aID AND Table3.bID = Table2.bID
    WHERE Table5.ID=@ID AND Table2.cURL LIKE '%' + @URL + '%'