Eliminare una pubblicazione

Si applica a:SQL ServerIstanza gestita di SQL di Azure

Questo argomento descrive come eliminare una pubblicazione in SQL Server tramite SQL Server Management Studio, Transact-SQL o Replication Management Objects (RMO).

Contenuto dell'articolo

Utilizzo di SQL Server Management Studio

Eliminare le pubblicazioni dalla cartella Pubblicazioni locali in SQL Server Management Studio.

Per eliminare una pubblicazione

  1. Connettersi al server di pubblicazione in Management Studio e quindi espandere il nodo del server.

  2. Espandere la cartella Replica e quindi la cartella Pubblicazioni locali .

  3. Fare clic con il pulsante destro del mouse sulla pubblicazione che si desidera eliminare e quindi scegliere Elimina.

Utilizzo di Transact-SQL

È possibile eliminare pubblicazioni a livello di programmazione tramite le stored procedure di replica. Le stored procedure utilizzate dipendono dal tipo di pubblicazione eliminato.

Nota

L'eliminazione di una pubblicazione non comporta la rimozione degli oggetti pubblicati dal database di pubblicazione o degli oggetti corrispondenti dal database di sottoscrizione. Utilizzare il comando DROP <object> per rimuovere manualmente questi oggetti, se necessario.

Per eliminare una pubblicazione snapshot o transazionale

  1. Eseguire una delle operazioni seguenti:

    • Per eliminare una singola pubblicazione, eseguire sp_droppublication nel database di pubblicazione del server di pubblicazione.

    • Per eliminare tutte le pubblicazioni e rimuovere tutti gli oggetti di replica da un database pubblicato, eseguire sp_removedbreplication nel server di pubblicazione. Specificare il valore tran per @type. (Facoltativo) Se il server di distribuzione non è accessibile oppure se lo stato del database è sospetto oppure offline, specificare il valore 1 per @force. (Facoltativo) Specificare il nome del database per @dbname se sp_removedbreplication non viene eseguita nel database di pubblicazione.

      Nota

      Specificando il valore 1 per @force , è possibile che nel database rimangano oggetti di pubblicazione correlati alla replica.

  2. (Facoltativo) Se il database non contiene altre pubblicazioni, eseguire sp_replicationdboption (Transact-SQL) per disabilitare la pubblicazione del database corrente usando la replica snapshot o transazionale.

  3. (Facoltativo) Nel database di sottoscrizione del Sottoscrittore eseguire sp_subscription_cleanup per rimuovere gli eventuali metadati di replica rimanenti nel database di sottoscrizione.

Per eliminare una pubblicazione di tipo merge

  1. Eseguire una delle operazioni seguenti:

    • Per eliminare una singola pubblicazione, eseguire sp_dropmergepublication (Transact-SQL) nel database di pubblicazione del server di pubblicazione.

    • Per eliminare tutte le pubblicazioni e rimuovere tutti gli oggetti di replica da un database pubblicato, eseguire sp_removedbreplication nel server di pubblicazione. Specificare il valore merge per @type. (Facoltativo) Se il server di distribuzione non è accessibile oppure se lo stato del database è sospetto oppure offline, specificare il valore 1 per @force. (Facoltativo) Specificare il nome del database per @dbname se sp_removedbreplication non viene eseguita nel database di pubblicazione.

      Nota

      Specificando il valore 1 per @force , è possibile che nel database rimangano oggetti di pubblicazione correlati alla replica.

  2. (Facoltativo) Se il database non contiene altre pubblicazioni, eseguire sp_replicationdboption (Transact-SQL) per disabilitare la pubblicazione del database corrente usando la replica di tipo merge.

  3. (Facoltativo) Nel database di sottoscrizione del Sottoscrittore eseguire sp_mergesubscription_cleanup (Transact-SQL) per rimuovere gli eventuali metadati di replica rimanenti nel database di sottoscrizione.

Esempi (Transact-SQL)

In questo esempio viene illustrato come rimuovere una pubblicazione transazionale e disabilitare la pubblicazione transazionale per un database. Si presuppone che in precedenza siano state rimosse tutte le sottoscrizioni. Per ulteriori informazioni, vedere Delete a Pull Subscription o Delete a Push Subscription.

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

In questo esempio viene illustrato come rimuovere una pubblicazione di tipo merge e disabilitare la pubblicazione di tipo merge per un database. Si presuppone che in precedenza siano state rimosse tutte le sottoscrizioni. Per ulteriori informazioni, vedere Delete a Pull Subscription o Delete a Push Subscription.

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

Utilizzo di RMO (Replication Management Objects)

È possibile eliminare pubblicazioni a livello di programmazione tramite gli oggetti RMO (Replication Management Objects). Le classi RMO utilizzate per rimuovere una pubblicazione dipendono dal tipo di pubblicazione rimossa.

Per rimuovere una pubblicazione snapshot o transazionale

  1. Creare una connessione al server di pubblicazione tramite la classe ServerConnection .

  2. Creare un'istanza della classe TransPublication.

  3. Impostare le proprietà Name e DatabaseName per la pubblicazione, quindi impostare la proprietà ConnectionContext sulla connessione creata nel passaggio 1.

  4. Controllare la proprietà IsExistingObject per verificare che la pubblicazione sia esistente. Se il valore di questa proprietà è false, le proprietà di pubblicazione sono state definite in modo non corretto nel passaggio 3 oppure la pubblicazione non esiste.

  5. Chiamare il metodo Remove .

  6. (Facoltativo) Se per il database non esistono altre pubblicazioni transazionali, è possibile disabilitare il database per la pubblicazione transazionale come illustrato di seguito:

    1. Creare un'istanza della classe ReplicationDatabase. Impostare la proprietà ConnectionContext sull'istanza di ServerConnection restituita al passaggio 1.

    2. Chiamare il metodo LoadProperties . Se il metodo restituisce false, verificare che il database esista.

    3. Impostare le proprietà EnabledTransPublishing su false.

    4. Chiamare il metodo CommitPropertyChanges .

  7. Chiudere le connessioni.

Per rimuovere una pubblicazione di tipo merge

  1. Creare una connessione al server di pubblicazione tramite la classe ServerConnection .

  2. Creare un'istanza della classe MergePublication.

  3. Impostare le proprietà Name e DatabaseName per la pubblicazione, quindi impostare la proprietà ConnectionContext sulla connessione creata nel passaggio 1.

  4. Controllare la proprietà IsExistingObject per verificare che la pubblicazione sia esistente. Se il valore di questa proprietà è false, le proprietà di pubblicazione sono state definite in modo non corretto nel passaggio 3 oppure la pubblicazione non esiste.

  5. Chiamare il metodo Remove .

  6. (Facoltativo) Se per il database non esistono altre pubblicazioni di tipo merge, è possibile disabilitare il database per la pubblicazione di tipo merge come illustrato di seguito:

    1. Creare un'istanza della classe ReplicationDatabase. Impostare la proprietà ConnectionContext sull'istanza di ServerConnection restituita al passaggio 1.

    2. Chiamare il metodo LoadProperties . Se il metodo restituisce false, verificare che il database esista.

    3. Impostare le proprietà EnabledMergePublishing su false.

    4. Chiamare il metodo CommitPropertyChanges .

  7. Chiudere le connessioni.

Esempi (RMO)

Nell'esempio seguente viene eliminata una pubblicazione transazionale. Se per il database non esistono altre pubblicazioni transazionali, verrà anche disabilitata la pubblicazione transazionale.

// 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

Nell'esempio seguente viene eliminata una pubblicazione di tipo merge. Se per il database non esistono altre pubblicazioni di tipo merge, verrà anche disabilitata la pubblicazione di tipo merge.

// 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

Vedi anche

Replication System Stored Procedures Concepts
Pubblicare dati e oggetti di database