To customize the merge process, you'll need to mplementimplement a custom business logic handler (requires .NET code - cannot be implemented using TSQL):. See https://msdn.microsoft.com/en-us/library/ms152495(v=sql.110).aspxExecute Business Logic During Merge Synchronization in the documentation. It is available in SQL Server 2012 and later.
Override methods Update, Insert and commitCommit to transform changed rows, and conflict resolver method to modify conflict logic. Can't use TSQLT-SQL, must be done with .NET code. The main limitation is that each overridable method is called once for every row (like SSIS row transformations), to the best of my knowledge there isn't a method called on all changed rows.
Applicable to any version 2012 +.
There are some work aroundsworkarounds to handle your case, but this the general solution.
This is the BOL c#C# template.:
using System; using System.Text; using System.Data; using System.Data.Common; using Microsoft.SqlServer.Replication.BusinessLogicSupport; using Microsoft.Samples.SqlServer.BusinessLogicHandler; namespace Microsoft.Samples.SqlServer.BusinessLogicHandler { public class OrderEntryBusinessLogicHandler : Microsoft.SqlServer.Replication.BusinessLogicSupport.BusinessLogicModule { // Variables to hold server names. private string publisherName; private string subscriberName; public OrderEntryBusinessLogicHandler() { } // Implement the Initialize method to get publication // and subscription information. public override void Initialize( string publisher, string subscriber, string distributor, string publisherDB, string subscriberDB, string articleName) { // Set the Publisher and Subscriber names. publisherName = publisher; subscriberName = subscriber; } // Declare what types of row changes, conflicts, or errors to handle. override public ChangeStates HandledChangeStates { get { // Handle Subscriber inserts, updates and deletes. return ChangeStates.SubscriberInserts | ChangeStates.SubscriberUpdates | ChangeStates.SubscriberDeletes; } } public override ActionOnDataChange InsertHandler(SourceIdentifier insertSource, DataSet insertedDataSet, ref DataSet customDataSet, ref int historyLogLevel, ref string historyLogMessage) { // custom logic for updates } public override ActionOnDataChange UpdateHandler(SourceIdentifier updateSource, DataSet updatedDataSet, ref DataSet customDataSet, ref int historyLogLevel, ref string historyLogMessage) { // custom logic for updates } public override ActionOnDataDelete DeleteHandler(SourceIdentifier deleteSource, DataSet deletedDataSet, ref int historyLogLevel, ref string historyLogMessage) { // custom logic for deletes } } using System; using System.Text; using System.Data; using System.Data.Common; using Microsoft.SqlServer.Replication.BusinessLogicSupport; using Microsoft.Samples.SqlServer.BusinessLogicHandler; namespace Microsoft.Samples.SqlServer.BusinessLogicHandler { public class OrderEntryBusinessLogicHandler : Microsoft.SqlServer.Replication.BusinessLogicSupport.BusinessLogicModule { // Variables to hold server names. private string publisherName; private string subscriberName; public OrderEntryBusinessLogicHandler() { } // Implement the Initialize method to get publication // and subscription information. public override void Initialize( string publisher, string subscriber, string distributor, string publisherDB, string subscriberDB, string articleName) { // Set the Publisher and Subscriber names. publisherName = publisher; subscriberName = subscriber; } // Declare what types of row changes, conflicts, or errors to handle. override public ChangeStates HandledChangeStates { get { // Handle Subscriber inserts, updates and deletes. return ChangeStates.SubscriberInserts | ChangeStates.SubscriberUpdates | ChangeStates.SubscriberDeletes; } } public override ActionOnDataChange InsertHandler(SourceIdentifier insertSource, DataSet insertedDataSet, ref DataSet customDataSet, ref int historyLogLevel, ref string historyLogMessage) { // custom logic for updates } public override ActionOnDataChange UpdateHandler(SourceIdentifier updateSource, DataSet updatedDataSet, ref DataSet customDataSet, ref int historyLogLevel, ref string historyLogMessage) { // custom logic for updates } public override ActionOnDataDelete DeleteHandler(SourceIdentifier deleteSource, DataSet deletedDataSet, ref int historyLogLevel, ref string historyLogMessage) { // custom logic for deletes } }