在查询中链接同一表的两个实例

时间:2019-07-16 15:35:40

标签: sql-server ms-access migrate

我有一张与销售组织的货币相关的销售订单表,以及一个成本核算工厂,它可能有也可能没有其他货币。我有一个汇率表,列出了不同货币对的汇率。我需要将汇率表链接到销售货币,并分别链接到成本货币。当我尝试两次链接交换表时,出现错误消息,我收到此错误-

  

对象“ dbo.VW_CurrrentExchange”和“ dbo.VW_CurrentExchange”在   FROM子句具有相同的公开名称。使用相关名称   区分它们。

我尝试过两次链接表,为表赋予一个额外的名称(例如在Access中,例如dbo.VW_CurrentExchange_1),并且只是对Access中的内容进行建模。

select dbo.SalesOrderItems.DV, dbo.SalesOrderItems.SOrg, dbo.SalesOrderItems.SalesDoc, dbo.SalesOrderItems.item, dbo.SalesOrderItems.itca, dbo.SalesOrderItems.rj, dbo.SalesOrderItems.Createdon1 as ItemCreation, dbo.SalesOrderItems.[Sold-topt] as SoldTo, dbo.SalesOrderItems.OrderQuantity, dbo.SalesOrderItems.Cumulconfirmedqty as CCQSU,  dbo.SalesOrderItems.SU, dbo.SalesOrderItems.Cumulconfirmedqty1 as CCQBU, dbo.SalesOrderItems.BUn, dbo.SalesOrderItems.Material, dbo.SalesOrderItems.Plnt as Plant, dbo.SalesOrderItems.Reqdlvdt as RDD, dbo.SalesOrderItems.Netprice, dbo.SalesOrderItems.per, dbo.SalesOrderItems.RSD, dbo.SalesOrderItemStatus.DS , dbo.SalesOrderItemStatus.DS1 , dbo.SalesOrderItemStatus.DS2 , dbo.SalesOrderItemStatus.DS3, dbo.SalesOrderItemStatus.GM, dbo.SalesOrderItemStatus.OvCS, dbo.SalesOrderItemStatus.OS, dbo.SalesOrderItemStatus.DB, dbo.SalesOrderItemStatus.DB1, dbo.SalesOrderItemStatus.GS, dbo.SalesOrderItemStatus.BS, dbo.SalesOrderItemStatus.Rejectionstatus, dbo.customers.Name3, dbo.MARA.MaterialDescription, dbo.MARA.MatlGroup, dbo.MARC.ProfitCtr, dbo.VW_CurrentExchange.Exchange as PExch, dbl.VW_CurrentExchange.exchange as CExch           

from dbo.SalesOrderItems join  dbo.SalesOrderItemStatus on dbo.SalesOrderItems.salesdoc =  dbo.SalesOrderItemStatus.salesdoc and dbo.salesorderitems.item = SalesOrderItemStatus.item join dbo.VW_CurrentExchange on dbo.salesorderitems.curr = dbo.vw_currentexchange.Currency2 join dbo.customers on dbo.SalesOrderItems.[Sold-topt]=dbo.customers.Customer join dbo.MARA on dbo.SalesOrderItems.Material = dbo.MARA.Material join dbo.MARC on dbo.SalesOrderItems.Material = dbo.MARC.material and dbo.SalesOrderItems.plnt=dbo.MARC.Plnt join dbo.MBEW on dbo.SalesOrderItems.Material= dbo.MBEW.Material and dbo.SalesOrderItems.plnt=dbo.MBEW.ValA join dbo.Plants on dbo.SalesOrderItems.plnt = dbo.Plants.Plnt join dbo.VW_CurrentExchange on dbo.SalesOrderItems.curr = dbo.VW_CurrentExchange.Currency2

order by dbo.salesorderitems.Createdon1 desc

希望获得列出的字段,包括PExch和CExch,但希望获得Msg 1013,

  

16级状态1行1对象   FROM中的“ dbo.VW_CurrentExchange”和“ dbo.VW_CurrentExchange”   子句具有相同的公开名称。使用相关名称   区分它们。

1 个答案:

答案 0 :(得分:0)

将表链接两次是错误的事情。相反,正如@ indika-ranaweera在注释中所建议的那样,您想要做的是在SQL中使用SAME链接表,但给它一个别名。

实际上,始终为表提供别名 是一种好习惯,并且可以真正读取您的SQL,而不必依赖于Access提供的图形查询编辑器

我用http://www.dpriver.com/pp/sqlformat.htm重新格式化了您的SQL,添加了别名,希望对VW_CurrentExchange的两种用法做出明智的猜测,并得到以下结果:

SELECT SOI.dv, 
       SOI.sorg, 
       SOI.salesdoc, 
       SOI.item, 
       SOI.itca, 
       SOI.rj, 
       SOI.createdon1         AS ItemCreation, 
       SOI.[sold-topt]        AS SoldTo, 
       SOI.orderquantity, 
       SOI.cumulconfirmedqty  AS CCQSU, 
       SOI.su, 
       SOI.cumulconfirmedqty1 AS CCQBU, 
       SOI.bun, 
       SOI.material, 
       SOI.plnt               AS Plant, 
       SOI.reqdlvdt           AS RDD, 
       SOI.netprice, 
       SOI.per, 
       SOI.rsd, 
       SOIS.ds, 
       SOIS.ds1, 
       SOIS.ds2, 
       SOIS.ds3, 
       SOIS.gm, 
       SOIS.ovcs, 
       SOIS.os, 
       SOIS.db, 
       SOIS.db1, 
       SOIS.gs, 
       SOIS.bs, 
       SOIS.rejectionstatus, 
       C.name3, 
       MARA.materialdescription, 
       MARA.matlgroup, 
       MARC.profitctr, 
       VCE1.exchange        AS PExch, 
       VCE2.exchange        AS CExch 
FROM   dbo.salesorderitems AS SOI
       JOIN dbo.salesorderitemstatus AS SOIS
     ON SOI.salesdoc = SOIS.salesdoc 
        AND SOI.item = salesorderitemstatus.item 
       JOIN dbo.vw_currentexchange AS VCE1
     ON SOI.curr = VCE1.currency2 
       JOIN dbo.customers AS C
     ON SOI.[sold-topt] = C.customer 
       JOIN dbo.mara AS MARA
     ON SOI.material = MARA.material 
       JOIN dbo.marc AS MARC
     ON SOI.material = MARC.material 
        AND SOI.plnt = MARC.plnt 
       JOIN dbo.mbew AS MBEW
     ON SOI.material = MBEW.material 
        AND SOI.plnt = MBEW.vala 
       JOIN dbo.plants AS P
     ON SOI.plnt = P.plnt 
       JOIN dbo.vw_currentexchange AS VCE2
     ON SOI.curr = VCE2.currency2 
ORDER  BY SOI.createdon1 DESC 

编辑:查看我发布的内容后,我看不到您需要两份vw_currentexchange的原因。它们都由相同的确切字段(currcurrency2)关联。也许其中之一应该是currency1