sp_link_publication (Transact-SQL)

适用于:SQL Server

设置在连接到发布服务器时立即更新订阅的同步触发器所使用的配置和安全信息。 此存储过程在订阅服务器的订阅数据库中执行。

重要

使用远程分发服务器配置发布服务器时,为所有参数提供的值(包括 job_loginjob_password)都会以纯文本方式发送到该分发服务器。 在执行此存储过程之前,应该对发布服务器及其远程分发服务器之间的连接进行加密。 有关详细信息,请参阅启用数据库引擎的加密连接(SQL Server 配置管理器)

重要

在某些情况下,如果订阅服务器运行 Microsoft SQL Server 2005 (9.x) Service Pack 1 或更高版本,并且发布服务器正在运行早期版本,则此存储过程可能会失败。 如果存储过程在此方案中失败,请将发布服务器升级到 SQL Server 2005 (9.x) Service Pack 1 或更高版本。

Transact-SQL 语法约定

语法

  
sp_link_publication [ @publisher = ] 'publisher'   
        , [ @publisher_db = ] 'publisher_db'   
        , [ @publication = ] 'publication'   
        , [ @security_mode = ] security_mode  
    [ , [ @login = ] 'login' ]  
    [ , [ @password = ]'password' ]  
    [ , [ @distributor = ] 'distributor' ]  

参数

[ @publisher = ] 'publisher' 要链接到的发布服务器的名称。 publishersysname,没有默认值。

[ @publisher_db = ] 'publisher_db' 要链接到的发布服务器数据库的名称。 publisher_dbsysname,没有默认值。

[ @publication = ] 'publication' 要链接到的发布的名称。 publicationsysname,没有默认值。

[ @security_mode = ] security_mode 订阅服务器用于连接到远程发布服务器以便立即更新的安全模式。 security_modeint,可以是以下值之一。 请尽可能使用 Windows 身份验证。

描述
0 使用 SQL Server 身份验证,此存储过程中指定的登录名作为登录名和密码

注意:在以前版本的 SQL Server 中,此选项用于指定动态远程过程调用 (RPC) 。
1 使用用户在订阅服务器上进行更改 (SQL Server身份验证或 Windows 身份验证) 的安全上下文。

注意:此帐户还必须存在于具有足够权限的发布服务器上。 使用 Windows 身份验证时,必须支持安全策略帐户委托。
2 使用使用 sp_link_publication 创建的现有用户定义的链接服务器登录名。

[ @login = ] 'login' 登录名。 login 的数据类型为 sysname,默认值为 NULL。 当 security_mode0 时,必须指定此参数。

[ @password = ] 'password' 密码。 passwordsysname,默认值为 NULL。 当 security_mode0 时,必须指定此参数。

[ @distributor = ] 'distributor' 分发服务器的名称。 分发服务器sysname,默认值为 NULL。

返回代码值

0 (成功) 或 1 (失败)

备注

sp_link_publication 用于在事务复制中立即更新订阅。

sp_link_publication 可用于推送订阅和拉取订阅。 在创建订阅之前或之后都可以调用此过程。 在 MSsubscription_properties (Transact-SQL) 系统表中插入或更新条目。

对于推送订阅,可以通过 sp_subscription_cleanup (Transact-SQL) 清理条目。 对于拉取订阅,可以通过 sp_droppullsubscription (Transact-SQL) sp_subscription_cleanup (Transact-SQL) 清理 条目。 还可以使用 NULL 密码调用 sp_link_publication ,以清除 MSsubscription_properties (Transact-SQL) 系统表中的条目,了解安全问题。

当立即更新订阅服务器连接到发布服务器时,它使用的默认模式不允许使用 Windows 身份验证进行连接。 若要用 Windows 身份验证模式进行连接,则必须设置到发布服务器的链接服务器,且立即更新订阅服务器在更新订阅服务器时应使用该连接。 这要求使用 security_mode2 运行sp_link_publication = 。 使用 Windows 身份验证时,必须支持安全策略帐户委托。

示例

-- This script uses sqlcmd scripting variables. They are in the form
-- $(MyVariable). For information about how to use scripting variables  
-- on the command line and in SQL Server Management Studio, see the 
-- "Executing Replication Scripts" section in the topic
-- "Programming Replication Using System Stored Procedures".

-- Execute this batch at the Subscriber.
DECLARE @publication AS sysname;
DECLARE @publicationDB AS sysname;
DECLARE @publisher AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS nvarchar(512);
SET @publication = N'AdvWorksProductTran';
SET @publicationDB = N'AdventureWorks2022';
SET @publisher = $(PubServer);
SET @login = $(Login);
SET @password = $(Password);

-- At the subscription database, create a pull subscription to a transactional 
-- publication using immediate updating with queued updating as a failover.
EXEC sp_addpullsubscription 
    @publisher = @publisher, 
    @publication = @publication, 
    @publisher_db = @publicationDB, 
    @update_mode = N'failover', 
    @subscription_type = N'pull';

-- Add an agent job to synchronize the pull subscription, 
-- which uses Windows Authentication when connecting to the Distributor.
EXEC sp_addpullsubscription_agent 
    @publisher = @publisher, 
    @publisher_db = @publicationDB, 
    @publication = @publication,
    @job_login = @login,
    @job_password = @password; 
 
-- Add a Windows Authentication-based linked server that enables the 
-- Subscriber-side triggers to make updates at the Publisher. 
EXEC sp_link_publication 
    @publisher = @publisher, 
    @publication = @publication,
    @publisher_db = @publicationDB, 
    @security_mode = 0,
    @login = @login,
    @password = @password;
GO

USE AdventureWorks2022;
GO

-- Execute this batch at the Publisher.
DECLARE @publication AS sysname;
DECLARE @subscriptionDB AS sysname;
DECLARE @subscriber AS sysname;
SET @publication = N'AdvWorksProductTran'; 
SET @subscriptionDB = N'AdventureWorks2022Replica'; 
SET @subscriber = $(SubServer);

-- At the Publisher, register the subscription, using the defaults.
USE [AdventureWorks2022]
EXEC sp_addsubscription 
    @publication = @publication, 
    @subscriber = @subscriber, 
    @destination_db = @subscriptionDB, 
    @subscription_type = N'pull', 
    @update_mode = N'failover';
GO

权限

只有 sysadmin 固定服务器角色的成员才能执行 sp_link_publication

另请参阅

sp_droppullsubscription (Transact-SQL)
sp_helpsubscription_properties (Transact-SQL)
sp_subscription_cleanup (Transact-SQL)
系统存储过程 (Transact-SQL)