查看和修改发布属性

适用于:SQL ServerAzure SQL 托管实例

此主题介绍如何使用 SQL Server Management Studio、Transact-SQL 或复制管理对象 (RMO) 在 SQL Server 中查看和修改发布属性。

本主题内容

开始之前

限制和局限

  • 创建复制后,有些属性便不可以再进行修改,如果该发布存在订阅,则其他属性也无法再进行修改。 不能进行修改的属性将显示为只读。

建议

使用 SQL Server Management Studio

可在“发布属性 - <发布>”对话框(在 SQL Server Management Studio 和复制监视器中可用)中查看和修改发布属性。 有关启动复制监视器的信息,请参阅启动复制监视器

“发布属性 - <发布>”对话框中包括以下页:

  • “常规” 页,包含发布名称和说明、数据库名称、发布类型以及订阅过期设置。

  • “项目” 页,对应于新建发布向导中的 “项目” 页。 使用此页可添加和删除项目,以及更改项目的属性和列筛选。

  • “筛选行” 页,对应于新建发布向导中的 “筛选表行” 页。 使用此页可添加、编辑和删除所有发布类型的静态行筛选器,以及添加、编辑和删除合并发布的参数化行筛选器和联接筛选器。

  • “快照” 页,使您可以指定快照的格式和位置、快照是否压缩,以及应用快照前后要运行的脚本。

  • “FTP 快照” 页(适用于快照和事务发布,以及运行 SQL Server 2005 之前版本的发布服务器的合并发布),使您可以指定订阅服务器是否可以通过文件传输协议 (FTP) 下载快照文件。

  • “FTP 快照和 Internet” 页(适用于运行 SQL Server 2005 或更高版本的发布服务器的合并发布),使您可以指定订阅服务器是否可以通过 FTP 下载快照文件,以及订阅服务器是否可以通过 HTTPS 对订阅进行同步。

  • “订阅选项” 页,使您可以设置多个应用于所有订阅的选项。 这些选项会随着发布类型而有所不同。

  • “发布访问列表” 页,使您可以指定可以访问发布的登录名和组。

  • “代理安全性” 页,使您可以访问用于运行下列代理并连接复制拓扑中计算机的帐户设置:所有发布的快照代理、所有事务发布的日志读取器代理以及允许排队更新订阅的事务发布的队列读取器代理。

  • “数据分区” 页(适用于来自运行 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

使用复制管理对象 (RMO)

可以使用复制管理对象 (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 )和逻辑异或运算符(在 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)可确定是否为 Attributes 属性设置了给定的 PublicationAttributes 值。 使用逻辑 OR 运算符(在 Visual C# 中为| ,在 Visual Basic 中为 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)
使用复制监视器查看信息和执行任务
查看和修改项目属性