使用脚本删除复制

时间:2012-07-19 10:16:25

标签: replication sql-server-2012

我正在使用sql server 2012和复制过程。现在复制过程正常,但我想删除此复制,我的意思是所有的出版物,订阅和文章通过脚本。我浏览了这个网站http://support.microsoft.com/kb/324401并尝试了以下脚本

:setvar PublisherDatabase "AdventureWorks2012"
:setvar SubscriberServer "HYDHTC0131320D\MSSQLSERVER2"

use [$(PublisherDatabase)]

--Drop all subscriptions
exec sp_dropsubscription  
@publication = N'TestPubs',
@article = N'all',
--@subscriber = [$(SubscriberServer)]
@subscriber = N'all',
@destination_db = N'all'

--Drop publication
if exists (Select 1 From SysPublications where name = N'TestPubs')
EXEC sp_droppublication @publication = N'TestPubs'

EXEC sp_replicationdboption @dbname = [$(PublisherDatabase)], @optname = N'publish', @value = N'false'

--Drop subscriber entry
EXEC sp_dropsubscriber @subscriber = [$(SubscriberServer)]

--Drop distributor
EXEC sp_dropdistributor @no_checks = 1

执行上述脚本后,我得到以下错误。

Only one Log Reader Agent or log-related procedure (sp_repldone, sp_replcmds, and sp_replshowcmds) can connect to a database at a time. If you executed a log-related procedure, drop the connection over which the procedure was executed or execute sp_replflush over that connection before starting the Log Reader Agent or executing another log-related procedure.

Msg 18752, Level 16, State 1, Procedure sp_replcmds, Line 1

Only one Log Reader Agent or log-related procedure (sp_repldone, sp_replcmds, and sp_replshowcmds) can connect to a database at a time. If you executed a log-related procedure, drop the connection over which the procedure was executed or execute sp_replflush over that connection before starting the Log Reader Agent or executing another log-related procedure.

The Subscriber was dropped.

Msg 20015, Level 16, State 1, Procedure 

sp_MSreplremoveuncdir, Line 83
Could not remove directory 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\ReplData\unc\HYDHTC0131320D_ADVENTUREWORKS2012_TESTPUBS\20120719152739\'. Check the security context of xp_cmdshell and close other processes that may be accessing the directory.

查看此屏幕截图以了解更多详情

enter image description here

任何人都可以帮助我解决这些问题

2 个答案:

答案 0 :(得分:2)

在删除复制时看起来你有一些错误,并且在订阅者处有一些孤立的订阅元数据。可以使用sp_removedbreplication从订阅数据库中删除孤立的元数据。

为了将来参考,您可以按照以下步骤删除所有订阅,发布以及禁用发布和分发:

来自链接的相关代码位

a)放弃交易复制

的推送订阅
-- This batch is executed at the Publisher to remove 
-- a pull or push subscription to a transactional publication.
DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
SET @publication = N'AdvWorksProductTran';
SET @subscriber = $(SubServer);

USE [AdventureWorks2012]
EXEC sp_dropsubscription 
  @publication = @publication, 
  @article = N'all',
  @subscriber = @subscriber;
GO

b)删除合并复制

的订阅
DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
SET @publication = N'AdvWorksSalesOrdersMerge';
SET @subscriber = $(SubServer);
SET @subscriptionDB = N'AdventureWorks2012Replica';

USE [AdventureWorks2012]
EXEC sp_dropmergesubscription 
  @publication = @publication, 
  @subscriber = @subscriber, 
  @subscriber_db = @subscriptionDB;
GO

c)在交易复制上删除发布并设置源数据库以停止成为发布者。

DECLARE @publicationDB AS sysname;
DECLARE @publication AS sysname;
SET @publicationDB = N'AdventureWorks2008R2'; 
SET @publication = N'AdvWorksProductTran'; 

-- Remove a transactional publication.
USE [AdventureWorks2008R2]
EXEC sp_droppublication @publication = @publication;

-- Remove replication objects from the database.
USE [master]
EXEC sp_replicationdboption 
  @dbname = @publicationDB, 
  @optname = N'publish', 
  @value = N'false';
GO

d)在合并复制上删除发布并设置源数据库以停止成为发布者。

DECLARE @publication AS sysname
DECLARE @publicationDB    AS sysname
SET @publication = N'AdvWorksSalesOrdersMerge' 
SET @publicationDB = N'AdventureWorks2008R2'

-- Remove the merge publication.
USE [AdventureWorks2008R2]
EXEC sp_dropmergepublication @publication = @publication;

-- Remove replication objects from the database.
USE master
EXEC sp_replicationdboption 
  @dbname = @publicationDB, 
  @optname = N'merge publish', 
  @value = N'false'
GO

答案 1 :(得分:0)

我的解决方法是在交易中执行sp_droppublication

-- local variables
declare
 @return_code    int -- return code of sp
,@dependent_publ sysname -- publication dependent on replicated RDZ tables

declare a cursor for
select distinct p.name from syspublications p
 inner join sysarticles s on p.pubid = s.pubid 
 inner join adm_replicated_rdz_tables r on r.table_name = s.name

if exists
 (select * from sys.objects
   where object_id = object_id('[dbo].[sysarticles]') and type in ('U'))
begin
 open a; fetch next from a into @dependent_publ; while @@fetch_status = 0
 begin
  begin transaction
   print @dependent_publ
   execute sp_droppublication @dependent_publ;
  commit
  fetch next from a into @dependent_publ;
 end; close a; deallocate a;
end