跨多个服务器合并数据

时间:2014-09-10 13:06:44

标签: sql sql-server tsql

好的,所以我有这个用于记录存储过程用法的查询。问题是,我使用了merge语句,它不能跨多个服务器插入到中央表中。我基本上需要在每个服务器上生成信息,然后将数据插入中央服务器上的表中。任何输入都会很好地解决这个问题。我们正在使用SQL 2008 R2。

MERGE INTO [DBA].dbo.SP_Exec_Stats_Table STAT
USING 
(
SELECT db.name as [DatabaseName]
    ,OBJECT_NAME(d.object_id, d.database_id) [ProcedureName] 
    ,d.last_execution_time
    ,o.modify_date
    ,d.total_elapsed_time/d.execution_count AS [avg_elapsed_time]
    ,d.execution_count
    ,@@SERVERNAME as [ServerName]
    ,d.object_id
FROM sys.dm_exec_procedure_stats AS d inner join sys.databases as db on db.database_id = d.database_id
    inner join sys.objects o on o.object_id = d.object_id
WHERE d.database_id not in(1,2,3,4,5) 
) as SRC
ON STAT.[Object_id] = SRC.Object_id
WHEN MATCHED 
 AND STAT.[Last_Exec_Time] <> SRC.last_execution_time THEN
UPDATE SET
    [Last_Exec_Time] = SRC.last_execution_time
WHEN NOT MATCHED THEN
INSERT  (DatabaseName 
,object_id
,[ProcedureName]
,[Last_Exec_Time]
,[Modified_Date]
,[Avg_Elapsed_Time]
,[Exec_Count]
,[Server_Name]
)
VALUES (SRC.DatabaseName 
    ,SRC.object_id
    ,SRC.[ProcedureName]
    ,SRC.last_execution_time
    ,SRC.modify_date
    ,SRC.[Avg_Elapsed_Time]
    ,SRC.execution_count
    ,SRC.[ServerName]
    ) ;

1 个答案:

答案 0 :(得分:0)

通过重写查询以从远程服务器提取信息并将其插入本地服务器,我能够绕过MERGE语句的限制。我将'from'更改为包含远程服务器的完全限定名称,并能够将数据提取到本地服务器。