Kommentar
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
gäller för:SQL Server
Azure SQL Managed Instance
Detta ämne beskriver hur man tar bort en publikation i SQL Server genom att använda SQL Server Management Studio, Transact-SQL eller Replication Management Objects (RMO).
Använda SQL Server Management Studio
Ta bort publikationer från mappen Local Publications i SQL Server Management Studio.
Att ta bort en publikation
Anslut till Publisher i Management Studio och expandera sedan servernoden.
Expandera mappen Replikering och expandera sedan mappen Lokala publikationer .
Högerklicka på publikationen du vill ta bort och klicka sedan på Radera.
Använda Transact-SQL
Publikationer kan raderas programmatiskt med hjälp av replikeringslagrade procedurer. De lagrade procedurer du använder beror på vilken typ av publikation som tas bort.
Anmärkning
Att ta bort en publikation tar inte bort publicerade objekt från publiceringsdatabasen eller motsvarande objekt från prenumerationsdatabasen. Använd kommandot DROP <object> för att manuellt ta bort dessa objekt om det behövs.
För att ta bort en ögonblicksbild eller transaktionell publikation
Gör något av följande:
För att radera en enskild publikation, kör sp_droppublication på Publisher i publikationsdatabasen.
För att ta bort alla publikationer i och ta bort alla replikeringsobjekt från en publicerad databas, kör sp_removedbreplication på Publisher. Ange ett värde på tran för @type. (Valfritt) Om distributören inte kan nås eller om databasens status är misstänkt eller offline, ange värdet 1 för @force. (Valfritt) Ange namnet på databasen för @dbname om sp_removedbreplication inte körs i publiceringsdatabasen.
Anmärkning
Att ange värdet 1 för @force kan lämna replikationsrelaterade publiceringsobjekt i databasen.
(Valfritt) Om denna databas inte har några andra publikationer, kör sp_replicationdboption (Transact-SQL) för att inaktivera publiceringen av den aktuella databasen med hjälp av snapshot eller transaktionell replikering.
(Valfritt) Hos prenumeranten, i prenumerationsdatabasen, kör sp_subscription_cleanup för att ta bort eventuella återstående replikeringsmetadata i prenumerationsdatabasen.
För att ta bort en sammanslagningspublikation
Gör något av följande:
För att radera en enskild publikation, kör sp_dropmergepublication (Transact-SQL) vid Publisher i publikationsdatabasen.
För att ta bort alla publikationer i och ta bort alla replikeringsobjekt från en publicerad databas, kör sp_removedbreplication på Publisher. Ange ett värde för sammanslagning för @type. (Valfritt) Om distributören inte kan nås eller om databasens status är misstänkt eller offline, ange värdet 1 för @force. (Valfritt) Ange namnet på databasen för @dbname om sp_removedbreplication inte körs i publiceringsdatabasen.
Anmärkning
Att ange värdet 1 för @force kan lämna replikationsrelaterade publiceringsobjekt i databasen.
(Valfritt) Om denna databas inte har några andra publikationer, kör sp_replicationdboption (Transact-SQL) för att inaktivera publiceringen av den aktuella databasen med hjälp av merge-replikering.
(Valfritt) Hos prenumeranten i prenumerationsdatabasen kör du sp_mergesubscription_cleanup (Transact-SQL) för att ta bort eventuella kvarvarande replikationsmetadata i prenumerationsdatabasen.
Exempel (Transact-SQL)
Detta exempel visar hur man tar bort en transaktionell publikation och inaktiverar transaktionell publicering för en databas. Detta exempel förutsätter att alla prenumerationer tidigare togs bort. För mer information, se Ta bort en Pull-prenumeration eller Ta bort en Push-prenumeration.
DECLARE @publicationDB AS sysname;
DECLARE @publication AS sysname;
SET @publicationDB = N'AdventureWorks';
SET @publication = N'AdvWorksProductTran';
-- Remove a transactional publication.
USE [AdventureWorks2022]
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
Detta exempel visar hur man tar bort en sammanslagningspublikation och inaktiverar sammanslagningspublicering för en databas. Detta exempel förutsätter att alla prenumerationer tidigare togs bort. För mer information, se Ta bort en Pull-prenumeration eller Ta bort en Push-prenumeration.
DECLARE @publication AS sysname
DECLARE @publicationDB AS sysname
SET @publication = N'AdvWorksSalesOrdersMerge'
SET @publicationDB = N'AdventureWorks'
-- Remove the merge publication.
USE [AdventureWorks]
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
Använda replikeringshanteringsobjekt (RMO)
Du kan ta bort publikationer programmatiskt genom att använda Replication Management Objects (RMO). De RMO-klasser du använder för att ta bort en publikation beror på vilken typ av publikation du tar bort.
Att ta bort en ögonblicksbild eller transaktionell publikation
Skapa en anslutning till Publisher med hjälp av klassen ServerConnection.
Skapa en instans av TransPublication klassen.
Ställ in egenskaperna Name och DatabaseName för publikationen och ställ in egenskapen ConnectionContext till den anslutning som skapades i steg 1.
Kontrollera egenskapen IsExistingObject för att verifiera att publikationen finns. Om värdet av denna egenskap är falskt, har antingen publiceringsegenskaperna i steg 3 definierats felaktigt eller så existerar inte publiceringen.
Anropa Remove metoden.
(Valfritt) Om inga andra transaktionella publikationer finns för denna databas kan databasen inaktiveras för transaktionell publicering enligt följande:
Skapa en instans av ReplicationDatabase klassen. Sätt egenskapen ConnectionContext till instansen av ServerConnection från steg 1.
Anropa LoadProperties metoden. Om denna metod ger falskt, bekräfta att databasen existerar.
Sätt egenskapen EnabledTransPublishing till falsk.
Anropa CommitPropertyChanges metoden.
Stäng anslutningarna.
För att ta bort en sammanslagningspublikation
Skapa en anslutning till Publisher med hjälp av klassen ServerConnection.
Skapa en instans av MergePublication klassen.
Ställ in egenskaperna Name och DatabaseName för publikationen och ställ in egenskapen ConnectionContext till den anslutning som skapades i steg 1.
Kontrollera egenskapen IsExistingObject för att verifiera att publikationen finns. Om värdet av denna egenskap är falskt, har antingen publiceringsegenskaperna i steg 3 definierats felaktigt eller så existerar inte publiceringen.
Anropa Remove metoden.
(Valfritt) Om inga andra sammanslagningspublikationer finns för denna databas kan databasen inaktiveras för sammanslagningspublicering enligt följande:
Skapa en instans av ReplicationDatabase klassen. Sätt egenskapen ConnectionContext till instansen av ServerConnection från steg 1.
Anropa LoadProperties metoden. Om denna metod returnerar falskt, verifiera att databasen existerar.
Sätt egenskapen EnabledMergePublishing till falsk.
Anropa CommitPropertyChanges metoden.
Stäng anslutningarna.
Exempel (RMO)
Följande exempel tar bort en transaktionell publikation. Om inga andra transaktionspublikationer finns för denna databas är transaktionell publicering också inaktiverad.
// Define the Publisher, publication database,
// and publication names.
string publisherName = publisherInstance;
string publicationName = "AdvWorksProductTran";
string publicationDbName = "AdventureWorks2022";
TransPublication publication;
ReplicationDatabase publicationDb;
// Create a connection to the Publisher
// using Windows Authentication.
ServerConnection conn = new ServerConnection(publisherName);
try
{
conn.Connect();
// Set the required properties for the transactional publication.
publication = new TransPublication();
publication.ConnectionContext = conn;
publication.Name = publicationName;
publication.DatabaseName = publicationDbName;
// Delete the publication, if it exists and has no subscriptions.
if (publication.LoadProperties() && !publication.HasSubscription)
{
publication.Remove();
}
else
{
// Do something here if the publication does not exist
// or has subscriptions.
throw new ApplicationException(String.Format(
"The publication {0} could not be deleted. " +
"Ensure that the publication exists and that all " +
"subscriptions have been deleted.",
publicationName, publisherName));
}
// If no other transactional publications exists,
// disable publishing on the database.
publicationDb = new ReplicationDatabase(publicationDbName, conn);
if (publicationDb.LoadProperties())
{
if (publicationDb.TransPublications.Count == 0)
{
publicationDb.EnabledTransPublishing = false;
}
}
else
{
// Do something here if the database does not exist.
throw new ApplicationException(String.Format(
"The database {0} does not exist on {1}.",
publicationDbName, publisherName));
}
}
catch (Exception ex)
{
// Implement application error handling here.
throw new ApplicationException(String.Format(
"The publication {0} could not be deleted.",
publicationName), ex);
}
finally
{
conn.Disconnect();
}
' Define the Publisher, publication database,
' and publication names.
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksProductTran"
Dim publicationDbName As String = "AdventureWorks2022"
Dim publication As TransPublication
Dim publicationDb As ReplicationDatabase
' Create a connection to the Publisher
' using Windows Authentication.
Dim conn As ServerConnection = New ServerConnection(publisherName)
Try
conn.Connect()
' Set the required properties for the transactional publication.
publication = New TransPublication()
publication.ConnectionContext = conn
publication.Name = publicationName
publication.DatabaseName = publicationDbName
' Delete the publication, if it exists and has no subscriptions.
If publication.LoadProperties() And Not publication.HasSubscription Then
publication.Remove()
Else
' Do something here if the publication does not exist
' or has subscriptions.
Throw New ApplicationException(String.Format( _
"The publication {0} could not be deleted. " + _
"Ensure that the publication exists and that all " + _
"subscriptions have been deleted.", _
publicationName, publisherName))
End If
' If no other transactional publications exists,
' disable publishing on the database.
publicationDb = New ReplicationDatabase(publicationDbName, conn)
If publicationDb.LoadProperties() Then
If publicationDb.TransPublications.Count = 0 Then
publicationDb.EnabledTransPublishing = False
End If
Else
' Do something here if the database does not exist.
Throw New ApplicationException(String.Format( _
"The database {0} does not exist on {1}.", _
publicationDbName, publisherName))
End If
Catch ex As Exception
' Implement application error handling here.
Throw New ApplicationException(String.Format( _
"The publication {0} could not be deleted.", _
publicationName), ex)
Finally
conn.Disconnect()
End Try
Följande exempel tar bort en sammanslagningspublicering. Om inga andra sammanslagningspublikationer finns för denna databas är även sammanslagningspublicering inaktiverad.
// Define the Publisher, publication database,
// and publication names.
string publisherName = publisherInstance;
string publicationName = "AdvWorksSalesOrdersMerge";
string publicationDbName = "AdventureWorks2022";
MergePublication publication;
ReplicationDatabase publicationDb;
// Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(publisherName);
try
{
// Connect to the Publisher.
conn.Connect();
// Set the required properties for the merge publication.
publication = new MergePublication();
publication.ConnectionContext = conn;
publication.Name = publicationName;
publication.DatabaseName = publicationDbName;
// Delete the publication, if it exists and has no subscriptions.
if (publication.LoadProperties() && !publication.HasSubscription)
{
publication.Remove();
}
else
{
// Do something here if the publication does not exist
// or has subscriptions.
throw new ApplicationException(String.Format(
"The publication {0} could not be deleted. " +
"Ensure that the publication exists and that all " +
"subscriptions have been deleted.",
publicationName, publisherName));
}
// If no other merge publications exists,
// disable publishing on the database.
publicationDb = new ReplicationDatabase(publicationDbName, conn);
if (publicationDb.LoadProperties())
{
if (publicationDb.MergePublications.Count == 0 && publicationDb.EnabledMergePublishing)
{
publicationDb.EnabledMergePublishing = false;
}
}
else
{
// Do something here if the database does not exist.
throw new ApplicationException(String.Format(
"The database {0} does not exist on {1}.",
publicationDbName, publisherName));
}
}
catch (Exception ex)
{
// Implement application error handling here.
throw new ApplicationException(String.Format(
"The publication {0} could not be deleted.",
publicationName), ex);
}
finally
{
conn.Disconnect();
}
' Define the Publisher, publication database,
' and publication names.
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publicationDbName As String = "AdventureWorks2022"
Dim publication As MergePublication
Dim publicationDb As ReplicationDatabase
' Create a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(publisherName)
Try
' Connect to the Publisher.
conn.Connect()
' Set the required properties for the merge publication.
publication = New MergePublication()
publication.ConnectionContext = conn
publication.Name = publicationName
publication.DatabaseName = publicationDbName
' Delete the publication, if it exists and has no subscriptions.
If (publication.LoadProperties() And Not publication.HasSubscription) Then
publication.Remove()
Else
' Do something here if the publication does not exist
' or has subscriptions.
Throw New ApplicationException(String.Format( _
"The publication {0} could not be deleted. " + _
"Ensure that the publication exists and that all " + _
"subscriptions have been deleted.", _
publicationName, publisherName))
End If
' If no other merge publications exists,
' disable publishing on the database.
publicationDb = New ReplicationDatabase(publicationDbName, conn)
If publicationDb.LoadProperties() Then
If publicationDb.MergePublications.Count = 0 _
And publicationDb.EnabledMergePublishing Then
publicationDb.EnabledMergePublishing = False
End If
Else
' Do something here if the database does not exist.
Throw New ApplicationException(String.Format( _
"The database {0} does not exist on {1}.", _
publicationDbName, publisherName))
End If
Catch ex As Exception
' Implement application error handling here.
Throw New ApplicationException(String.Format( _
"The publication {0} could not be deleted.", _
publicationName), ex)
Finally
conn.Disconnect()
End Try