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: Thursday, May 16, 2013 12:43 PM
Points: 1,148, Visits: 3,148
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
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, May 15, 2013 3:03 AM
Points: 73, Visits: 386
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


Database Architect
initPRO GmbH
[url=http://www.initpro.de][/url]
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: Thursday, May 16, 2013 12:43 PM
Points: 1,148, Visits: 3,148
Very good point Helmut, thank you!

Tommy

Post #1079106
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse