Tuesday, May 3, 2016

Add Articles to existing publication(transactional replication) with out generating whole snapshot



--========================================================================
-- GET A LIST OF SUBSCRIBERS
 select
db_name() PublisherDB
, sp.name as PublisherName
, sa.name as TableName
, UPPER(srv.srvname) as SubscriberServerName
from dbo.syspublications sp
join dbo.sysarticles sa on sp.pubid = sa.pubid
join dbo.syssubscriptions s on sa.artid = s.artid
join master.dbo.sysservers srv on s.srvid = srv.srvid

exec sp_helppublication 'DB_pub'
GO
--Check if allow_anonymous=0,immediate_sync=0 or or not if not zero run below command

--Before running the sp_refreshsubscriptions SP, make sure that the publisher properties "allow_anonymous" and "immediate_sync" are set to "False",
-- if these 2 options are set to "True" then this SP will mark all the articles for
--  generating snapshot instead of marking only the newly added articles.


EXEC sp_changepublication
@publication = 'DBpub',
@property = N'allow_anonymous',
@value = 'false'
GO
EXEC sp_changepublication
@publication = 'DB_pub',
@property = N'immediate_sync',
@value = 'false'
GO


--Then add artciles

EXEC sp_addarticle @publication = N'DB_pub',
                   @article = N'LeadSourceGroup',
                   @source_object=N'LeadSourceGroup'



--========================================================================
-- Only add the subscribers if they don't exist
--========================================================================
--EXEC sp_addsubscription
--  @publication = N'mydatabase',
--   @article = N'OpenInvoiceAddress',
--  @subscriber = N'my_subscriber_01',
--  @destination_db = N'mydatabase'
--GO

-- EXEC sp_addsubscription
--  @publication = N'mydatabase',
--   @article = N'OpenInvoiceAddress',
--  @subscriber = N'SQLmy_subscriber_02',
--  @destination_db = N'mydatabase'
--GO

--Should on poublisher
EXEC sp_refreshsubscriptions @publication = N'db_pub'
GO
-- Start the Snapshot Agent job.This will only generetae snapshot for
--add 1 table or any tables adeed
EXEC sp_startpublication_snapshot @publication = N'DB_pub'
go

----MUST
--enable back
EXEC sp_changepublication
@publication = 'DB_pub',
@property = N'allow_anonymous',
@value = 'True'
GO
EXEC sp_changepublication
@publication = N'DB_pub,
@property = N'immediate_sync',
@value = 'true'
GO

https://blog.sqlauthority.com/2009/06/27/sql-server-fix-error-17892-logon-failed-for-login-due-to-trigger-execution-changed-database-context...