在同一服务器SQL Server上连接多个数据库

时间:2014-10-04 23:54:59

标签: sql sql-server join

我正在尝试从两个数据库进行查询 - 位于同一台服务器上。第一个数据库查询有点复杂,有JOIN和UNION子句,但加入另一个数据库非常简单。我一直在最后一行代码 - SITE_ID_NUMBER上收到错误。

我从未尝试过这个 - 有关我可能做错的任何线索吗?附加说明 - 数据库的JOIN子句由一个本地列组成 - 即“6041”和一个计算 - 我必须使用SUBSTRING并添加6以获得匹配值。

更新 - 经过一些更改后,我实际收到错误:

  

Msg 8156,Level 16,State 1,Line 32
  为“P”多次指定了“RECORD_DATE_TIME”列。

谢谢。

SELECT P.[6] + SUBSTRING(A.SHIP_TO,6,3) AS 'STORE' ,
       S.SITE_ID_NUMBER,
       S.DM,
       S.AREA,
       S.STORE_NAME,
       S.LOCATION,
       P.WHSE AS 'DIVISION_DESC', 
       P.ORDER_STATUS,
       P.MATERIAL,
       P.DESCRIPTION,
       P.PO_NUMBER AS 'CUSTOMER_PO_NUMBER',
       P.ORDER_QUANTITY AS'QTY',
       P.RSHIP_DATE AS 'REQUESTED_SHIP_DATE',
       S.TRANSIT_6040_6041

FROM

(SELECT *

FROM  PDX_SAP_USER.dbo.VW_ADIDAS_RETAIL_aRI A
JOIN  PDX_SAP_USER.dbo.VW_WB_DELIVERIES D
ON    A.DELIVERY_NUMBER = D.DELIVERY_NUMBER

UNION ALL 

SELECT *

FROM   PDX_SAP_USER.dbo.VW_ADIDAS_RETAIL_aRO A
JOIN   PDX_SAP_USER.dbo.VW_WB_DELIVERIES D
ON     A.DELIVERY_NUMBER = D.DELIVERY_NUMBER) P 

JOIN [ADI_USER.MAINTAINED].dbo.SiteDataAdiRbk S

ON  S.SITE_ID_NUMBER = P.STORE;

3 个答案:

答案 0 :(得分:1)

Msg 8156, Level 16, State 1, Line 32 The column 'RECORD_DATE_TIME' was specified multiple times for 'P'. RECORD_DATE_TIME在您提供的查询中无处可寻。无论如何,错误很明显。我将向您展示一个再现错误的简单示例。使用此示例来修复代码。

CREATE TABLE [dbo].[t1](
    [id] [int] NULL,
    [name] [nchar](10) NULL
) ON [PRIMARY]

如图所示将表连接到自身以获取此错误。

select *
from
(
select *
from t1 inner join t1 as t2 on t1.id = t2.id
) as t

Msg 8156, Level 16, State 1, Line 6
The column 'id' was specified multiple times for 't'.

答案 1 :(得分:1)

在最外层,您可以访问2个表别名:

  • P:使用Union
  • 的子查询中的结果集
  • S:哪个是表的别名[ADI_USER.MAINTAINED] .dbo.SiteDataAdiRbk

选择列表中的第一项是 P. [6] + SUBSTRING( A .SHIP_TO,6,3)AS' STORE'

您正在引用一个名为A的表或别名,该表或别名无法访问。

答案 2 :(得分:0)

谢谢大家的回复和好主意。经过一些非常需要的关注其他项目之后,我突然得到了答案,而且到目前为止它似乎正在发挥作用。尽管如此,我还是没有注意到这些想法。我的目标是加入在我们的工作场所用户维护文件夹中创建的表格,我在这里读取/写入数据库,提取交付信息,并按照下面的方式进行操作 - 我会说我需要进一步挖掘帐户由于存在两个可能的位置,因此货物来自哪里的变化,但这比我们现在好得多,甚至新的分析师也能够运行并发布到报告站点。

SELECT AD.STORE,
       S.DM,
       S.AREA,
       S.STORE_NAME,
       S.LOCATION,
       AD.DIVISION_DESC, 
       AD.ORDER_STATUS,
       AD.MATERIAL,
       AD.DESCRIPTION,
       AD.CUSTOMER_PO_NUMBER,
       AD.QTY,
       AD.REQUESTED_SHIP_DATE,
       S.TRANSIT_6040_6041,
       AD.REQUESTED_SHIP_DATE + S.TRANSIT_6040_6041 AS'ESTIMATED_ESTIMATED_DELIVERY_DATE'


FROM 

(

SELECT '6' + SUBSTRING(A.SHIP_TO,6,3) AS 'STORE',
       '' AS 'DM',
       '' AS 'AREA',
       '' AS 'STORE_NAME',
       '' AS 'LOCATION',
       A.WHSE AS 'DIVISION_DESC', 
       A.ORDER_STATUS,
       D.MATERIAL,
       D.DESCRIPTION,
       A.PO_NUMBER AS 'CUSTOMER_PO_NUMBER',
       D.ORDER_QUANTITY AS'QTY',
       A.RSHIP_DATE AS 'REQUESTED_SHIP_DATE',
       '' AS 'TRANSIT',
       '' AS 'ESTIMATED_DELIVERY_DATE'


FROM  VW_ADIDAS_RETAIL_aRI A
JOIN  VW_WB_DELIVERIES D
ON    A.DELIVERY_NUMBER = D.DELIVERY_NUMBER

GROUP BY A.SHIP_TO,
       A.WHSE,
       A.ORDER_STATUS,
       A.PO_NUMBER,
       D.MATERIAL, 
       D.DESCRIPTION, 
       D.ORDER_QUANTITY,
       A.RSHIP_DATE,
       A.ASHIP_DATE

HAVING A.ORDER_STATUS NOT IN ('shp')

UNION ALL 

SELECT '6' + SUBSTRING(A.SHIP_TO,6,3) AS 'STORE',
       '' AS 'DM',
       '' AS 'AREA',
       '' AS 'STORE_NAME',
       '' AS 'LOCATION',
       A.WHSE AS 'DIVISION_DESC',
       A.ORDER_STATUS,
       D.MATERIAL,
       D.DESCRIPTION,
       A.PO_NUMBER AS 'CUSTOMER_PO_NUMBER',
       D.ORDER_QUANTITY AS'QTY',
       A.RSHIP_DATE AS 'REQUESTED_SHIP_DATE',
       '' AS 'TRANSIT',
       '' AS 'ESTIMATED_DELIVERY_DATE'

FROM   VW_ADIDAS_RETAIL_aRO A
JOIN   VW_WB_DELIVERIES D
ON     A.DELIVERY_NUMBER = D.DELIVERY_NUMBER

GROUP BY A.SHIP_TO,
       A.WHSE,
       A.ORDER_STATUS,
       A.PO_NUMBER,
       D.MATERIAL, 
       D.DESCRIPTION, 
       D.ORDER_QUANTITY,
       A.RSHIP_DATE,
       A.ASHIP_DATE

HAVING A.ORDER_STATUS NOT IN ('shp')) AD

JOIN [ADI_USER_MAINTAINED].dbo.SiteDataAdiRbk S 

ON AD.STORE = S.SITE_ID_NUMBER;