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' 這是要連結之發行者的名稱。 publisher sysname ,沒有預設值。

[ @publisher_db = ] 'publisher_db' 這是要連結之發行者資料庫的名稱。 publisher_db為 sysname ,沒有預設值。

[ @publication = ] 'publication' 這是要連結的發行集名稱。 publication sysname ,沒有預設值。

[ @security_mode = ] security_mode 這是訂閱者用來連線到遠端發行者以進行立即更新的安全性模式。 security_mode為 int ,而且可以是下列其中一個值。 儘可能使用 Windows 驗證。

描述
0 使用 SQL Server 驗證搭配這個預存程式中指定的登入做為 登入 密碼

注意:在舊版的 SQL Server 中,此選項是用來指定動態遠端程序呼叫 (RPC)。
1 使用使用者在訂閱者端進行變更的安全性內容(SQL Server 驗證或 Windows 驗證)。

注意:此帳戶也必須存在於具有足夠許可權的發行者端。 使用 Windows 驗證時,必須支援安全性帳戶委派。
2 使用使用 sp_link_publication 建立 的現有使用者定義連結伺服器登入。

[ @login = ] 'login' 這是登入。 login 是預設值為 NULL 的 sysname。 當 security_mode 0 ,必須指定此參數。

[ @password = ] 'password' 這是密碼。 password sysname ,預設值為 Null。 當 security_mode 0 ,必須指定此參數。

[ @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_mode = 2 執行 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

權限

只有系統管理員 固定伺服器角色的成員 可以執行 sp_link_publication

另請參閱

sp_droppullsubscription (Transact-SQL)
sp_helpsubscription_properties (Transact-SQL)
sp_subscription_cleanup (Transact-SQL)
系統預存程序 (Transact-SQL)