Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Query to create a pull subscripton for SQL Server replication
USE [Database];
-- Execute this batch at the Subscriber.
DECLARE @iPublisher AS sysname;
DECLARE @iPublication AS sysname;
DECLARE @iPublicationDB AS sysname;
DECLARE @iSubscriber AS sysname;
DECLARE @iSubscriptionDB AS sysname;
SET @iPublisher = N'PublisherName';
SET @iPublication = N'PublicationName';
SET @iPublicationDB = N'PublisherDatabase';
SET @iSubscriber = @@SERVERNAME;
SELECT @iSubscriptionDB = DB_NAME();
-- At the subscriber database, create a pull subscription to a transactional or snapshot publication.
EXEC sys.sp_addpullsubscription @publisher = @iPublisher,
@publication = @iPublication,
@publisher_db = @iPublicationDB;
--Execute the outputed query at the Publisher to register the subscriber
SELECT 'EXEC sys.sp_addsubscription @publication = ''' + @iPublication + ''', @subscriber = ''' + @iSubscriber
+ ''', @destination_db = ''' + @iSubscriptionDB
+ ''', @subscription_type = N''pull'', @status = N''subscribed'';' AS 'Run on publisher';
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment