Search code examples
sql-server-2008t-sqlreplicationsubscriptionmerge-replication

Delete a Push Subscription from Subscriber using T-SQL in Merge Replication


I have a Publisher server and Subscriber Server which are synchronized using Merge replication. When i using wizard to remove Replication a login window ask me to login on Subscriber Server and remove its subscription but when I using generated script for removing Replication, the subscription in subscriber won't remove. How can I do it using TSQL? In Other Word: I want to Connect to subscriber and delete record of subscription at subscriber also using T-SQL


Solution

  • At last I found the solution of my problem:

    I used Linked Server to create a connection to remote server:

    exec sp_addlinkedserver 'RemoteServer\PeerInstance', N'SQL Server'
    
    exec sp_addlinkedsrvlogin 'RemoteServer\PeerInstant', 'true', 'sa', 'password'
    
    exec ReplicatedDBOnMainServer.sys.sp_dropmergesubscription
    @publication = N'PublicationName', 
    @subscriber = N'RemoteServer\PeerInstant'
    
    exec [RemoteServer\PeerInstant].ReplicatedDBOnRemote.sys.sp_mergesubscription_cleanup
    @publisher = N'MainServer\MainInstant', 
    @publisher_db = N'ReplicatedDBOnMainServer'
    @publication = N'PublicationName'