如何交叉连接来自多个服务器的表?

时间:2019-06-18 16:27:30

标签: sql-server sql-server-2008-r2 ssrs-2008-r2 cross-server

我正在一个项目上创建一个表,该表从本地服务器和2个在线服务器中提取信息。 2个在线服务器均与我的本地服务器链接。我只能读取在线服务器,并且数据量很大,无法创建副本。

我构建了一些可以在Management Studio上使用的代码,但是,当我将这些代码放入SSRS时,我收到一条消息,说我的一个表已经存在。我试图在其前面放置一个drop table子句,但是随后我得到了下一个表的消息,该行已经存在。而且,如果我为每个表拉出drop语句,则刷新字段时都会收到超时超时的错误消息。

SELECT s.SiteID, s.[StoreName], cf.CustomerID, , cf.AccountNumber, cf.AccountStatus,
 cf.Store_ID, cf.InstitutionID, cf.TransactionTime, cf.Comment
INTO #Report_Table1
FROM dbo.View_GetCustomerInfo cf
LEFT JOIN dbo.Store_Table s ON cf.Store_ID = s.Store_ID
;

SELECT t.*, cl.SaleAmount
INTO #Report_Table2
FROM #Report_Table1 t
LEFT JOIN OnlineServe01.Views.dbo.SaleUpdate su
    ON t.CustomerID = cl.CustomerID AND t.Store_ID = cl.Store_ID AND [Status] = 'A'
;

SELECT InstitutionID, Source_ID, BankName
INTO #Report_BankName
FROM OnlineServe01.Views.dbo.InstitutionInfo bn 
WHERE InstitutionID IN (
    SELECT InstitutionID FROM #Report_Table2)
;

SELECT df.*, bn.BankName
INTO #Report_Table3 
FROM #Report_Table2 t
LEFT JOIN #Report_BankName bn ON df.InstitutionID = bn.InstitutionID AND df.Store_ID = bn.Store_ID
;

SELECT StoreName, SiteID, CustomerID, SaleAmount
, BankName, AccountNumber, AccountStatus, TransactionTime, Comment
INTO #Report_Table4
FROM #Report_Table3 t
;

SELECT *
INTO #Report_PlayerName
FROM (
    SELECT DISTINCT CustomerID, FirstName, LastName,
      Dense_Rank () OVER (Partition by CustomerID ORDER BY FirstName) AS Rnk
    FROM OnlineServe02.CustomerManagement.dbo.CustomerName
    WHERE PreferredName = 0 
    AND CustomerID IN (SELECT DISTINCT CustomerID FROM #Report_Table4)
) a
WHERE Rnk = 1
;

SELECT t.*, pn.LastName, pn.FirstName, ca.Deposited, ca.Used, ca.InTransit, ca.Available
FROM #Report_Table4 t
LEFT JOIN OnlineServe02.CustomerManagement.dbo.AccountActivity ca 
ON t.CustomerID = ca.CustomerID AND t.SiteID = ca.SiteID
LEFT JOIN #Report_PlayerName pn ON t.CustomerID = pn.CustomerID 
;

0 个答案:

没有答案