Share via


sp_addlogreader_agent (Transact-SQL)

適用於:SQL ServerAzure SQL 受控執行個體

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

重要

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

Transact-SQL 語法慣例

語法

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_login = ] N'job_login'

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

注意

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

[ @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 標識符 先前稱為 Azure Active Directory (Azure AD)。

連接到發行者時代理程式所使用的安全性模式。 @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 令牌驗證。

[ @publisher_login = ] N'publisher_login'

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

[ @publisher_password = ] N'publisher_password'

連接到發行者時所使用的密碼。 @publisher_password為 sysname,預設值為 NULL

重要

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

[ @publisher = ] N'publisher'

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

注意

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

傳回碼值

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