Skip to content

Instantly share code, notes, and snippets.

Created April 28, 2017 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/59cbf198abe06612c6aa722b987ce55c to your computer and use it in GitHub Desktop.
Save anonymous/59cbf198abe06612c6aa722b987ce55c to your computer and use it in GitHub Desktop.
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