Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL 2008 MERGE using Dynamic SQL over a Linked Server Expand / Collapse
Author
Message
Posted Tuesday, April 07, 2009 10:45 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 2:08 PM
Points: 1,157, Visits: 3,249
Comments posted to this topic are about the item SQL 2008 MERGE using Dynamic SQL over a Linked Server

Tommy

Post #692289
Posted Wednesday, March 16, 2011 9:30 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, April 03, 2014 4:20 AM
Points: 78, Visits: 418
Thanks Tommy for this great procedure.
But there's a problem if you want to merge a table with more than 2 columns in the PK.
If you change

SELECT @pk_2 = 'AND ' + @local_schema_name + '_TARGET.' + column_name + ' = ' + @local_schema_name + '_SOURCE.' + column_name 
FROM @pk_table WHERE key_seq = 2

into

SET @pk_2 = '';
SELECT @pk_2 = @pk_2 + ' AND ' + @local_schema_name + '_TARGET.' + column_name + ' = ' + @local_schema_name + '_SOURCE.' + column_name
FROM @pk_table WHERE key_seq > 1;

you can also handle tables with more than 2 columns as a PK.

Best regards
Helmut
Post #1079055
Posted Wednesday, March 16, 2011 10:37 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 2:08 PM
Points: 1,157, Visits: 3,249
Very good point Helmut, thank you!

Tommy

Post #1079106
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse