檢視及修改發行集屬性

適用於:SQL ServerAzure SQL 受控執行個體

此主題描述如何使用 SQL Server Management Studio、Transact-SQL 或 Replication Management Objects (RMO),檢視和修改 SQL Server 中的發行集屬性。

本主題內容

開始之前

限制事項

  • 有些屬性在建立了發行集後無法再修改,而另一些當存在發行集的訂閱時便無法修改。 無法修改的屬性以唯讀顯示。

建議

使用 SQL Server Management Studio

您可以在位於 SQL Server Management Studio 與複寫監視器的 [發行集屬性 - <發行集>] 對話方塊中,檢視及修改發行集屬性。 如需啟動複寫監視器的詳細資訊,請參閱啟動複寫監視器

[發行集屬性 - <發行集>] 對話方塊上包含下列頁面:

  • [一般] 頁面包含發行集名稱和描述、發行集類型以及訂閱過期設定。

  • [發行項] 頁面對應「新增發行集精靈」中的 [發行項] 頁面。 此頁面用於新增及刪除發行項,以及變更發行項的屬性與資料行篩選。

  • [篩選資料列] 頁面對應「新增發行集精靈」中的 [篩選資料表的資料列] 頁面。 此頁面用於新增、編輯和刪除所有類型發行集的靜態資料列篩選,以及新增、編輯和刪除合併式發行集的參數化資料列篩選器和聯結篩選。

  • [快照集] 頁面允許您指定快照集的格式和位置、是否應壓縮快照集,以及套用快照集前後要執行的指令碼。

  • [FTP 快照集] 頁面 (針對快照式和交易式發行集,以及執行 SQL Server 2005 之前版本之「發行者」的合併式發行集) 允許您指定「訂閱者」是否可以透過「檔案傳輸通訊協定」(FTP) 下載快照集檔案。

  • [FTP 快照集和網際網路] 頁面 (針對執行 SQL Server 2005 或更新版本之「發行者」的合併式發行集) 允許您指定「訂閱者」是否可以透過 FTP 下載快照集檔案,以及「訂閱者」是否可以透過 HTTP 同步處理訂閱。

  • [訂閱選項] 頁面允許您設定套用至所有訂閱的一些選項。 選項視發行集類型的不同而不同。

  • [發行集存取清單] 頁面允許您指定可存取發行集的登入和群組。

  • [代理程式安全性] 頁面允許您存取執行以下代理程式之帳戶的設定,並與複寫拓撲中的電腦建立連接:所有發行集的「快照集代理程式」;所有交易式發行集的「記錄讀取器代理程式」;及允許佇列更新訂閱之交易式發行集的「佇列讀取器代理程式」。

  • [資料分割] 頁面 (針對執行 SQL Server 2005 或更新版本之「發行者」的合併式發行集) 允許您指定至含參數化篩選之發行集的「訂閱者」是否可以在一個快照集不可用時要求另一個快照集。 它還允許您為一個或多個資料分割一次性或按循環排程產生快照集。

若要在 Management Studio 中檢視和修改發行集屬性

  1. 連線到 Management Studio 中的發行者,然後展開伺服器節點。

  2. 展開 [複寫] 資料夾,然後展開 [本機發行集] 資料夾。

  3. 以滑鼠右鍵按一下發行集,然後按一下 [屬性]

  4. 必要時修改任何屬性,然後按一下 [確定]

若要在複寫監視器中檢視和修改發行集屬性

  1. 展開「複寫監視器」左窗格中的「發行者」群組,然後展開一個「發行者」。

  2. 以滑鼠右鍵按一下發行集,然後按一下 [屬性]

  3. 必要時修改任何屬性,然後按一下 [確定]

使用 TRANSACT-SQL

您可以使用複寫預存程序來以程式設計的方式修改發行集及傳回其屬性。 您使用的預存程序將根據發行集的類型而定。

檢視快照式或交易式發行集的屬性

  1. 執行 sp_helppublication,針對 @publication 參數指定發行集的名稱。 如果您不指定這個參數,就會傳回發行者上與所有發行集有關的資訊。

變更快照式或交易式發行集的屬性

  1. 執行 sp_changepublication,在 @property 參數中指定要變更的發行集屬性,並在 @value 參數中指定這個屬性的新值。

    注意

    如果此變更將需要產生新的快照集,您也必須針對 @force_invalidate_snapshot 指定 1的值,而如果此變更將需要重新初始化訂閱者,您就必須針對 @force_reinit_subscription 指定 1的值。 如需當變更時需要新的快照集或重新初始化之屬性的詳細資訊,請參閱變更發行集與發行項屬性

檢視合併式發行集的屬性

  1. 執行 sp_helpmergepublication,針對 @publication 參數指定發行集的名稱。 如果您不指定這個參數,就會傳回發行者上與所有發行集有關的資訊。

變更合併式發行集的屬性

  1. 執行 sp_changemergepublication,在 @property 參數中指定變更的發行集屬性,並在 @value 參數中指定這個屬性的新值。

    注意

    如果此變更需要產生新的快照集,您也必須為 @force_invalidate_snapshot 指定 1 值;如果此變更將需要重新初始化訂閱者,則您必須為 @force_reinit_subscription 指定 1 值。如需變更時需要新快照集或重新初始化的屬性詳細資訊,請參閱變更發行集與發行項屬性

檢視快照集的屬性

  1. 執行 sp_helppublication_snapshot,針對 @publication 參數指定發行集的名稱。

變更快照集的屬性

  1. 執行 sp_changepublication_snapshot,針對適當的快照集參數指定一或多個新的快照集屬性。

範例 (Transact-SQL)

這個異動複寫範例會傳回發行集的屬性。

DECLARE @myTranPub AS sysname
SET @myTranPub = N'AdvWorksProductTran' 

USE [AdventureWorks2022]
EXEC sp_helppublication @publication = @myTranPub
GO

這個異動複寫範例會停用發行集的結構描述複寫。

DECLARE @publication AS sysname
SET @publication = N'AdvWorksProductTran' 

-- Turn off DDL replication for the transactional publication.
USE [AdventureWorks2022]
EXEC sp_changepublication 
  @publication = @publication, 
  @property = N'replicate_ddl', 
  @value = 0
GO

這個合併式複寫範例會傳回發行集的屬性。

DECLARE @publication AS sysname;
SET @publication = N'AdvWorksSalesOrdersMerge';

USE [AdventureWorks2022]
EXEC sp_helpmergepublication @publication = @publication;
GO

這個合併式複寫範例會停用發行集的結構描述複寫。

DECLARE @publication AS sysname;
SET @publication = N'AdvWorksSalesOrdersMerge'; 

-- Disable DDL replication for the publication.
USE [AdventureWorks2022]
EXEC sp_changemergepublication 
  @publication = @publication, 
  @property = N'replicate_ddl', 
  @value = 0,
  @force_invalidate_snapshot = 0, 
  @force_reinit_subscription = 0;
GO

使用 Replication Management Objects (RMO)

您可以使用 Replication Management Objects (RMO) 以程式設計的方式修改發行集及存取其屬性。 用來檢視或修改發行集屬性的 RMO 類別,將取決於發行集的類型而定。

檢視或修改快照式或交易式發行集的屬性

  1. 使用 ServerConnection 類別建立與發行者的連接。

  2. 建立 TransPublication 類別的執行個體、為發行集設定 NameDatabaseName 屬性,以及將 ConnectionContext 屬性設定為步驟 1 中所建立的連接。

  3. 呼叫 LoadProperties 方法以取得物件的屬性。 如果此方法傳回 false,則表示步驟 2 中的發行集屬性定義不正確,或者該發行集不存在。

  4. (選擇性) 若要變更屬性,請針對一或多個可設定的屬性設定新的值。 使用邏輯 AND 運算子 (Microsoft Visual C# 中的 & 及 Microsoft Visual Basic 中的 And ),以判斷是否已針對 Attributes 屬性設定給定的 PublicationAttributes 值。 使用包含的邏輯 OR 運算子 (Visual C# 中的 | 和 Visual Basic 中的 Or ),並使用排除的邏輯 OR 運算子 (Visual C# 中的 ^ 和 Visual Basic 中的 Xor ),為 PublicationAttributes 屬性變更 Attributes 值。

  5. (選擇性) 如果您已針對 P:Microsoft.SqlServer.Replication.ReplicationObject.CachePropertyChanges 指定 CachePropertyChanges的值,請呼叫 CommitPropertyChanges 方法來認可伺服器上的變更。 如果您已針對 false 指定 CachePropertyChanges 的值 (預設值),則會立即將變更傳送到伺服器。

檢視或修改合併式發行集的屬性

  1. 使用 ServerConnection 類別建立與發行者的連接。

  2. 建立 MergePublication 類別的執行個體、為發行集設定 NameDatabaseName 屬性,以及將 ConnectionContext 屬性設定為步驟 1 中所建立的連接。

  3. 呼叫 LoadProperties 方法以取得物件的屬性。 如果此方法傳回 false,則表示步驟 2 中的發行集屬性定義不正確,或者該發行集不存在。

  4. (選擇性) 若要變更屬性,請針對一或多個可設定的屬性設定新的值。 使用邏輯 AND 運算子 (& Microsoft Visual C# 中的和 Visual Basic 中的 And),以判斷是否已針對 PublicationAttributes 屬性設定給定的 Attributes 值。 使用包含的邏輯 OR 運算子 (Visual C# 中的 | 和 Visual Basic 中的 Or ),並使用排除的邏輯 OR 運算子 (Visual C# 中的 ^ 和 Visual Basic 中的 Xor ),為 PublicationAttributes 屬性變更 Attributes 值。

  5. (選擇性) 如果您已針對 P:Microsoft.SqlServer.Replication.ReplicationObject.CachePropertyChanges 指定 CachePropertyChanges的值,請呼叫 CommitPropertyChanges 方法來認可伺服器上的變更。 如果您已針對 false 指定 CachePropertyChanges 的值 (預設值),則會立即將變更傳送到伺服器。

範例 (RMO)

此範例會設定交易式發行集的發行集屬性。 這些變更在明確傳送到伺服器之前,會先加以快取。

// Define the server, database, and publication names
string publisherName = publisherInstance;
string publicationName = "AdvWorksProductTran";
string publicationDbName = "AdventureWorks2022";

TransPublication publication;

// Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(publisherName);

try
{
    // Connect to the Publisher.
    conn.Connect();

    // Set the required properties for the publication.
    publication = new TransPublication();
    publication.ConnectionContext = conn;
    publication.Name = publicationName;
    publication.DatabaseName = publicationDbName;

    // Explicitly enable caching of property changes on this object.
    publication.CachePropertyChanges = true;

    // If we can't get the properties for this publication, 
    // throw an application exception.
    if (publication.LoadProperties())
    {
        // Enable support for push subscriptions and disable support 
        // for pull subscriptions.
        if ((publication.Attributes & PublicationAttributes.AllowPull) != 0)
        {
            publication.Attributes ^= PublicationAttributes.AllowPull;
        }
        if ((publication.Attributes & PublicationAttributes.AllowPush) == 0)
        {
            publication.Attributes |= PublicationAttributes.AllowPush;
        }

        // Send changes to the server.
        publication.CommitPropertyChanges();
    }
    else
    {
        throw new ApplicationException(String.Format(
            "Settings could not be retrieved for the publication. " +
            "Ensure that the publication {0} exists on {1}.",
            publicationName, publisherName));
    }
}
catch (Exception ex)
{
    // Do error handling here.
    throw new ApplicationException(
        "The publication property could not be changed.", ex);
}
finally
{
    conn.Disconnect();
}
' Define the server, database, and publication names
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksProductTran"
Dim publicationDbName As String = "AdventureWorks2022"

Dim publication As TransPublication

' 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 publication.
    publication = New TransPublication()
    publication.ConnectionContext = conn
    publication.Name = publicationName
    publication.DatabaseName = publicationDbName

    ' Explicitly enable caching of property changes on this object.
    publication.CachePropertyChanges = True

    ' If we can't get the properties for this publication, 
    ' throw an application exception.
    If publication.LoadProperties() Then
        ' Enable support for push subscriptions and disable support 
        ' for pull subscriptions.
        If (publication.Attributes And PublicationAttributes.AllowPull) <> 0 Then
            publication.Attributes = publication.Attributes _
            Xor PublicationAttributes.AllowPull
        End If
        If (publication.Attributes And PublicationAttributes.AllowPush) = 0 Then
            publication.Attributes = publication.Attributes _
            Or PublicationAttributes.AllowPush
        End If

        ' Send changes to the server.
        publication.CommitPropertyChanges()
    Else
        Throw New ApplicationException(String.Format( _
         "Settings could not be retrieved for the publication. " + _
         "Ensure that the publication {0} exists on {1}.", _
         publicationName, publisherName))
    End If
Catch ex As Exception
    ' Do error handling here.
    Throw New ApplicationException( _
        "The publication property could not be changed.", ex)
Finally
    conn.Disconnect()
End Try

此範例會停用合併式發行集的 DDL 複寫。

// Define the server, database, and publication names
string publisherName = publisherInstance;
string publicationName = "AdvWorksSalesOrdersMerge";
string publicationDbName = "AdventureWorks2022";

MergePublication publication;

// Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(publisherName);

try
{
    // Connect to the Publisher.
    conn.Connect();

    // Set the required properties for the publication.
    publication = new MergePublication();
    publication.ConnectionContext = conn;
    publication.Name = publicationName;
    publication.DatabaseName = publicationDbName;


    // If we can't get the properties for this merge publication, then throw an application exception.
    if (publication.LoadProperties())
    {
        // If DDL replication is currently enabled, disable it.
        if (publication.ReplicateDdl == DdlReplicationOptions.All)
        {
            publication.ReplicateDdl = DdlReplicationOptions.None;
        }
        else
        {
            publication.ReplicateDdl = DdlReplicationOptions.All;
        }
    }
    else
    {
        throw new ApplicationException(String.Format(
            "Settings could not be retrieved for the publication. " +
            "Ensure that the publication {0} exists on {1}.",
            publicationName, publisherName));
    }
}
catch (Exception ex)
{
    // Do error handling here.
    throw new ApplicationException(
        "The publication property could not be changed.", ex);
}
finally
{
    conn.Disconnect();
}
' Define the server, database, and publication names
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publicationDbName As String = "AdventureWorks2022"

Dim publication As MergePublication

' 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 publication.
    publication = New MergePublication()
    publication.ConnectionContext = conn
    publication.Name = publicationName
    publication.DatabaseName = publicationDbName

    ' If we can't get the properties for this merge publication, then throw an application exception.
    If publication.LoadProperties() Then
        ' If DDL replication is currently enabled, disable it.
        If publication.ReplicateDdl = DdlReplicationOptions.All Then
            publication.ReplicateDdl = DdlReplicationOptions.None
        Else
            publication.ReplicateDdl = DdlReplicationOptions.All
        End If
    Else
        Throw New ApplicationException(String.Format( _
         "Settings could not be retrieved for the publication. " + _
         "Ensure that the publication {0} exists on {1}.", _
         publicationName, publisherName))
    End If
Catch ex As Exception
    ' Do error handling here.
    Throw New ApplicationException( _
        "The publication property could not be changed.", ex)
Finally
    conn.Disconnect()
End Try

另請參閱

發行資料和資料庫物件
變更發行集與發行項屬性
對發行集資料庫進行結構描述變更
Replication System Stored Procedures Concepts
從發行集中加入和卸除發行項 (SQL Server Management Studio)
使用複寫監視器來檢視資訊及執行工作
檢視和修改發行項屬性