不能绑定多部分标识符“”

时间:2012-02-23 20:37:37

标签: sql-server-2008

我收到错误“多部分标识符”Company.Id“无法绑定”。在代码的最后一个Company.Id下面的代码块上。谁能告诉我为什么我会收到这个错误?

declare @ModuleId int
set @ModuleId = 3

Select Top 1 Vehicle.Id 
From dbo.Vehicle (NoLock) 
Join 
(
        Select InventoryCompany.CompanyId As Id
    From dbo.InventoryCompany (NoLock) 
        Join dbo.Inventory (NoLock) On Inventory.Id = InventoryCompany.InventoryId
        Join dbo.Module(NoLock) On Module.Content = Inventory.Id  
    Where dbo.Module.Id = @ModuleId
)CompanyIds On Company.Id = Vehicle.CompanyId

3 个答案:

答案 0 :(得分:5)

在最后一行,请更改:

CompanyIds On Company.Id = Vehicle.CompanyId

要:

CompanyIds On CompanyIds.Id = Vehicle.CompanyId

答案 1 :(得分:1)

您需要将标识符更改为CompanyIds

declare @ModuleId int
set @ModuleId = 3

Select Top 1 Vehicle.Id 
From dbo.Vehicle (NoLock) 
Join 
(
        Select InventoryCompany.CompanyId As Id
    From dbo.InventoryCompany (NoLock) 
        Join dbo.Inventory (NoLock) On Inventory.Id = InventoryCompany.InventoryId
        Join dbo.Module(NoLock) On Module.Content = Inventory.Id  
    Where dbo.Module.Id = @ModuleId
)CompanyIds 
ON CompanyIds.Id = Vehicle.CompanyId

答案 2 :(得分:1)

您将子查询命名为CompanyIds,这就是您应该使用的

declare @ModuleId int
set @ModuleId = 3

Select Top 1 Vehicle.Id 
From dbo.Vehicle (NoLock) 
Join 
(
        Select InventoryCompany.CompanyId As Id
    From dbo.InventoryCompany (NoLock) 
        Join dbo.Inventory (NoLock) On Inventory.Id = InventoryCompany.InventoryId
        Join dbo.Module(NoLock) On Module.Content = Inventory.Id  
    Where dbo.Module.Id = @ModuleId
) CompanyIds On CompanyIds.Id = Vehicle.CompanyId