我在 SQL SERVER 2008 的存储过程中编写了以下查询: (我故意截断了表名,也使查询更易于阅读)
1)
select Pr.Id from D D , DP DP, P P, Pr Pr
where D.Id = DP.Id AND DP.Id = P.Id AND P.Id = Pr.Id;
它工作得很好,但是当我使用内部连接编写相同的查询时
2)
select Pr.PId from D D
INNER JOIN DP DP ON D.Id = DP.Id
INNER JOIN P P ON DP.Id = P.Id
INNER JOIN Pr Pr ON P.Id = Pr.Id
它抛出错误:无法绑定(bind)多部分标识符“Pr.Id”。
3) 我再次尝试了另一个版本:
select Pr.Id from Pr Pr
INNER JOIN P P ON Pr.Id = P.Id
INNER JOIN DP DP ON P.Id = DP.Id
INNER JOIN Dealer D ON DP.Id = D.Id
我不明白为什么 1 和 3 工作而 2 不工作。 虽然我觉得他们都是同一个意思。
最佳答案
由于示例 2) 和 3) 之间的区别在于 Pr
表的位置,并且由于您的错误引用了 Pr.Id
,因此我假设您是在连接序列中引用表之前尝试使用 Pr
中的列。
一个简单的例子:
select a.ID
from a
join b on a.id = b.id and a.id = c.id <--- referenced too early.
join c on c.id = b.cid
会给你错误:
The multi-part identifier "c.id" could not be bound.
因为您试图在表别名出现在您的连接序列中之前引用它。
关于sql - 内部连接错误(多部分标识符无法绑定(bind)错误仅在使用时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15414359/