问题
My Source and Destination tables exist on different servers. I am using Execute SQL Task to write Merge Statements to synchronize them.
Could anyone explain how I can reference two different databases that exist on different servers inside my Execute SQL Task?
回答1:
Possible approaches:
I would suggest the following approaches instead of trying to use MERGE statement within Execute SQL Task between two database servers.
Approach #1:
Create two
OLEDB Connection Managersto each of the SQL Server instances. For example, if you have two databases SourceDB and DestinationDB, you could create two connection managers namedOLEDB_SourceDBandOLEDB_DestinationDB. You could also use ADO.NET connection manager, if you prefer that. Based on what I have read in SSIS based books, OLEDB performs better than ADO.NET connection manager.Drag and drop a
Data Flow Taskon the Control Flow tab.Within the Data Flow Task, configure an
OLE DB Sourceto read the data from source database table.Use
Lookup Transformationthat checks whether if the data already exists in the destination table using the uniquer key between source and destination tables.If the source table row
does not existin the destination table, then insert the rows into destination table usingOLE DB DestinationIf the source table row
existsin the destination table, then insert the rows into a staging table on the destination database using anotherOLE DB Destination.Place an
Execute SQL Taskafter the Data Flow Task on the Control Flow tab. Write a query that would update the data in destination table using the staging table data.
Check the answer to the below SO question for detailed steps.
How do I optimize Upsert (Update and Insert) operation within SSIS package?
Approach #2:
Create two
OLEDB Connection Managersto each of the SQL Server instances. For example, if you have two databases SourceDB and DestinationDB, you could create two connection managers namedOLEDB_SourceDBandOLEDB_DestinationDB.Drag and drop a
Data Flow Taskon the Control Flow tab.Within the Data Flow Task, configure an
OLE DB Sourceto read the data from source database table and insert into a staging table usingOLE DB Destination.Place an
Execute SQL Taskafter theData Flow Taskon the Control Flow tab. Write a query that would use the MERGE statement between staging table and the destination table.
回答2:
See this link - http://technet.microsoft.com/en-us/library/cc280522%28v=sql.105%29.aspx
Basically, to do this, you would need to get the data from the different servers into the same place with Data Flow tasks, and then perform an Execute SQL task to do the merge.
The Merge and Merge Join SSIS Data Flow tasks don't look like they do what you want to do.
来源:https://stackoverflow.com/questions/15066982/how-can-i-use-merge-statement-across-multiple-database-servers