如何从Microsoft Sql Server数据库中删除所有事务发布?

时间:2014-07-15 14:46:14

标签: sql-server transactional-replication

我的数据库有很多使用Sql Server复制的许多表的订阅者。

当我尝试删除表或删除数据库中参与已发布数据库订阅的列时,数据库迁移失败。

这些订阅者检查他们的发布是否仍然有效,如果没有,他们会重新订阅。

我需要通过T-SQL从当前数据库中删除所有发布,因此当我自动执行数据库迁移时,删除列或删除表而不会导致迁移失败并需要手动干预。

2 个答案:

答案 0 :(得分:4)

要从服务器删除所有事务发布,请在所连接的数据库上运行以下脚本:

declare @PublicationName varchar(max)
declare @ArticleName varchar(max)
declare @SubscriberServerName varchar(max)
declare @DestinationDb varchar(max)

IF OBJECT_ID('dbo.syspublications') is not null
BEGIN
      DECLARE db_cursor CURSOR FOR  
            select  
                   sp.name as PublicationName 
                   ,sa.name as TableName 
                  , UPPER(srv.srvname) as SubscriberServerName  
                  , dest_db as DestinationDb
                  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 

      OPEN db_cursor   
      FETCH NEXT FROM db_cursor INTO @PublicationName, @ArticleName, @SubscriberServerName, @DestinationDb

      WHILE @@FETCH_STATUS = 0   
      BEGIN   
            -- Dropping the transactional subscriptions
            exec sp_dropsubscription @publication = @PublicationName, @subscriber = @SubscriberServerName, @destination_db = @DestinationDb, @article = N'all'

            -- Dropping the transactional articles
            exec sp_dropsubscription @publication = @PublicationName, @article = @ArticleName, @subscriber = N'all', @destination_db = N'all'

            exec sp_droparticle @publication = @PublicationName, @article = @ArticleName, @force_invalidate_snapshot = 1

            -- Dropping the transactional publication
            exec sp_droppublication @publication = @PublicationName

            FETCH NEXT FROM db_cursor INTO @PublicationName, @ArticleName, @SubscriberServerName, @DestinationDb
      END   

      CLOSE db_cursor   
      DEALLOCATE db_cursor
END

答案 1 :(得分:0)

在您希望为复制对象清理的数据库上运行此过程sp_removedbreplication http://technet.microsoft.com/en-us/library/ms188734%28v=sql.105%29.aspx