无法在 SQL Server 2008 上绑定多部分标识符

Posted

技术标签:

【中文标题】无法在 SQL Server 2008 上绑定多部分标识符【英文标题】:The multi-part identifier could not be bound on SQL Server 2008 【发布时间】:2010-11-07 23:35:03 【问题描述】:

我有两张桌子

requests (ID, company_id, amount)

companies (ID, name)

带有 FK 约束 (requests.company_id -> companies.id)

requests.company 可以是NULL

我需要获取所有请求并将company_id 替换为适当的公司name,如果没有指定公司,则将其留空。

我有下一个问题:

SELECT R.[ID], C.[name] AS [company], R.[amount], ...
FROM [requests] AS R, [companies] AS C, ...
WHERE R.[company_id] = C.[ID]

company 字段中输入 NULL 之前它工作正常。

我试着做下一步:

SELECT R.[ID], C.[name] AS [company], ...
FROM [requests] AS R, ...
LEFT OUTER JOIN [companies] AS C
ON R.[company_id] = S.ID

得到了

无法绑定多部分标识符“R.company_id”

ON 子句转换中的字段也会出现相同的错误。我做错了什么?

【问题讨论】:

【参考方案1】:

我想你想要:

SELECT R.[ID], ISNULL(C.[name], '') AS [company]
FROM [requests] AS R
    LEFT OUTER JOIN [companies] AS C
ON R.[company_id] = C.ID

编辑:见 cmets,需要左连接 ...

在我看来也不需要左连接,所以你可以重写为:

SELECT R.[ID], C.[name] AS [company]
FROM [requests] AS R
    JOIN [companies] AS C
ON R.[company_id] = C.ID

【讨论】:

他确实需要左连接。 Requests.Company_ID 可以为空。如果没有外部连接,Company_ID 为 null 的请求将从结果中删除。 @Shannon,你说得对,我错过了这个:如果没有指定公司,则将其留空。【参考方案2】:

您展示的代码示例有省略号,我相信是省略号中的内容导致了问题。

你有:

SELECT R.[ID], C.[name] AS [company], ...
FROM [requests] AS R, ...
LEFT OUTER JOIN [companies] AS C
ON R.[company_id] = S.ID

假设是这样的:

SELECT R.[ID], C.[name] AS [company], X.Field
FROM [requests] AS R, [eXample] as X 
LEFT OUTER JOIN [companies] AS C
ON R.[company_id] = S.ID
WHERE X.[request_id] = R.ID

换句话说,ANSI 92 之前的内连接语法与 ANSI 92 外连接语法的混合。在 SQL Server 2005 上进行测试时,在您的示例中,在将 R 与 ... 分隔开的逗号之后,似乎看不到请求的别名 R,而在我的示例中,[eXample] 为 X。然而,以下确实有效:

SELECT R.[ID], C.[name] AS [company], X.Field
FROM [eXample] as X, [requests] AS R 
-- Requests and companies on the same side of the comma
LEFT OUTER JOIN [companies] AS C
ON R.[company_id] = S.ID
WHERE X.[request_id] = R.ID

SELECT R.[ID], C.[name] AS [company], X.Field
FROM [requests] AS R LEFT OUTER JOIN [companies] AS C
    ON R.[company_id] = S.ID, [eXample] as X 
WHERE X.[request_id] = R.ID
-- Yuck, I would hate to find this. Not at all sure from reading
-- the code how it would work.

还是我最喜欢的,因为我喜欢 ANSI 92 连接语法:

SELECT R.[ID], C.[name] AS [company], X.Field
FROM [requests] AS R
INNER JOIN [eXample] as X ON X.[request_id] = R.ID
LEFT OUTER JOIN [companies] AS C ON R.[company_id] = S.ID

【讨论】:

表在哪里别名为“S”以便约束“= S.ID”有效? S.ID 在 OP 中,可能在椭圆中。 ???既然它帮助了 OP,我不想编辑,因为我担心我只会扼杀解释。处理丢失的代码很困难。【参考方案3】:

用逗号更改表顺序使其在 sql server 2005,2008 和 2012 中工作

例如

假设您有如下查询,但在 sql 2005 及更高版本中将失败:

SELECT t1.*,t2.*, t3.*
FROM table1 AS t1, table2 as t2 
LEFT OUTER JOIN table3 AS t3
ON t1.id = t3.id
WHERE t1.id = t2.id

如果您重写查询并在连接前以逗号更改表的顺序,则该查询将在 sql 2005 及更高版本中工作。例如

SELECT t1.*,t2.*, t3.*
FROM table2 as t2, table1 as t1 
LEFT OUTER JOIN table3 AS t3
ON t1.id = t3.id
WHERE t1.id = t2.id

【讨论】:

以上是关于无法在 SQL Server 2008 上绑定多部分标识符的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server 2008 使用 case 语句查看错误

如何将架构绑定添加到 SQL Server 2008 中的视图

无法进入远程 SQL Server 2008 上的存储过程

无法在 Windows Server 2008 R2 上启动 Apache

无法安装 SQL Server 2008 R2 Express

启动 SQL Server 管理 Studio 在 SQL Server 2008R2 中的错误消息:"无法读取此系统上以前注册的服务器的列表" 解决方法