左连接无法绑定

时间:2017-08-02 17:39:58

标签: sql-server

我有这张表:

WS | Cat | Prod | CS | Dis| UserWS | MarA | ModA | MarU | BCIU

我需要加入它们以获得所有行关系,即使有些是空的,我试过这个:

Select *
FROM WS 
Left join MarU on MarU.Code=WS.used_brand
Left join Prod p on p.MarA=Cat.OID 
Left join CS on WS.CS =CS.OID  
Left join Prod pp on WS.product_code=pp.Code 
Left join MarA on WS.customer_car_brand=MarA.Code 
Left join ModA on WS.customer_car_model=ModA.Code
Left join CS c on c.UserWS=UserWS.Oid 
Left join CS css on css.Dis=Dis.OID
Left join BCIU on BCIU.Code=WS.used_bci

当我运行它时,我收到此错误:

  

[Err] 42000 - [SQL Server]多部分标识符“Cat.OID”可以   不受约束。 42000 - [SQL Server]多部分标识符   “UserWS.Oid”无法绑定。 42000 - [SQL Server]多部分   标识符“Dis.OID”无法绑定。 42000 - [SQL Server]   无法绑定多部分标识符“Cat.Name”。 42000 - [SQL   服务器]无法绑定多部分标识符“Prod.Name”。 42000    - [SQL Server]无法绑定多部分标识符“Dis.Nombre”。 42000 - [SQL Server]多部分标识符“UserWS.Nombre”   无法受约束。 42000 - [SQL Server]多部分标识符   “Cat.Segmento”无法受约束。

我缺少什么?

3 个答案:

答案 0 :(得分:2)

首先,简化查询,以隔离错误。你会得到同样的错误:

SELECT *
FROM WS Left join
     MarU
     on MarU.Code = WS.used_brand Left join
     Prod p
     on p.MarA = Cat.OID 

如错误所示,Cat未定义。这不是前向引用问题,因为Cat未在FROM子句中的任何位置定义。大概,你刚刚省略了相关的表/视图。

答案 1 :(得分:2)

您的错误消息实际上是多个错误。我轮流对付他们。

查看以-->开头的行:

[Err] 
42000 - [SQL Server]The multi-part identifier "Cat.OID" could not be bound.
 --> Your SQL doesn't mention JOIN Cat table anywhere

42000 - [SQL Server]The multi-part identifier "UserWS.Oid" could not be bound. 
 --> Your SQL doesn't mention JOIN UserWS table anywhere

42000 - [SQL Server]The multi-part identifier "Dis.OID" could not be bound. 
 --> Your SQL doesn't mention JOIN Dis table anywhere

42000 - [SQL Server]The multi-part identifier "Cat.Name" could not be bound. 
 --> Your SQL doesn't mention JOIN Cat table anywhere

42000 - [SQL Server]The multi-part identifier "Prod.Name" could not be bound. 
 --> Your SQL doesn't call the Prod table "Prod", it joins it twice and aliases it as "p" and "pp". refer to p.Name or pp.Name, not Prod.Name

42000 - [SQL Server]The multi-part identifier "Dis.Nombre" could not be bound. 
 --> Your SQL doesn't mention JOIN Dis table anywhere

42000 - [SQL Server]The multi-part identifier "UserWS.Nombre" could not be bound. 
 --> Your SQL doesn't mention JOIN UserWS table anywhere

42000 - [SQL Server]The multi-part identifier "Cat.Segmento" could not be bound.
 --> Your SQL doesn't mention JOIN Cat table anywhere

答案 2 :(得分:1)

Left join Prod p on p.MarA=Cat.OID 

上述语句中的cat必须是Table或Table别名,但您的语句中没有表或别名,只有Cat。

事先添加LEFT JOIN CAT on [ForeignKey]=CAT.[PrimaryKey]