查看和修改发布属性

适用于: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 类的实例,为发布设置 Name 和 DatabaseName 属性并将 ConnectionContext 属性设置为在步骤 1 中创建的连接。

  3. 调用 LoadProperties 方法获取该对象的属性。 如果此方法返回 false,则说明步骤 2 中的发布属性定义不正确,或者此发布不存在。

  4. (可选)若要更改属性,为一个或多个可设置的属性设置新值。 使用逻辑 AND 运算符(在 Microsoft Visual C# 中为& ,在 Microsoft Visual Basic 中为 And)可确定是否为 PublicationAttributes 属性设置了给定的 Attributes 值。 使用包含式逻辑 OR 运算符(在 Visual C# 中为 |,在 Visual Basic 中为 Or)和排他式逻辑 OR 运算符(在 Visual C# 中为 ^,在 Visual Basic 中为 Xor),可更改 Attributes 属性的 PublicationAttributes 值。

  5. (可选)如果为 CachePropertyChanges 指定了值 true,请调用 CommitPropertyChanges 方法将更改提交到服务器。 如果将 false 的值指定为 CachePropertyChanges (默认值),则会将更改立即发送到服务器。

查看或修改合并发布的属性

  1. 使用 ServerConnection 类创建与发布服务器的连接。

  2. 创建 MergePublication 类的实例,为发布设置 Name 和 DatabaseName 属性并将 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),可更改 Attributes 属性的 PublicationAttributes 值。

  5. (可选)如果为 CachePropertyChanges 指定了值 true,请调用 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