The multi-part identifier could not be bound again

寵の児 提交于 2019-12-07 12:44:19

问题


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:


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 + '%'


来源:https://stackoverflow.com/questions/17989401/the-multi-part-identifier-could-not-be-bound-again

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!