Showing posts with label Replication. Show all posts
Showing posts with label Replication. Show all posts

Wednesday, 1 July 2020

Add new Article to existing Transnational Replication using Script


Add Article to existing Publication

To add the new (articles/SP/Views/Functions) to Transactional replication,  Don't not use GUI to add articles (including Tables, views, SP’s and Functions), always use sp_addarticle and sp_addsubscription like following scripts.

                   

Make sure that your publication has IMMEDIATE_SYNC and ALLOW_ANONYMOUS properties set to FALSE or 0.
use JAIVERMA_DB

select immediate_sync , allow_anonymous from syspublications

If either of them is TRUE then modify that to FALSE by using the following command
EXEC sp_changepublication
@publication = My-Replication',
@property = 'allow_anonymous' ,
@value = 'false'
GO

EXEC sp_changepublication
@publication = My-Replication',
s@property = 'immediate_sync' ,
@value = 'false'


Adding the Tables to Replication -

1)    Add the article to the publication


use JAIVERMA_DB

use [JAIVERMA_DB]
exec sp_addarticle @publication = N’MY-Replication’, @article = N'New_Table_name', @source_object = N'New_Table_name', @force_invalidate_snapshot=1
GO



2)     Add the subscription for this new article


use [JAIVERMA_DB]
exec sp_addsubscription @publication = N’MY-Replication’, @subscriber = N’SECONDARYSERVERNAME’, @destination_db = N'JAIVERMA_DB', @article = N'New_Table_name',  @reserved='Internal', @subscription_type = N'Pull'
GO


use [JAIVERMA_DB]
exec sp_addsubscription @publication = N’MY-Replication’, @subscriber = N'GPC-INISQDB06', @destination_db = N'JAIVERMA_DB', @article = N' New_Table_name',  @reserved='Internal', @subscription_type = N'Pull'
GO



Adding the SP to Replication -
1)      Adding the SP to publication

use [JAIVERMA_DB]
exec sp_addarticle @publication = N’MY-Replication’, @article = N'New_SP_Name', @source_object = N'New_SP_name', @type = N'proc schema only', @destination_table = N'New_SP_name',  @force_invalidate_snapshot=1
GO

2)      Add the subscription for this new SP


use [JAIVERMA_DB]
exec sp_addsubscription @publication = N’MY-Replication’, @subscriber = N’SECONDARYSERVERNAME’, @destination_db = N'JAIVERMA_DB', @article = N' New_SP_Name'',  @reserved='Internal', @subscription_type = N'Pull'
GO






Adding the View to existing replication

1)      Adding the View to publication


use [JAIVERMA_DB]
exec sp_addarticle @publication = N’MY-Replication’,
@article = N'New_View_name',
@source_object = N'New_View_name',
@type = N'view schema only',
@destination_table = N'New_View_name',
@force_invalidate_snapshot=1
GO


2)      Add the subscription for this new view

use [JAIVERMA_DB]
exec sp_addsubscription @publication = N’MY-Replication’, @subscriber = N’SECONDARYSERVERNAME’, @destination_db = N'JAIVERMA_DB', @article = N'New_View_name', @reserved='Internal', @subscription_type = N'Pull'
GO




Adding the User Defined Functions to existing replication

1)      Adding the User Defined Functions to publication

use [JAIVERMA_DB]

exec sp_addarticle @publication = N’MY-Replication’,
@article = N'New_Function_name',
@source_object = N'New_Function_name',
@type = N'func schema only',
@destination_table = N'New_Function_name',
@force_invalidate_snapshot=1
GO


2)      Add the subscription for this new Function

use [JAIVERMA_DB]

exec sp_addsubscription @publication = N’MY-Replication’, @subscriber = N’SECONDARYSERVERNAME’, @destination_db = N'JAIVERMA_DB', @article = N'New_Function_name',  @reserved='Internal', @subscription_type = N'Pull'
GO






3)    Lastly start the SNAPSHOT AGENT job from the job activity monitor. Verify that the snapshot was generated for only newly added articles . 







Wednesday, 8 April 2020

How to Add Article (Order_table_2) again in Transnational Replication and snapshot agent should not run for all the existing articles



Issue 1: Subscriber end article rows are more than Publisher, how to fix this issue.

Issue 2: There are two tables (Order_table_1 & Order_table_2) And user want and in both table column name “Status” should have same value. Publisher side it is showing same value for both tables but in subscriber side it is having the issue. Where the Order_table_2 have correct values for status column.

Issue 3: How to remove existing article (Order_table) from transactional replication and ADD Article (Coustomer_table) again but should not run snapshot agent for all the existing articles.

Issue 4: How to Add Article (Order_table_1) again but should not run snapshot agent for all the existing articles.

Solution:

1.     Data validation: Run the query on Publisher end and subscriber end to find out the data mis-match between two table
--Run below query total count on publisher and both subscriber.
SELECT S.STATUS ,P.STATUS, * FROM Order_table_1 S
INNER JOIN Order_table_2  P
ON S.ID=P.ID
WHERE S.STATUS <> P.STATUS

       --Run below query total count on on publisher and both subscriber.
         select count(*) from [dbo].Order_table_1

2.      Check the immediate_sync , allow_anonymous values. By default these value set as 1 and we need to make them 0

       --Ran this on publisher server
          sp_helppublication


        -- Or ran this on Distributor server
            select immediate_sync,allow_anonymous,* from distribution.dbo.MSpublications

3.     Set immediate_sync , allow_anonymous as 0 (Zero)

--Run on your publisher database to change the value as 0 for immediate_sync,allow_anonymous

EXEC sp_changepublication
@publication = 'IND-Replication',
@property = 'allow_anonymous' ,
@value = 'false'
GO
EXEC sp_changepublication
@publication = 'IND-Replication',
@property = 'immediate_sync' ,
@value = 'false'
GO

4.     To remove existing Article from replication
A.       Go Publisher server à Open Replication à Go to replication property à Click on Articles à Uncheck the Article which you want to remove à Click on OK.
B.       Now connect to Subscriber server à Select the replication database  à Drop the article which you want to remove from replication.

5.     To add Article in existing replication

--Run below query to add article on replication database< Display_Expand_database> at publisher end, Make sure change the @publication value, @article and @source_ object

USE Display_Expand_database
EXEC sp_addarticle
@publication = IND-Replication',
@article = Order_table_1,
@source_object = Order_table_1

6.     To Add subscription on subscriber end in existing replication

--Run below query at publisher end to add subscription on replication, Make sure change the @publication value, @destination_db.
Note- We have two subscriber so below script is for both subscriber.

EXEC sp_addsubscription
@publication = IND-Replication',
@subscriber = 'IND_SQLDB01',
@destination_db = 'Display_Expand_database'

EXEC sp_addsubscription
@publication = 'IND-Replication',
@subscriber = 'IND_SQLDB02',
@destination_db = 'Display_Expand_database'



7.     Now open the replication monitor à expend “My Publisher” Click on Replication Name  à Click on “Agent” Tab à See the job name as “Snapshot agent” à Right click on “Snapshot agent” and click on “Start Agent” à See the Last action (It will show the progress of snapshot for only Newly added article.)



8.     Data validation: Now match the total row count and check the mis-match values. It will be same now on publisher and both subscriber end.

       --Run below query total count on on publisher and both subscriber.
select count(*) from [dbo].Order_table_1

--Run below query total count on publisher and both subscriber.
SELECT S.STATUS ,P.STATUS, * FROM Order_table_1 S
INNER JOIN Order_table_2  P
ON S.ID=P.ID
WHERE S.STATUS <> P.STATUS