title | description | author | ms.author | ms.date | ms.service | ms.subservice | ms.topic | ms.custom | helpviewer_keywords | ||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Define logical record relationship between Merge table articles |
Learn how to define the logical record relationship between related tables used for Merge Replication articles. |
MashaMSFT |
mathoma |
09/25/2024 |
sql |
replication |
how-to |
|
|
[!INCLUDE SQL Server] This topic describes how to define a logical record relationship between merge table articles in [!INCLUDEssnoversion] by using [!INCLUDEssManStudioFull], [!INCLUDEtsql], or Replication Management Objects (RMO).
Merge replication allows you to define a relationship between related rows in different tables. These rows can then be processed as a transactional unit during synchronization. A logical record can be defined between two articles whether or not they have a join filter relationship. For more information, see Group Changes to Related Rows with Logical Records.
Note
[!INCLUDEssNoteDepFutureAvoid]
In This Topic
-
Before you begin:
-
To define a logical record relationship between merge table articles, using:
- If you add, modify, or delete a logical record after subscriptions to the publication have been initialized, you must generate a new snapshot and reinitialize all subscriptions after making the change. For more information about requirements for property changes, see Change Publication and Article Properties.
Define logical records in the Add Join dialog box, which is available in the New Publication Wizard and the Publication Properties - <Publication> dialog box. For more information about using the wizard and accessing the dialog box, see Create a Publication and View and Modify Publication Properties.
Logical records can be defined in the Add Join dialog box only if they are applied to a join filter in a merge publication, and the publication follows the requirements for using precomputed partitions. To define logical records that are not applied to join filters and to set conflict detection and resolution at the logical record level, you must use stored procedures.
-
On the Filter Table Rows page of the New Publication Wizard or the Filter Rows page of the Publication Properties - <Publication> dialog box, select a row filter in the Filtered Tables pane.
A logical record relationship is associated with a join filter, which extends a row filter. Therefore you must define a row filter before you can extend the filter with a join and apply a logical record relationship. After one join filter is defined, you can extend this join filter with another join filter. For more information about defining join filters, see Define and Modify a Join Filter Between Merge Articles.
-
Click Add, and then click Add Join to Extend the Selected Filter.
-
Define a join filter in the Add Join dialog box, and then select the check box Logical Record.
-
If you are in the Publication Properties - <Publication> dialog box, click OK to save and close the dialog box.
-
Delete only the logical record relationship or delete the logical record relationship and the join filter associated with it.
To delete only the logical record relationship:
-
On the Filter Rows page of the New Publication Wizard or the Filter Rows page of the Publication Properties - <Publication> dialog box, select the join filter associated with the logical record relationship in the Filtered Tables pane, and then click Edit.
-
In the Edit Join dialog box, clear the check box Logical Record.
-
Select OK.
To delete the logical record relationship and join filter associated with it:
- On the Filter Rows page of the New Publication Wizard or Publication Properties - <Publication> dialog box, select a filter in the Filtered Tables pane, and then click Delete. If the join filter you delete is itself extended by other joins, those joins will also be deleted.
-
You can programmatically specify logical record relationships between articles using replication stored procedures.
-
If the publication contains any articles that are filtered, execute sp_helpmergepublication, and note the value of use_partition_groups in the result set.
-
If the value is 1, then precomputed partitions are already being used.
-
If the value is 0, then execute sp_changemergepublication at the Publisher on the publication database. Specify a value of use_partition_groups for @property and a value of true for @value.
[!NOTE]
If the publication does not support precomputed partitions, then logical records cannot be used. For more information, see Requirements for Using Precomputed Partitions in the topic Optimize Parameterized Filter Performance with Precomputed Partitions. -
If the value is NULL, then the Snapshot Agent needs to be run to generate the initial snapshot for the publication.
-
-
If the articles that will comprise the logical record do not exist, execute sp_addmergearticle at the Publisher on the publication database. Specify one of the following conflict detection and resolution options for the logical record:
-
To detect and resolve conflicts that occur within related rows in the logic record, specify a value of true for @logical_record_level_conflict_detection and @logical_record_level_conflict_resolution.
-
To use the standard row- or column-level conflict detection and resolution, specify a value of false for @logical_record_level_conflict_detection and @logical_record_level_conflict_resolution, which is the default.
-
-
Repeat step 2 for each article that will comprise the logical record. You must use the same conflict detection and resolution option for each article in the logical record. For more information, see Detecting and Resolving Conflicts in Logical Records.
-
At the publisher on the publication database, execute sp_addmergefilter. Specify @publication, the name of one article in the relationship for @article, the name of the second article for @join_articlename, a name for the relationship for @filtername, a clause that defines the relationship between the two articles for @join_filterclause, the type of join for @join_unique_key and one of the following values for @filter_type:
-
2 - Defines a logical relationship.
-
3 - Defines a logical relationship with a join filter.
[!NOTE]
If a join filter is not used, the direction of the relationship between the two articles is not important. -
-
Repeat step 2 for each remaining logical record relationship in the publication.
-
To detect and resolve conflicts that occur within related rows in the logical record:
-
At the Publisher on the publication database, execute sp_changemergearticle. Specify a value of logical_record_level_conflict_detection for @property and a value of true for @value. Specify a value of 1 for @force_invalidate_snapshot and @force_reinit_subscription.
-
At the Publisher on the publication database, execute sp_changemergearticle. Specify a value of logical_record_level_conflict_resolution for @property and a value of true for @value. Specify a value of 1 for @force_invalidate_snapshot and @force_reinit_subscription.
-
-
To use the standard row-level or column-level conflict detection and resolution:
-
At the Publisher on the publication database, execute sp_changemergearticle. Specify a value of logical_record_level_conflict_detection for @property and a value of false for @value. Specify a value of 1 for @force_invalidate_snapshot and @force_reinit_subscription.
-
At the Publisher on the publication database, execute sp_changemergearticle. Specify a value of logical_record_level_conflict_resolution for @property and a value of false for @value. Specify a value of 1 for @force_invalidate_snapshot and @force_reinit_subscription.
-
-
At the Publisher on the publication database, execute the following query to return information about all logical record relationships defined for the specified publication:
:::code language="sql" source="../codesnippet/tsql/define-a-logical-record-_1.sql":::
Note the name of the logical record relationship being removed in the filtername column in the result set.
[!NOTE]
This query returns the same information as sp_helpmergefilter; however, this system stored procedure only returns information about logical record relationships that are also join filters. -
At the Publisher on the publication database, execute sp_dropmergefilter. Specify @publication, the name of one of the articles in the relationship for @article, and the name of the relationship from step 1 for @filtername.
This example enables precomputed partitions on an existing publication, and creates a logical record comprising the two new articles for the SalesOrderHeader
and SalesOrderDetail
tables.
:::code language="sql" source="../codesnippet/tsql/define-a-logical-record-_2.sql":::
Note
Merge replication allows you to specify that conflicts be tracked and resolved at the logical record level, but these options cannot be set using RMO.
-
Create a connection to the Publisher by using the xref:Microsoft.SqlServer.Management.Common.ServerConnection class.
-
Create an instance of the xref:Microsoft.SqlServer.Replication.MergePublication class, set the xref:Microsoft.SqlServer.Replication.Publication.Name%2A and xref:Microsoft.SqlServer.Replication.Publication.DatabaseName%2A properties for the publication, and set the xref:Microsoft.SqlServer.Replication.ReplicationObject.ConnectionContext%2A property to the connection created in step 1.
-
Call the xref:Microsoft.SqlServer.Replication.ReplicationObject.LoadProperties%2A method to get the 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.
-
If the xref:Microsoft.SqlServer.Replication.MergePublication.PartitionGroupsOption%2A property is set to xref:Microsoft.SqlServer.Replication.PartitionGroupsOption.False, set it to xref:Microsoft.SqlServer.Replication.PartitionGroupsOption.True.
-
If the articles that are to comprise the logical record do not exist, create an instance of the xref:Microsoft.SqlServer.Replication.MergeArticle class, and set the following properties:
-
The name of the article for xref:Microsoft.SqlServer.Replication.Article.Name%2A.
-
The name of the publication for xref:Microsoft.SqlServer.Replication.Article.PublicationName%2A.
-
(Optional) If the article is horizontally filtered, specify the row filter clause for the xref:Microsoft.SqlServer.Replication.MergeArticle.FilterClause%2A property. Use this property to specify a static or parameterized row filter. For more information, see Parameterized Row Filters.
For more information, see Define an Article.
-
-
Call the xref:Microsoft.SqlServer.Replication.Article.Create%2A method.
-
Repeat steps 5 and 6 for each article comprising the logical record.
-
Create an instance of the xref:Microsoft.SqlServer.Replication.MergeJoinFilter class to define the logical record relationship between articles. Then, set the following properties:
-
The name of the child article in the logical record relationship for the xref:Microsoft.SqlServer.Replication.MergeJoinFilter.ArticleName%2A property.
-
The name of the existing, parent article in the logical record relationship for the xref:Microsoft.SqlServer.Replication.MergeJoinFilter.JoinArticleName%2A property.
-
A name for the logical record relationship for the xref:Microsoft.SqlServer.Replication.MergeJoinFilter.FilterName%2A property.
-
The expression that defines the relationship for the xref:Microsoft.SqlServer.Replication.MergeJoinFilter.JoinFilterClause%2A property.
-
A value of xref:Microsoft.SqlServer.Replication.FilterTypes.LogicalRecordLink for the xref:Microsoft.SqlServer.Replication.MergeJoinFilter.FilterTypes%2A property. If the logical record relationship is also a join filter, specify a value of xref:Microsoft.SqlServer.Replication.FilterTypes.JoinFilterAndLogicalRecordLink for this property. For more information, see Group Changes to Related Rows with Logical Records.
-
-
Call the xref:Microsoft.SqlServer.Replication.MergeArticle.AddMergeJoinFilter%2A method on the object that represents the child article in the relationship. Pass the xref:Microsoft.SqlServer.Replication.MergeJoinFilter object from step 8 to define the relationship.
-
Repeat steps 8 and 9 for each remaining logical record relationship in the publication.
This example creates a logical record comprising the two new articles for the SalesOrderHeader
and SalesOrderDetail
tables.
[!code-csHowTo#rmo_CreateLogicalRecord]
[!code-vbHowTo#rmo_vb_CreateLogicalRecord]