Menghapus Publikasi

Berlaku untuk:SQL ServerAzure SQL Managed Instance

Topik ini menjelaskan cara menghapus publikasi di SQL Server dengan menggunakan SQL Server Management Studio, Transact-SQL, atau Replication Management Objects (RMO).

Dalam Topik Ini

Menggunakan SQL Server Management Studio

Hapus publikasi dari folder Publikasi Lokal di SQL Server Management Studio.

Untuk menghapus publikasi

  1. Koneksi ke Publisher di Management Studio, lalu perluas simpul server.

  2. Perluas folder Replikasi, lalu perluas folder Publikasi Lokal.

  3. Klik kanan publikasi yang ingin Anda hapus, lalu klik Hapus.

Menggunakan T-SQL

Publikasi dapat dihapus secara terprogram menggunakan prosedur tersimpan replikasi. Prosedur tersimpan yang Anda gunakan bergantung pada jenis publikasi yang dihapus.

Catatan

Menghapus publikasi tidak menghapus objek yang diterbitkan dari database publikasi atau objek terkait dari database langganan. DROP <object> Gunakan perintah untuk menghapus objek ini secara manual jika perlu.

Untuk menghapus rekam jepret atau publikasi transaksi

  1. Lakukan salah satu langkah berikut:

    • Untuk menghapus satu publikasi, jalankan sp_droppublication di Publisher pada database publikasi.

    • Untuk menghapus semua publikasi di dan menghapus semua objek replikasi dari database yang diterbitkan, jalankan sp_removedbreplication di Publisher. Tentukan nilai tran untuk @type. (Opsional) Jika Distributor tidak dapat diakses atau jika status database dicurigai atau offline, tentukan nilai 1 untuk @force. (Opsional) Tentukan nama database untuk @dbname jika sp_removedbreplication tidak dijalankan pada database publikasi.

      Catatan

      Menentukan nilai 1 untuk @force dapat meninggalkan objek penerbitan terkait replikasi dalam database.

  2. (Opsional) Jika database ini tidak memiliki publikasi lain, jalankan sp_replicationdboption (Transact-SQL) untuk menonaktifkan publikasi database saat ini menggunakan rekam jepret atau replikasi transaksional.

  3. (Opsional) Di Pelanggan pada database langganan, jalankan sp_subscription_cleanup untuk menghapus metadata replikasi yang tersisa dalam database langganan.

Untuk menghapus publikasi gabungan

  1. Lakukan salah satu langkah berikut:

    • Untuk menghapus satu publikasi, jalankan sp_dropmergepublication (Transact-SQL) di Publisher pada database publikasi.

    • Untuk menghapus semua publikasi di dan menghapus semua objek replikasi dari database yang diterbitkan, jalankan sp_removedbreplication di Publisher. Tentukan nilai penggabungan untuk @type. (Opsional) Jika Distributor tidak dapat diakses atau jika status database dicurigai atau offline, tentukan nilai 1 untuk @force. (Opsional) Tentukan nama database untuk @dbname jika sp_removedbreplication tidak dijalankan pada database publikasi.

      Catatan

      Menentukan nilai 1 untuk @force dapat meninggalkan objek penerbitan terkait replikasi dalam database.

  2. (Opsional) Jika database ini tidak memiliki publikasi lain, jalankan sp_replicationdboption (Transact-SQL) untuk menonaktifkan publikasi database saat ini menggunakan replikasi penggabungan.

  3. (Opsional) Di Pelanggan pada database langganan, jalankan sp_mergesubscription_cleanup (Transact-SQL) untuk menghapus metadata replikasi yang tersisa dalam database langganan.

Contoh (Transact-SQL)

Contoh ini memperlihatkan cara menghapus publikasi transaksional dan menonaktifkan penerbitan transaksional untuk database. Contoh ini mengasumsikan bahwa semua langganan sebelumnya dihapus. Untuk informasi selengkapnya, lihat Menghapus Langganan Pull atau Menghapus Langganan Push.

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

Contoh ini memperlihatkan cara menghapus publikasi gabungan dan menonaktifkan penerbitan gabungan untuk database. Contoh ini mengasumsikan bahwa semua langganan sebelumnya dihapus. Untuk informasi selengkapnya, lihat Menghapus Langganan Pull atau Menghapus Langganan Push.

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

Menggunakan Objek Manajemen Replikasi (RMO)

Anda dapat menghapus publikasi secara terprogram dengan menggunakan Objek Manajemen Replikasi (RMO). Kelas RMO yang Anda gunakan untuk menghapus publikasi bergantung pada jenis publikasi yang Anda hapus.

Untuk menghapus rekam jepret atau publikasi transaksi

  1. Buat koneksi ke Publisher dengan menggunakan ServerConnection kelas .

  2. Membuat instans kelas TransPublication.

  3. Atur Name properti dan DatabaseName untuk publikasi, dan atur ConnectionContext properti ke koneksi yang dibuat di langkah 1.

  4. IsExistingObject Periksa properti untuk memverifikasi bahwa publikasi ada. Jika nilai properti ini salah, properti publikasi di langkah 3 didefinisikan dengan tidak benar atau publikasi tidak ada.

  5. Panggil Remove metode.

  6. (Opsional) Jika tidak ada publikasi transaksional lain untuk database ini, database dapat dinonaktifkan untuk penerbitan transaksional sebagai berikut:

    1. Membuat instans kelas ReplicationDatabase. Atur ConnectionContext properti ke instans ServerConnection dari langkah 1.

    2. Panggil LoadProperties metode. Jika metode ini mengembalikan false, konfirmasikan bahwa database ada.

    3. Atur EnabledTransPublishing properti ke false.

    4. Panggil CommitPropertyChanges metode.

  7. Tutup koneksi.

Untuk menghapus publikasi gabungan

  1. Buat koneksi ke Publisher dengan menggunakan ServerConnection kelas .

  2. Membuat instans kelas MergePublication.

  3. Atur Name properti dan DatabaseName untuk publikasi, dan atur ConnectionContext properti ke koneksi yang dibuat di langkah 1.

  4. IsExistingObject Periksa properti untuk memverifikasi bahwa publikasi ada. Jika nilai properti ini salah, properti publikasi di langkah 3 didefinisikan dengan tidak benar atau publikasi tidak ada.

  5. Panggil Remove metode.

  6. (Opsional) Jika tidak ada publikasi gabungan lain untuk database ini, database dapat dinonaktifkan untuk penerbitan gabungan sebagai berikut:

    1. Membuat instans kelas ReplicationDatabase. Atur ConnectionContext properti ke instans ServerConnection dari Langkah 1.

    2. Panggil LoadProperties metode. Jika metode ini mengembalikan false, verifikasi bahwa database ada.

    3. Atur EnabledMergePublishing properti ke false.

    4. Panggil CommitPropertyChanges metode.

  7. Tutup koneksi.

Contoh (RMO)

Contoh berikut menghapus publikasi transaksi. Jika tidak ada publikasi transaksional lain untuk database ini, penerbitan transaksional juga dinonaktifkan.

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

Contoh berikut menghapus publikasi gabungan. Jika tidak ada publikasi gabungan lain untuk database ini, penerbitan gabungan juga dinonaktifkan.

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

Lihat Juga

Konsep Prosedur Tersimpan Sistem Replikasi
Menerbitkan Objek Data dan Database