Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Applies to:
SQL Server
Azure SQL Database
This topic describes how to validate data at the Subscriber in SQL Server by using SQL Server Management Studio, Transact-SQL, or Replication Management Objects (RMO).
Transactional and merge replication allow you to validate that data at the Subscriber matches data at the Publisher. Validation can be performed for specific subscriptions or for all subscriptions to a publication. Specify one of the following validation types and the Distribution Agent or Merge Agent will validate data the next time it runs:
In addition to validating that data at the Subscriber and Publisher match, merge replication provides the ability to validate that data is partitioned correctly for each Subscriber. For more information, see Validate Partition Information for a Merge Subscriber.
Note
Azure SQL Managed Instance can be a publisher, distributor, and subscriber for snapshot and transactional replication. Databases in Azure SQL Database can only be push subscribers for snapshot and transactional replication. For more information, see Transactional replication with Azure SQL Database and Azure SQL Managed Instance.
SQL Server validates data by calculating a row count or a checksum at the Publisher and then comparing those values to the row count or checksum calculated at the Subscriber. One value is calculated for the entire publication table and one value is calculated for the entire subscription table, but data in text, ntext, or image columns is not included in the calculations.
While the calculations are performed, shared locks are placed temporarily on tables for which row counts or checksums are being run, but the calculations are completed quickly and the shared locks removed, usually in a matter of seconds.
When binary checksums are used, 32-bit redundancy check (CRC) occurs on a column-by-column basis rather than a CRC on the physical row on the data page. This allows the columns with the table to be in any order physically on the data page, but still compute to the same CRC for the row. Binary checksum validation can be used when there are row or column filters on the publication.
Validating data is a three-part process:
A single subscription or all subscriptions to a publication are marked for validation. Mark subscriptions for validation in the Validate Subscription, Validate Subscriptions, and Validate All Subscriptions dialog boxes, which are available from the Local Publications folder and the Local Subscriptions folder in Microsoft SQL Server Management Studio. You can also mark subscriptions from the All Subscriptions tab, the Subscription Watch List tab, and the publications node in Replication Monitor. For information about starting Replication Monitor, see Start the Replication Monitor.
A subscription is validated the next time it is synchronized by the Distribution Agent (for transactional replication) or the Merge Agent (for merge replication). The Distribution Agent typically runs continuously, in which case validation occurs immediately; the Merge Agent typically runs on demand, in which case validation occurs after you run the agent.
View the validation results:
Take the following issues into consideration when validating data:
When validation is complete, the Distribution Agent or Merge Agent logs messages regarding success or failure (replication does not report which rows failed). These messages can be viewed in SQL Server Management Studio, Replication Monitor, and replication system tables. The how-to topic listed above demonstrates how to run validation and view the results.
To handle validation failures, consider the following:
Configure the replication alert named Replication: Subscriber has failed data validation so that you are notified of the failure. For more information, see Configure Predefined Replication Alerts (SQL Server Management Studio).
Is the fact that validation failed an issue for your application? If the validation failure is an issue, manually update the data so that it is synchronized, or reinitialize the subscription:
Data can be updated using the tablediff utility. For more information about using this utility, see Compare Replicated Tables for Differences (Replication Programming).
For more information about reinitialization, see Reinitialize Subscriptions.
At the Publisher on the publication database, execute sp_publication_validation (Transact-SQL). Specify @publication
and one of the following values for @rowcount_only
:
Note
When you execute sp_publication_validation (Transact-SQL), sp_article_validation (Transact-SQL) is executed for each article in the publication. To successfully execute sp_publication_validation (Transact-SQL), you must have SELECT permissions on all columns in the published base tables.
(Optional) Start the Distribution Agent for each subscription if it is not already running. For more information, see Synchronize a Pull Subscription and Synchronize a Push Subscription.
Check the agent output for the result of the validation.
At the Publisher on the publication database, execute sp_article_validation (Transact-SQL). Specify @publication
, the name of the article for @article
, and one of the following values for @rowcount_only
:
Note
To successfully execute sp_article_validation (Transact-SQL), you must have SELECT permissions on all columns in the published base table.
(Optional) Start the Distribution Agent for each subscription if it is not already running. For more information, see Synchronize a Pull Subscription and Synchronize a Push Subscription.
Check the agent output for the result of the validation.
At the Publisher on the publication database, open an explicit transaction using BEGIN TRANSACTION (Transact-SQL).
At the Publisher on the publication database, execute sp_marksubscriptionvalidation (Transact-SQL). Specify the publication for @publication
, the name of the Subscriber for @subscriber
, and the name of the subscription database for @destination_db
.
(Optional) Repeat step 2 for each subscription being validated.
At the Publisher on the publication database, execute sp_article_validation (Transact-SQL). Specify @publication
, the name of the article for @article
, and one of the following values for @rowcount_only
:
Note
To successfully execute sp_article_validation (Transact-SQL), you must have SELECT permissions on all columns in the published base table.
At the Publisher on the publication database, commit the transaction using COMMIT TRANSACTION (Transact-SQL).
(Optional) Repeat steps 1 through 5 for each article being validated.
(Optional) Start the Distribution Agent if it is not already running. For more information, see Synchronize a Pull Subscription and Synchronize a Push Subscription.
Check the agent output for the result of the validation. For more information, see Validate Data at the Subscriber.
In Replication Monitor, expand a Publisher group in the left pane, and then expand a Publisher.
Right-click the publication for which you want to validate subscriptions, and then click Validate Subscriptions.
In the Validate Subscriptions dialog box, select which subscriptions to validate:
To specify the type of validation to perform (row count, or row count and checksum) click Validation Options, and then specify options in the Subscription Validation Options dialog box.
Select OK.
Click the All Subscriptions tab.
View validation results. For each push subscription:
Connect to the Publisher in SQL Server Management Studio, and then expand the server node.
Expand the Replication folder, and then expand the Local Publications folder.
Expand the publication for which you want to validate subscriptions, right-click the subscription, and then click Validate Subscription.
In the Validate Subscription dialog box, select Validate this subscription.
To specify the type of validation to perform (row count, or row count and checksum) click Options, and then specify options in the Subscription Validation Options dialog box.
Select OK.
View validation results in Replication Monitor or the View Synchronization Status dialog box:
If you do not see any messages regarding validation, the agent has already logged a subsequent message. In this case, view the validation results in Replication Monitor. For more information, see the Replication Monitor how to procedures in this topic.
Connect to the Publisher in SQL Server Management Studio, and then expand the server node.
Expand the Replication folder, and then expand the Local Publications folder.
Right-click the publication for which you want to validate subscriptions, and then click Validate All Subscriptions.
In the Validate All Subscriptions dialog box, specify the type of validation to perform (row count, or row count and checksum).
Select OK.
View validation results in Replication Monitor or the View Synchronization Status dialog box. For each subscription:
If you do not see any messages regarding validation, the agent has already logged a subsequent message. In this case, view the validation results in Replication Monitor. For more information, see the Replication Monitor how to procedures in this topic.
At the Publisher on the publication database, execute sp_validatemergesubscription (Transact-SQL). Specify @publication
, the name of the Subscriber for @subscriber
, the name of the subscription database for @subscriber_db
, and one of the following values for @level
:
This marks the selected subscription for validation.
Start the merge agent for each subscription. For more information, see Synchronize a Pull Subscription and Synchronize a Push Subscription.
Check the agent output for the result of the validation.
Repeat steps 1 through 3 for each subscription being validated.
Note
A subscription to a merge publication can also be validated at the end of a synchronization by specifying the -Validate parameter when running the Replication Merge Agent.
At the Publisher on the publication database, execute sp_validatemergepublication (Transact-SQL). Specify @publication
and one of the following values for @level
:
This marks all subscriptions for validation.
Start the merge agent for each subscription. For more information, see Synchronize a Pull Subscription and Synchronize a Push Subscription.
Check the agent output for the result of the validation. For more information, see Validate Data at the Subscriber.
Start the Merge Agent at the Subscriber (pull subscription) or at the Distributor (push subscription) from the command prompt in one of the following ways.
For more information, see Synchronize a Pull Subscription or Synchronize a Push Subscription.
Replication enables you to use Replication Management Objects (RMO) to programmatically validate that data at the Subscriber matches data at the Publisher. The objects you use depend on the type of replication topology. Transactional replication requires validation of all subscriptions to a publication.
Note
For an example, see Example (RMO), later in this section.
Create a connection to the Publisher by using the ServerConnection class.
Create an instance of the TransPublication class. Set the Name and DatabaseName properties for the publication. Set the ConnectionContext property to the connection created in step 1.
Call the LoadProperties method to get the remaining properties of the object. If this method returns false, either the publication properties in step 2 were defined incorrectly or the publication does not exist.
Call the ValidatePublication method. Pass the following:
A Boolean that indicates whether to stop the Distribution Agent after validation is completed.
This marks the articles for validation.
If not already running, start the Distribution Agent to synchronize each subscription. For more information, see Synchronize a Push Subscription or Synchronize a Pull Subscription. The result of the validation operation is written to the agent history. For more information, see Monitoring Replication.
Create a connection to the Publisher by using the ServerConnection class.
Create an instance of the MergePublication class. Set the Name and DatabaseName properties for the publication. Set the ConnectionContext property to the connection created in step 1.
Call the LoadProperties method to get the remaining properties of the object. If this method returns false, either the publication properties in step 2 were defined incorrectly or the publication does not exist.
Call the ValidatePublication method. Pass the desired ValidationOption.
Run the Merge Agent for each subscription to start validation, or wait for the next scheduled agent run. For more information, see Synchronize a Pull Subscription and Synchronize a Push Subscription. The result of the validation operation is written to the agent history, which you view by using Replication Monitor. For more information, see Monitoring Replication.
Create a connection to the Publisher by using the ServerConnection class.
Create an instance of the MergePublication class. Set the Name and DatabaseName properties for the publication. Set the ConnectionContext property to the connection created in step 1.
Call the LoadProperties method to get the remaining properties of the object. If this method returns false, either the publication properties in step 2 were defined incorrectly or the publication does not exist.
Call the ValidateSubscription method. Pass the name of the Subscriber and subscription database being validated and the desired ValidationOption.
Run the Merge Agent for the subscription to start validation, or wait for the next scheduled agent run. For more information, see Synchronize a Pull Subscription and Synchronize a Push Subscription. The result of the validation operation is written to the agent history, which you view by using Replication Monitor. For more information, see Monitoring Replication.
This example marks all subscriptions to a transactional publication for rowcount validation.
// Define the server, database, and publication names
string publisherName = publisherInstance;
string publicationName = "AdvWorksProductTran";
string publicationDbName = "AdventureWorks2022";
TransPublication publication;
// Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(publisherName);
try
{
// Connect to the Publisher.
conn.Connect();
// Set the required properties for the publication.
publication = new TransPublication();
publication.ConnectionContext = conn;
publication.Name = publicationName;
publication.DatabaseName = publicationDbName;
// If we can't get the properties for this publication,
// throw an application exception.
if (publication.LoadProperties())
{
// Initiate validataion for all subscriptions to this publication.
publication.ValidatePublication(ValidationOption.RowCountOnly,
ValidationMethod.ConditionalFast, false);
// If not already running, start the Distribution Agent at each
// Subscriber to synchronize and validate the subscriptions.
}
else
{
throw new ApplicationException(String.Format(
"Settings could not be retrieved for the publication. " +
"Ensure that the publication {0} exists on {1}.",
publicationName, publisherName));
}
}
catch (Exception ex)
{
// Do error handling here.
throw new ApplicationException(
"Subscription validation could not be initiated.", ex);
}
finally
{
conn.Disconnect();
}
' Define the server, database, and publication names
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksProductTran"
Dim publicationDbName As String = "AdventureWorks2022"
Dim publication As TransPublication
' Create a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(publisherName)
Try
' Connect to the Publisher.
conn.Connect()
' Set the required properties for the publication.
publication = New TransPublication()
publication.ConnectionContext = conn
publication.Name = publicationName
publication.DatabaseName = publicationDbName
' If we can't get the properties for this publication,
' throw an application exception.
If publication.LoadProperties() Then
' Initiate validataion for all subscriptions to this publication.
publication.ValidatePublication(ValidationOption.RowCountOnly, _
ValidationMethod.ConditionalFast, False)
' If not already running, start the Distribution Agent at each
' Subscriber to synchronize and validate the subscriptions.
Else
Throw New ApplicationException(String.Format( _
"Settings could not be retrieved for the publication. " + _
"Ensure that the publication {0} exists on {1}.", _
publicationName, publisherName))
End If
Catch ex As Exception
' Do error handling here.
Throw New ApplicationException( _
"Subscription validation could not be initiated.", ex)
Finally
conn.Disconnect()
End Try
This example marks a specific subscription to a merge publication for rowcount validation.
// Define the server, database, and publication names
string publisherName = publisherInstance;
string publicationName = "AdvWorksSalesOrdersMerge";
string publicationDbName = "AdventureWorks2022";
string subscriberName = subscriberInstance;
string subscriptionDbName = "AdventureWorks2022Replica";
MergePublication publication;
// Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(publisherName);
try
{
// Connect to the Publisher.
conn.Connect();
// Set the required properties for the publication.
publication = new MergePublication();
publication.ConnectionContext = conn;
publication.Name = publicationName;
publication.DatabaseName = publicationDbName;
// If we can't get the properties for this merge publication, then throw an application exception.
if (publication.LoadProperties())
{
// Initiate validation of the specified subscription.
publication.ValidateSubscription(subscriberName,
subscriptionDbName, ValidationOption.RowCountOnly);
// Start the Merge Agent to synchronize and validate the subscription.
}
else
{
throw new ApplicationException(String.Format(
"Settings could not be retrieved for the publication. " +
"Ensure that the publication {0} exists on {1}.",
publicationName, publisherName));
}
}
catch (Exception ex)
{
// Do error handling here.
throw new ApplicationException(String.Format(
"The subscription at {0} to the {1} publication could not " +
"be validated.", subscriberName, publicationName), ex);
}
finally
{
conn.Disconnect();
}
' Define the server, database, and publication names
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publicationDbName As String = "AdventureWorks2022"
Dim subscriberName As String = subscriberInstance
Dim subscriptionDbName As String = "AdventureWorks2022Replica"
Dim publication As MergePublication
' Create a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(publisherName)
Try
' Connect to the Publisher.
conn.Connect()
' Set the required properties for the publication.
publication = New MergePublication()
publication.ConnectionContext = conn
publication.Name = publicationName
publication.DatabaseName = publicationDbName
' If we can't get the properties for this merge publication, then throw an application exception.
If publication.LoadProperties() Then
' Initiate validation of the specified subscription.
publication.ValidateSubscription(subscriberName, _
subscriptionDbName, ValidationOption.RowCountOnly)
' Start the Merge Agent to synchronize and validate the subscription.
Else
Throw New ApplicationException(String.Format( _
"Settings could not be retrieved for the publication. " + _
"Ensure that the publication {0} exists on {1}.", _
publicationName, publisherName))
End If
Catch ex As Exception
' Do error handling here.
Throw New ApplicationException(String.Format( _
"The subscription at {0} to the {1} publication could not " + _
"be validated.", subscriberName, publicationName), ex)
Finally
conn.Disconnect()
End Try
Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayTraining
Learning path
Use advance techniques in canvas apps to perform custom updates and optimization - Training
Use advance techniques in canvas apps to perform custom updates and optimization