sys.sp_addlogreader_agent(Transact-SQL)

適用於:SQL ServerAzureSQL Managed Instance

為指定的資料庫加入記錄讀取器代理程式。 這個預存程式會在發行集資料庫的發行者端執行。

重要

使用遠端散發者設定發行者時,提供給所有參數的值,包括 @job_login 和 @job_password,都會以純文本形式傳送給散發者。 您應該先加密「發行者」及其遠端「散發者」之間的連接,再執行這個預存程序。 如需詳細資訊,請參閱針對加密連線設定 SQL Server 資料庫引擎。

Transact-SQL 語法慣例

語法

sys.sp_addlogreader_agent
    [ [ @job_login = ] N'job_login' ]
    [ , [ @job_password = ] N'job_password' ]
    [ , [ @job_name = ] N'job_name' ]
    [ , [ @publisher_security_mode = ] publisher_security_mode ]
    [ , [ @publisher_login = ] N'publisher_login' ]
    [ , [ @publisher_password = ] N'publisher_password' ]
    [ , [ @publisher = ] N'publisher' ]
    [ , [ @job_security_mode = ] job_security_mode ]
[ ; ]

引數

[ @job_login = ] N'job_login'

代理程序執行所在的 Windows 帳戶登入。 @job_login 為 nvarchar(257),預設值為 NULL。 此 Windows 帳戶一律用於與散發者的代理程式連線。 在 Azure SQL 受控執行個體 上,使用 SQL Server 帳戶。

注意

對於非 SQL Server 發行者,這必須是sp_adddistpublisher中指定的相同登入。

[ @job_password = ] N'job_password'

代理程序執行所在的 Windows 帳戶密碼。 @job_password 為 sysname,預設為 NULL。

重要

請勿將驗證資訊儲存在腳本檔案中。 為了獲得最佳安全性,應在運行時間提供登入名稱和密碼。

[ @job_name = ] N'job_name'

現有代理程式作業的名稱。 @job_name 為 sysname,預設為 NULL。 只有當代理程式開始使用現有的作業,而不是新建立的作業時,才指定此參數(預設值)。

[ @publisher_security_mode = ] publisher_security_mode

注意

Microsoft Entra ID 先前稱為 Azure Active Directory (Azure AD)。

決定代理程式在連接 Publisher 時所使用的驗證類型。 @publisher_security_mode 為 smallint,預設值為 1。 對於非 SQL Server 發佈者,請指定一個值。0 下列值會定義安全性模式:

  • 0指定 SQL Server 驗證。
  • 1指定 Windows 驗證。
  • 2指定自 SQL Server 2022 (16.x) CU 6 起的 Microsoft Entra 密碼驗證。
  • 3指定自 SQL Server 2022 (16.x) CU 6 起的 Microsoft Entra 整合驗證。
  • 4指定自 SQL Server 2022 (16.x) CU 6 起的 Microsoft Entra 令牌驗證。
  • 5指定使用 Microsoft Entra 管理身份驗證,使用發行商的預設管理身份。 此值僅適用於交易複寫,當 Publisher 與 Distributor 皆為 Azure SQL 受控執行個體 資源時。

[ @publisher_login = ] N'publisher_login'

連接到發行者時所使用的登入。 @publisher_login 為 sysname,預設為 NULL。 當@publisher_security_mode為 0時,必須指定@publisher_login。 如果 @publisher_login 為 且 NULL 為 1,則連接到發行者時會使用 @job_login 中指定的 Windows 帳戶。

[ @publisher_password = ] N'publisher_password'

連接到發行者時所使用的密碼。 @publisher_password 為 系統名稱,預設為 NULL。

重要

請勿將驗證資訊儲存在腳本檔案中。 為了獲得最佳安全性,應在運行時間提供登入名稱和密碼。

[ @publisher = ] N'publisher'

非 SQL Server 發行者的名稱。 @publisher 為 sysname,預設為 NULL。

注意

您不應該為 SQL Server 發行者指定此參數。

[ @job_security_mode = ] job_security_mode

僅供參考之用。 不支援。 我們無法保證未來的相容性。

傳回碼值

0 (成功)或 1 (失敗)。

備註

sp_addlogreader_agent 用於交易複製。

如果您在建立使用資料庫的發行集之前,將已啟用複寫的資料庫升級至這個 SQL Server 版本,則必須執行 sp_addlogreader_agent 以新增記錄讀取器代理程式。

權限

只有系統管理員固定伺服器角色或db_owner固定資料庫角色的成員才能執行 sp_addlogreader_agent。

範例

-- To avoid storing the login and password in the script file, the values 
-- are passed into SQLCMD as scripting variables. 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".

DECLARE @publicationDB AS sysname;
DECLARE @publication AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publicationDB = N'AdventureWorks'; 
SET @publication = N'AdvWorksProductTran'; 
-- Windows account used to run the Log Reader and Snapshot Agents.
SET @login = $(Login); 
-- This should be passed at runtime.
SET @password = $(Password); 

-- Enable transactional or snapshot replication on the publication database.
EXEC sp_replicationdboption 
    @dbname=@publicationDB, 
    @optname=N'publish',
    @value = N'true';

-- Execute sp_addlogreader_agent to create the agent job. 
EXEC sp_addlogreader_agent 
    @job_login = @login, 
    @job_password = @password,
    -- Explicitly specify the use of Windows Integrated Authentication (default) 
    -- when connecting to the Publisher.
    @publisher_security_mode = 1;

-- Create a new transactional publication with the required properties. 
EXEC sp_addpublication 
    @publication = @publication, 
    @status = N'active',
    @allow_push = N'true',
    @allow_pull = N'true',
    @independent_agent = N'true';

-- Create a new snapshot job for the publication, using a default schedule.
EXEC sp_addpublication_snapshot 
    @publication = @publication, 
    @job_login = @login, 
    @job_password = @password,
    -- Explicitly specify the use of Windows Integrated Authentication (default) 
    -- when connecting to the Publisher.
    @publisher_security_mode = 1;
GO