3

我正在尝试创建一个这样的存储过程,

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

但我收到此错误:

消息 4104,级别 16,状态 1,过程 SP_Name,第 7 行
无法绑定多部分标识符“Table1.eID”。
消息 4104,级别 16,状态 1,过程 SP_Name,第 7 行
无法绑定多部分标识符“Table1.cID”。
消息 4104,级别 16,状态 1,过程 SP_Name,第 7 行
无法绑定多部分标识符“Table1.cID”。

即使语法是正确的。

4

1 回答 1

4

您必须添加内部连接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 + '%'

或使用Table1代替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 + '%'
于 2013-08-01T08:56:51.643 回答