Quantcast
Channel: SQL Server Replication forum
Viewing all articles
Browse latest Browse all 4054

Transactional Replication - Generate a snapshot for a new art only

$
0
0

I will apreciate any help on this, thanks ahead!

We are running the below script that works well for us at dev and other environments but when running on prod the generated snapshot is for all articles in the publication rather than the desired results, for the new art only.

I have copied below the code being used.

SET NOCOUNT ON; SET TRANSACTION ISOLATION LEVEL READ COMMITTED;

DECLARE @rc int, @publication sysname, @article sysname, @subscriber sysname, @destination_db sysname
			 ,@delete_article_from_replication_configuration bit ,@debug bit = 1;

SELECT @publication			= N'MyPub'
			,@destination_db	= N'dest_database'
			,@subscriber 			= N'MyServer'
SELECT @article					= N'MyArt'


-- SET immediate_sync and allow_anonymous to false
EXEC sp_changepublication
			@publication	= @publication,
			@property			= N'immediate_sync',
			@value				= N'false';

EXEC sp_changepublication
			@publication = @publication,
			@property		 = N'allow_anonymous',
			@value			 = N'false';


-- add article
DECLARE @error_message nvarchar(4000);
IF NOT EXISTS (SELECT * FROM dbo.sysarticles a INNER JOIN dbo.syspublications p ON a.pubid = p.pubid WHERE a.name = @article AND p.name = @publication)
BEGIN;
EXEC @rc = sp_addarticle
		 @publication					= @publication
		,@article							= @article
		,@source_owner				= N'dbo'
		,@source_object				= @article
		,@destination_table		= @article
		,@type								= N'logbased'
		,@creation_script			= null
		,@description					= null
		,@pre_creation_cmd		= N'none'

		,@schema_option				= 0x000000000803100D /* 0x000000000803FFDF */

		,@status							= 16 /* 8 */
		,@vertical_partition	= N'false'
		,@ins_cmd 						= N'SQL'
		,@del_cmd 						= N'SQL'
		,@upd_cmd 						= N'SQL'
		,@filter							= null
		,@sync_object					= null
		,@auto_identity_range	= N'false'
		,@identityrangemanagementoption = N'manual';
IF ( (@@ERROR <> 0) OR (@rc <> 0) )
BEGIN;
	SELECT @error_message = ERROR_MESSAGE(); RAISERROR(@error_message, 16, 1);
	IF (@@TRANCOUNT > 0) ROLLBACK TRAN; RETURN;
END;
PRINT 'The article ''' + @article + ''' has been added to publication ''' + @publication + '''';
END;


-- add subscription
IF NOT EXISTS (SELECT * from syssubscriptions WHERE dest_db NOT LIKE 'virtual' AND srvname LIKE @subscriber AND artid IN
		(SELECT artid FROM dbo.sysarticles a INNER JOIN dbo.syspublications p ON a.pubid = p.pubid WHERE a.name = @article AND p.name = @publication ))
BEGIN;
EXEC @rc = sp_addsubscription
			 @publication					= @publication
			,@subscriber					= @subscriber
			,@destination_db			= @destination_db
			,@subscription_type 	= N'Pull'

			,@sync_type						= N'automatic'
			--,@sync_type						= N'replication support only'

			,@article							= @article
			,@update_mode					= N'read only'
			,@subscriber_type			= 0
			,@subscriptionstreams = 4;
IF ( (@@ERROR <> 0) OR (@rc <> 0) )
BEGIN;
	SELECT @error_message = ERROR_MESSAGE(); RAISERROR(@error_message, 16, 1);
	IF (@@TRANCOUNT > 0) ROLLBACK TRAN; RETURN;
END;
PRINT 'The subscription ''' + @subscriber + ''' for article ''' + @article + ''' has been created''';
END;


EXEC sp_changepublication
				 @publication								= @publication
				,@property									= N'sync_method'
				,@value											= N'native'
				,@force_invalidate_snapshot = 0
				,@force_reinit_subscription = 0;

-- create snapshot
EXEC sp_addpublication_snapshot
				 @publication										= @publication
				,@frequency_type								= 1
				,@frequency_interval						= 1
				,@frequency_relative_interval 	= 0
				,@frequency_recurrence_factor 	= 0
				,@frequency_subday							= 0
				,@frequency_subday_interval			= 0
				,@active_start_time_of_day			= 0
				,@active_end_time_of_day				= 235959
				,@active_start_date							= 0
				,@active_end_date								= 0;



Yaniv Etrogi
site | blog | linked in | mail
Please click the Mark as Answer button if a post solves your problem! orVote As Helpful


Viewing all articles
Browse latest Browse all 4054

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>