sp_addlogreader_agent (Transact-SQL)

加入給定資料庫的記錄讀取器代理程式。這個預存程序執行於發行集資料庫的發行者端。

安全性注意事項安全性注意事項

當利用遠端散發者來設定發行者時,提供給所有參數的值 (包括 job_login 和 job_password) 都會以純文字的方式傳給散發者。您應該先加密「發行者」及其遠端「散發者」之間的連接,再執行這個預存程序。如需詳細資訊,請參閱<加密 SQL Server 的連接>。

主題連結圖示Transact-SQL 語法慣例

語法

sp_addlogreader_agent [ @job_login = ] 'job_login'
        , [ @job_password = ] 'job_password'
    [ , [ @job_name = ] 'job_name' ]
    [ , [ @publisher_security_mode = ] publisher_security_mode ]
    [ , [ @publisher_login = ] 'publisher_login' ]
    [ , [ @publisher_password = ] 'publisher_password' ] 
    [ , [ @publisher = ] 'publisher' ]

引數

  • [ @job_login= ] 'job_login'
    這是用來執行代理程式之 Microsoft Windows 帳戶的登入。job_login 是 nvarchar(257),預設值是 NULL。通往散發者的代理程式連接一律使用這個 Windows 帳戶。

    [!附註]

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

  • [ @job_password= ] 'job_password'
    這是執行代理程式之 Windows 帳戶的密碼。job_password 是 sysname,預設值是 NULL。

    安全性注意事項安全性注意事項

    請勿將驗證資訊儲存在指令碼檔案中。為了要有最佳的安全性,登入名稱和密碼應該在執行階段提供。

  • [ @job_name= ] 'job_name'
    這是現有代理程式作業的名稱。job_name 是 sysname,預設值是 NULL。只有在利用現有的作業來啟動代理程式,而不用新建立的作業 (預設值) 時,才指定這個參數。

  • [ @publisher_security_mode= ] publisher_security_mode
    這是連接到發行者時,代理程式所用的安全性模式。publisher_security_mode 是 smallint,預設值是 10 指定 SQL Server 驗證,1 指定 Windows 驗證。非 SQL Server 發行者必須指定 0 值。

  • [ @publisher_login= ] 'publisher_login'
    這是連接到發行者時所用的登入。publisher_login 是 sysname,預設值是 NULL。當 publisher_security_mode 是 0 時,必須指定 publisher_login。如果 publisher_login 是 NULL,publisher_security_mode 是 1,當連接到發行者時,就會使用 job_login 所指定的 Windows 帳戶。

  • [ @publisher_password= ] 'publisher_password'
    這是連接到發行者時所用的密碼。publisher_password 是 sysname,預設值是 NULL。

    安全性注意事項安全性注意事項

    請勿將驗證資訊儲存在指令碼檔案中。為了要有最佳的安全性,登入名稱和密碼應該在執行階段提供。

  • [ @publisher= ] 'publisher'
    這是非 SQL Server 發行者的名稱。publisher 是 sysname,預設值是 NULL。

    [!附註]

    您不應該將這個參數指定給 SQL Server 發行者。

傳回碼值

0 (成功) 或 1 (失敗)

備註

sp_addlogreader_agent 用於交易式複寫中。

如果您先將啟用複寫的資料庫升級到 SQL Server 的這個版本,之後,才建立使用這個資料庫的發行集,您必須執行 sp_addlogreader_agent 來加入記錄讀取器代理程式。

權限

只有系統管理員 (sysadmin) 固定伺服器角色或 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'AdventureWorks2008R2'; 
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