sql中的多部分标识符

时间:2016-03-29 05:53:19

标签: sql-server sql-server-2008

我尝试了这个查询:

SELECT
    customer.custm_no, customer.name,customer.name2,
    orderhdr.otype,orderhdr.saleorder,
    orderdtl.sitno,orderdtl.qty,
    delvdtl.qty,delvdtl.issueqty
    material.name,develhdr.nodelv
FROM
    customer
    left join orderhdr on customer.custm_no = orderhdr.custm_no 
    left join orderdtl on orderhdr.otype = orderdtl.otype   
      and        orderhdr.SALEORDER = orderdtl.SALEORDER
    left join delvdtl on orderdtl.SALEORDER = delvdtl.SALEORDER   
    AND  orderdtl.sitno = delvdtl.sitno
    left join  develhdr on delvdtl.nodelv = develhdr.nodelv 
    left join MATMSTA on   material.MATNO = MATMSTA.MATNO   
    left join material on  MATMSTA.MATNO = material.MATNO 
    and MATMSTA.MATNO = material.MATNO
    left join  plantcocode on plant.plant = plantcocode.plant
   left join plant on orderdtl.plant = plantcocode.plant
   left join cocode  on plantcocode.cocode = cocode.cocode 
WHERE
    1 = 1   
order by
 orderdtl.saleorder, orderdtl.sitno

但这告诉我:

  

无法绑定多部分标识符“material .MATNO”。

     

无法绑定多部分标识符“plant.plant”。

编辑后

    SELECT customer.custm_no, customer.name,customer.name2,orderhdr.otype,orderhdr.saleorder,orderdtl.sitno,orderdtl.qty,delvdtl.qty,delvdtl.issueqtymaterial.name,develhdr.nodelv
FROM customer
  left join orderhdr on customer.custm_no = orderhdr.custm_no 
  left join orderdtl on orderhdr.otype = orderdtl.otype   
  and        orderhdr.SALEORDER = orderdtl.SALEORDER
 left join delvdtl on orderdtl.SALEORDER = delvdtl.SALEORDER   
   AND  orderdtl.sitno = delvdtl.sitno
        left join  develhdr on delvdtl.nodelv = develhdr.nodelv 
        left join material on delvdtl.MATNO = material.MATNO 
      left join plant on orderdtl.plant = plant.plant
      left join  plantcocode on plant.plant = plantcocode.plant
    left join cocode  on plantcocode.cocode = cocode.cocode
     WHERE
        1 = 1   
    order by
     orderdtl.saleorder, orderdtl.sitno

任何解决方案?

1 个答案:

答案 0 :(得分:1)

您的查询存在一些问题:
1)查询中没有date '+%m-%d'表/别名。是customer你应该给别名CUSTMST吗?
2)customer需要在MATMSTA子句中连接到其上方的某个表。
3)您在加入FROMjoin表时两次提到相同的MATMSTA条件。它应该只给一次。

纠正这些问题,应该没问题。