The multi-part identifier could not be bound again

2019-05-10 03:25发布

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.

1条回答
倾城 Initia
2楼-- · 2019-05-10 04:04

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 + '%'
查看更多
登录 后发表回答