방법: 복제 스크립트 업그레이드(복제 Transact-SQL 프로그래밍)

Transact-SQL 스크립트 파일을 사용하여 복제 토폴로지를 프로그래밍 방식으로 구성할 수 있습니다. 자세한 내용은 복제 시스템 저장 프로시저 개념를 참조하십시오. SQL Server 2005에서 sysadmin 고정 서버 역할의 멤버인 사용자가 SQL Server 2000에서 작성한 복제 스크립트를 실행하는 경우 스크립트는 제대로 실행됩니다. 사용자가 db_owner 고정 데이터베이스 역할 또는 다른 역할의 멤버인 경우에는 스크립트가 MSSQL_ENG021797 또는 MSSQL_ENG021798 오류와 함께 실패하며 스크립트를 업그레이드해야 합니다.

보안 정보보안 정보

sysadmin 역할의 멤버에 의해 실행되는 스크립트는 반드시 업그레이드할 필요는 없지만 이 항목에서 설명하는 대로 기존 스크립트를 수정하는 것이 좋습니다. 복제 에이전트 보안 모델 항목의 "에이전트에 필요한 사용 권한" 섹션에서 설명하는 대로 각 복제 에이전트에 대해 최소 사용 권한이 있는 계정을 지정합니다.

이러한 향상된 보안 기능은 복제 에이전트 작업이 실행되는 Microsoft Windows 계정을 명시적으로 지정할 수 있도록 함으로써 사용 권한을 보다 잘 제어할 수 있게 해 주며, 기존 스크립트의 다음 저장 프로시저에 영향을 줍니다.

  • sp_addpublication_snapshot:

    이제 sp_addpublication_snapshot(Transact-SQL)을 실행하여 배포자에서 스냅숏 에이전트가 실행되는 작업을 만들 때 Windows 자격 증명을 @job_login@job_password로 지정해야 합니다.

  • sp_addpushsubscription_agent:

    이제 sp_addpushsubscription_agent(Transact-SQL)를 실행하여 명시적으로 작업을 추가하고 배포자에서 배포 에이전트 작업이 실행되는 Windows 자격 증명(@job_login@job_password)을 지정해야 합니다. SQL Server 2005 이전 버전의 SQL Server에서 이 작업은 밀어넣기 구독이 만들어질 때 자동으로 수행되었습니다.

  • sp_addmergepushsubscription_agent:

    이제 sp_addmergepushsubscription_agent(Transact-SQL)를 실행하여 명시적으로 작업을 추가하고 배포자에서 병합 에이전트 작업이 실행되는 Windows 자격 증명(@job_login@job_password)을 지정해야 합니다. SQL Server 2005 이전 버전의 SQL Server에서 이 작업은 밀어넣기 구독이 만들어질 때 자동으로 수행되었습니다.

  • sp_addpullsubscription_agent:

    이제 sp_addpullsubscription_agent(Transact-SQL)를 실행하여 구독자에서 배포 에이전트가 실행되는 작업을 만들 때 Windows 자격 증명을 @job_login@job_password로 지정해야 합니다.

  • sp_addmergepullsubscription_agent:

    이제 sp_addmergepullsubscription_agent(Transact-SQL)을 실행하여 구독자에서 병합 에이전트가 실행되는 작업을 만들 때 Windows 자격 증명을 @job_login@job_password로 지정해야 합니다.

  • sp_addlogreader_agent:

    이제 sp_addmergepushsubscription_agent(Transact-SQL)를 실행하여 수동으로 작업을 추가하고 배포자에서 로그 판독기 에이전트가 실행되는 Windows 자격 증명을 지정해야 합니다. SQL Server 2005 이전 버전의 SQL Server에서 이 작업은 트랜잭션 게시가 만들어질 때 자동으로 수행되었습니다.

  • sp_addqreader_agent:

    이제 sp_addqreader_agent(Transact-SQL)를 실행하여 수동으로 작업을 추가하고 배포자에서 큐 판독기 에이전트가 실행되는 Windows 자격 증명을 지정해야 합니다. SQL Server 2005 이전 버전의 SQL Server에서 이 작업은 지연 업데이트를 지원하는 트랜잭션 게시가 만들어질 때 자동으로 수행되었습니다.

SQL Server 2005에 도입된 보안 모델에서 복제 에이전트는 항상 @job_name@job_password에 지정된 자격 증명을 사용하는 Windows 인증을 통해 SQL Server의 로컬 인스턴스에 연결합니다. 복제 에이전트 작업을 실행할 때 사용되는 Windows 계정의 요구 사항에 대한 자세한 내용은 복제 에이전트 보안 모델을 참조하십시오.

보안 정보보안 정보

가능하면 런타임에 사용자에게 자격 증명을 입력하라는 메시지를 표시하십시오. 스크립트 파일에 자격 증명을 저장하는 경우에는 파일 자체에 보안이 설정되도록 합니다.

스냅숏 또는 트랜잭션 게시를 구성하는 스크립트를 업그레이드하려면

  1. 기존 스크립트에서 sp_addpublication(Transact-SQL)을 실행하기 전에 게시 데이터베이스의 게시자에서 sp_addlogreader_agent(Transact-SQL)를 실행합니다. @job_name@job_password에 로그 판독기 에이전트가 실행되는 Windows 자격 증명을 지정합니다. 게시자에 연결할 때 에이전트가 SQL Server 인증을 사용하면 @publisher_security_mode에 대해 값 0을 지정하고 @publisher_login@publisher_password에 대해 SQL Server 로그인 정보를 지정해야 합니다. 이렇게 하면 게시 데이터베이스에 대한 로그 판독기 에이전트가 만들어집니다.

    [!참고]

    이 단계는 트랜잭션 게시 전용이며 스냅숏 게시에는 필요하지 않습니다.

  2. (옵션) sp_addpublication(Transact-SQL)을 실행하기 전에 배포 데이터베이스의 배포자에서 sp_addqreader_agent(Transact-SQL)를 실행합니다. @job_name@job_password에 큐 판독기 에이전트가 실행되는 Windows 자격 증명을 지정합니다. 이렇게 하면 배포자에 대한 큐 판독기 에이전트가 만들어집니다.

    [!참고]

    이 단계는 지연 업데이트 구독자를 지원하는 트랜잭션 게시에만 필요합니다.

  3. (옵션) 새 복제 기능을 구현하는 매개 변수에 대해 기본값이 아닌 값을 설정하려면 sp_addpublication(Transact-SQL) 실행을 업데이트합니다.

  4. sp_addpublication(Transact-SQL)을 실행한 후 게시 데이터베이스의 게시자에서 sp_addpublication_snapshot(Transact-SQL)을 실행합니다. @publication을 지정하고 @job_name@job_password에 스냅숏 에이전트가 실행되는 Windows 자격 증명을 지정합니다. 게시자에 연결할 때 에이전트가 SQL Server 인증을 사용하면 @publisher_security_mode에 대해 값 0을 지정하고 @publisher_login@publisher_password에 대해 SQL Server 로그인 정보를 지정해야 합니다. 이렇게 하면 게시에 대해 스냅숏 에이전트 작업이 만들어집니다.

  5. (옵션) 새 복제 기능을 구현하는 매개 변수에 대해 기본값이 아닌 값을 설정하려면 sp_addarticle(Transact-SQL) 실행을 업데이트합니다.

스냅숏 또는 트랜잭션 게시에 구독을 추가하는 스크립트를 업그레이드하려면

병합 게시를 구성하는 스크립트를 업그레이드하려면

  1. (옵션) 기존 스크립트에서 새 복제 기능을 구현하는 매개 변수에 대해 기본값이 아닌 값을 설정하려면 sp_addmergepublication(Transact-SQL) 실행을 업데이트합니다.

  2. sp_addmergepublication(Transact-SQL)을 실행한 후 게시 데이터베이스의 게시자에서 sp_addpublication_snapshot(Transact-SQL)을 실행합니다. @publication을 지정하고 @job_name@job_password에 스냅숏 에이전트가 실행되는 Windows 자격 증명을 지정합니다. 게시자에 연결할 때 에이전트가 SQL Server 인증을 사용하면 @publisher_security_mode에 대해 값 0을 지정하고 @publisher_login@publisher_password에 대해 SQL Server 로그인 정보를 지정해야 합니다. 이렇게 하면 게시에 대해 스냅숏 에이전트 작업이 만들어집니다.

  3. (옵션) 새 복제 기능을 구현하는 매개 변수에 대해 기본값이 아닌 값을 설정하려면 sp_add_targetservergroup(Transact-SQL) 실행을 업데이트합니다.

병합 게시에 구독을 추가하는 스크립트를 업그레이드하려면

다음은 Product 테이블의 트랜잭션 게시를 만드는 SQL Server 2000 스크립트의 예제입니다. 이 게시에서는 지연 업데이트를 장애 조치로 사용하는 즉시 업데이트를 지원합니다. 읽기 쉽도록 기본 매개 변수가 삭제되었습니다.

USE [Northwind]
GO

DECLARE @publication AS sysname
DECLARE @publicationDB AS sysname
DECLARE @article AS sysname
SET @publication = N'NwdProductTran'
SET @publicationDB = N'Northwind'
SET @article = N'Products'

-- Enable the replication database.
EXEC sp_replicationdboption 
    @dbname = @publicationDB, 
    @optname = N'publish', 
    @value = N'true'

-- Add the transactional publication.
EXEC sp_addpublication 
    @publication = @publication, 
    @sync_method = N'native', 
    @status = N'active', 
    @repl_freq = N'continuous', 
    @description = N'Transactional publication of Northwind.', 
    @allow_push = N'true', 
    @allow_pull = N'true', 
    @allow_sync_tran = N'true', 
    @autogen_sync_procs = N'true', 
    @allow_queued_tran = N'true'

-- Add a snapshot job.
EXEC sp_addpublication_snapshot 
    @publication = @publication

-- Add the transactional articles.
EXEC sp_addarticle 
    @publication = @publication, 
    @article = @article, 
    @source_owner = N'dbo', 
    @source_object = @article, 
    @destination_table = @article, 
    @type = N'logbased', 
    @schema_option = 0x00000000000080F3, 
    @ins_cmd = N'CALL sp_MSins_Products', 
    @del_cmd = N'XCALL sp_MSdel_Products', 
    @upd_cmd = N'XCALL sp_MSupd_Products', 
    @auto_identity_range = N'false'
GO

다음은 트랜잭션 게시를 만드는 앞의 스크립트를 SQL Server 2005 이상 버전에서 성공적으로 실행되도록 업그레이드하는 예제입니다. 이 게시에서는 지연 업데이트를 장애 조치로 사용하는 즉시 업데이트를 지원합니다. 새 매개 변수의 기본값이 명시적으로 선언되었습니다.

[!참고]

Windows 자격 증명은 sqlcmd 스크립팅 변수를 사용하여 런타임에 제공됩니다.

-- To avoid storing the login and password in the script file, the value 
-- is passed into SQLCMD as a scripting variable. 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 at the Distributor.
USE [distribution]

DECLARE @login AS sysname;
DECLARE @password AS sysname;
-- Specify the Windows account to run the Queue Reader Agent.
SET @login = $(Login); 
-- Pass the password at runtime.
SET @password = $(Password); 

-- Execute sp_addqreader_agent to create the Queue Reader Agent job. 
EXEC sp_addqreader_agent 
    @job_login = @login, 
    @job_password = @password;
GO

-- Execute at the Publisher.
USE [Northwind]
GO

DECLARE @publication AS sysname;
DECLARE @publicationDB AS sysname;
DECLARE @article AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publication = N'NwdProductTran';
SET @publicationDB = N'Northwind';
SET @article = N'Products';
-- Specify the Windows account to run the Log Reader and Snapshot Agents.
SET @login = $(Login); 
-- Pass the password at runtime.
SET @password = $(Password); 

-- Enable the replication 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;

-- Add the transactional publication.
EXEC sp_addpublication 
    @publication = @publication, 
    @sync_method = N'native', 
    @repl_freq = N'continuous', 
    @status = N'active',
    @description = N'Transactional publication of Northwind.', 
    @allow_push = N'true', 
    @allow_pull = N'true', 
    @allow_sync_tran = N'true', 
    @autogen_sync_procs = N'true', 
    @allow_queued_tran = N'true',
    @replicate_ddl = 1,
    @enabled_for_p2p = N'false';

-- 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;

-- Add a transactional article.
EXEC sp_addarticle 
  @publication = @publication, 
  @article = @article, 
  @source_owner = N'dbo', 
  @source_object = @article, 
  @destination_table = @article, 
  @type = N'logbased', 
  @schema_option = 0x00000000000080F3, 
  @ins_cmd = N'CALL sp_MSins_Products', 
  @del_cmd = N'XCALL sp_MSdel_Products', 
  @upd_cmd = N'XCALL sp_MSupd_Products', 
  @auto_identity_range = N'false',
  @identityrangemanagementoption = N'manual',
  @fire_triggers_on_snapshot = N'false';
GO

다음은 Customers 테이블의 병합 게시를 만드는 SQL Server 2000 스크립트의 예제입니다. 읽기 쉽도록 기본 매개 변수가 삭제되었습니다.

-- Enable the replication database.
USE [Northwind]
GO

DECLARE @publicationDB AS sysname
DECLARE @publication AS sysname
DECLARE @article AS sysname
SET @publicationDB = N'Northwind' 
SET @publication = N'NwdCustomersMerge' 
SET @article = N'Customers' 

EXEC sp_replicationdboption 
    @dbname = @publicationDB, 
    @optname = N'merge publish', 
    @value = N'true'

-- Add the merge publication.
EXEC sp_addmergepublication 
    @publication = @publication, 
    @description = N'Merge publication of Northwind.', 
    @retention = 14, 
    @sync_mode = N'native', 
    @centralized_conflicts = N'true', 
    @dynamic_filters = N'false', 
    @keep_partition_changes = N'false'

EXEC sp_addpublication_snapshot 
    @publication = @publication

-- Add the merge articles.
EXEC sp_addmergearticle 
    @publication = @publication, 
    @article = @article, 
    @source_owner = N'dbo', 
    @source_object = @article, 
    @type = N'table', 
    @description = null, 
    @column_tracking = N'true', 
    @schema_option = 0x000000000000CFF1
 GO

다음은 병합 게시를 만드는 앞의 스크립트를 SQL Server 2005 이상 버전에서 성공적으로 실행되도록 업그레이드하는 예제입니다. 새 매개 변수의 기본값이 명시적으로 선언되었습니다.

[!참고]

Windows 자격 증명은 sqlcmd 스크립팅 변수를 사용하여 런타임에 제공됩니다.

-- To avoid storing the login and password in the script file, the value 
-- is passed into SQLCMD as a scripting variable. 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".

-- Enabling the replication database
-- Enable the replication database.
USE [Northwind]
GO

DECLARE @publicationDB AS sysname;
DECLARE @publication AS sysname;
DECLARE @article AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publicationDB = N'Northwind';
SET @publication = N'NwdCustomersMerge';
SET @article = N'Customers';
-- Specify the Windows account to run the Snapshot Agent.
SET @login = $(Login); 
-- Supply the password at runtime.
SET @password = $(Password); 

EXEC sp_replicationdboption 
    @dbname = @publicationDB, 
    @optname = N'merge publish', 
    @value = N'true';

-- Add the merge publication.
EXEC sp_addmergepublication 
    @publication = @publication, 
    @description = N'Merge publication of Northwind.', 
    @retention = 14, 
    @sync_mode = N'native', 
    @dynamic_filters = N'false', 
    @keep_partition_changes = N'false',
    -- Only set to '90RTM' if all Subscribers are SQL Server 2005.
    @publication_compatibility_level = N'90RTM',
    @replicate_ddl = 1,
    @allow_subscriber_initiated_snapshot = N'true',
    @allow_web_synchronization = N'false',
    @allow_partition_realignment = N'true',
    @retention_period_unit = N'day',
    @automatic_reinitialization_policy = 0,
    @conflict_logging = N'both';

EXEC sp_addpublication_snapshot 
    @publication = @publication, 
    @job_login = @login,
    @job_password = @password;

-- Add the merge article.
EXEC sp_addmergearticle 
    @publication = @publication, 
    @article = @article, 
    @source_owner = N'dbo', 
    @source_object = @article, 
    @type = N'table', 
    @description = null, 
    @column_tracking = N'true', 
    @schema_option = 0x0000000000034FD1,
    @partition_options = 0,
    @subscriber_upload_options = 0,
    @identityrangemanagementoption = N'manual',
    @delete_tracking = N'true',
    @compensate_for_errors = N'false',
    @stream_blob_columns = N'true';
GO

다음은 트랜잭션 게시에 밀어넣기 구독을 만드는 SQL Server 2000 스크립트의 예제입니다. 읽기 쉽도록 기본 매개 변수가 삭제되었습니다.

DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
SET @publication = N'NwdProductTran' 
SET @subscriber = N'MYSUBSERVER' 
SET @subscriptionDB = N'NorthwindReplica' 

-- Add a Subscriber, using the defaults.
USE [master]
EXEC sp_addsubscriber 
    @subscriber = @subscriber

-- Add a push subscription to a transactional publication.
USE [Northwind]
EXEC sp_addsubscription 
    @publication = @publication, 
    @subscriber = @subscriber, 
    @destination_db = @subscriptionDB, 
    @subscription_type = N'push'
GO

다음은 트랜잭션 게시에 밀어넣기 구독을 만드는 앞의 스크립트를 SQL Server 2005 이상 버전에서 성공적으로 실행되도록 업그레이드하는 예제입니다. 새 매개 변수의 기본값이 명시적으로 선언되었습니다.

[!참고]

Windows 자격 증명은 sqlcmd 스크립팅 변수를 사용하여 런타임에 제공됩니다.

-- 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".

DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publication = N'NwdProductTran'; 
SET @subscriber = $(Subscriber); 
SET @subscriptionDB = N'NorthwindReplica'; 
-- Specify the Windows account to run the Distribution Agent.
SET @login = $(Login); 
-- Supply the password at runtime.
SET @password = $(Password); 

-- Add a push subscription to a transactional publication.
USE [Northwind]
EXEC sp_addsubscription 
    @publication = @publication, 
    @subscriber = @subscriber, 
    @destination_db = @subscriptionDB, 
    @subscription_type = N'push';

-- Add an agent job to synchronize the push subscription.
EXEC sp_addpushsubscription_agent 
    @publication = @publication, 
    @subscriber = @subscriber, 
    @subscriber_db = @subscriptionDB, 
    @job_login = @login, 
    @job_password = @password;
GO

다음은 병합 게시에 밀어넣기 구독을 만드는 SQL Server 2000 스크립트의 예제입니다. 읽기 쉽도록 기본 매개 변수가 삭제되었습니다.

DECLARE @publication AS sysname
DECLARE @subscriber AS sysname
DECLARE @subscriptionDB AS sysname
SET @publication = N'NwdCustomersMerge' 
SET @subscriber = N'SUBSERVER' 
SET @subscriptionDB = N'NorthwindReplica' 

-- Add a Subscriber, using the defaults.
USE [master]
EXEC sp_addsubscriber 
  @subscriber = @subscriber

-- Add a push subscription to a merge publication.
USE [Northwind]
EXEC sp_addmergesubscription 
  @publication = @publication, 
  @subscriber = @subscriber, 
  @subscriber_db = @subscriptionDB, 
  @subscription_type = N'push',
  @subscriber_type = N'local',
  @sync_type = N'automatic'
GO

다음은 병합 게시에 밀어넣기 구독을 만드는 앞의 스크립트를 SQL Server 2005 이상 버전에서 성공적으로 실행되도록 업그레이드하는 예제입니다. 새 매개 변수의 기본값이 명시적으로 선언되었습니다.

[!참고]

Windows 자격 증명은 sqlcmd 스크립팅 변수를 사용하여 런타임에 제공됩니다.

-- 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".

DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publication = N'NwdCustomersMerge'; 
SET @subscriber = $(Subscriber); 
SET @subscriptionDB = N'NorthwindReplica'; 
-- Specify the Windows account to run the Merge Agent.
SET @login = $(Login); 
-- Supply the password at runtime.
SET @password = $(Password); 

-- Add a push subscription to a merge publication.
USE [Northwind]
EXEC sp_addmergesubscription 
    @publication = @publication, 
    @subscriber = @subscriber, 
    @subscriber_db = @subscriptionDB, 
    @subscription_type = N'push';

-- Add an agent job to synchronize the push subscription.
EXEC sp_addmergepushsubscription_agent 
    @publication = @publication, 
    @subscriber = @subscriber, 
    @subscriber_db = @subscriptionDB, 
    @job_login = @login, 
    @job_password = @password;
GO

다음은 트랜잭션 게시에 끌어오기 구독을 만드는 SQL Server 2000 스크립트의 예제입니다. 읽기 쉽도록 기본 매개 변수가 삭제되었습니다.

DECLARE @publication AS sysname
DECLARE @subscriber AS sysname
DECLARE @subscriptionDB AS sysname
SET @publication = N'NwdCustomersMerge' 
SET @subscriber = N'SUBSERVER' 
SET @subscriptionDB = N'NorthwindReplica' 

-- Add a Subscriber, using the defaults.
USE [master]
EXEC sp_addsubscriber 
  @subscriber = @subscriber

-- Add a push subscription to a merge publication.
USE [Northwind]
EXEC sp_addmergesubscription 
  @publication = @publication, 
  @subscriber = @subscriber, 
  @subscriber_db = @subscriptionDB, 
  @subscription_type = N'push',
  @subscriber_type = N'local',
  @sync_type = N'automatic'
GO

다음은 트랜잭션 게시에 끌어오기 구독을 만드는 앞의 스크립트를 SQL Server 2005 이상 버전에서 성공적으로 실행되도록 업그레이드하는 예제입니다. 새 매개 변수의 기본값이 명시적으로 선언되었습니다.

[!참고]

Windows 자격 증명은 sqlcmd 스크립팅 변수를 사용하여 런타임에 제공됩니다.

-- 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 at the Subscriber.
DECLARE @publication AS sysname;
DECLARE @publisher AS sysname;
DECLARE @publicationDB AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publication = N'NwdProductTran'; 
SET @publisher = $(Publisher); 
SET @publicationDB = N'Northwind'; 
-- Specify the Windows account to run the Distribution Agent.
SET @login = $(Login); 
-- Supply the password at runtime.
SET @password = $(Password); 

-- At the subscription database, create a pull subscription 
-- to a transactional publication.
USE [NorthwindReplica]
EXEC sp_addpullsubscription 
    @publisher = @publisher, 
    @publication = @publication, 
    @publisher_db = @publicationDB,
    @subscription_type = N'pull';

-- Add an agent job to synchronize the pull subscription.
EXEC sp_addpullsubscription_agent 
    @publisher = @publisher, 
    @publisher_db = @publicationDB, 
    @publication = @publication, 
    @distributor = @publisher,
    @job_login = @login,
    @job_password = @password;
GO

-- Execute at the Publisher.
DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
SET @publication = N'NwdProductTran'; 
SET @subscriber = $(Subscriber); 
SET @subscriptionDB = N'NorthwindReplica'; 

-- Add a pull subscription to a transactional publication.
USE [Northwind]
EXEC sp_addsubscription 
    @publication = @publication, 
    @subscriber = @subscriber, 
    @destination_db = @subscriptionDB, 
    @subscription_type = N'pull';
GO

다음은 병합 게시에 끌어오기 구독을 만드는 SQL Server 2000 스크립트의 예제입니다. 읽기 쉽도록 기본 매개 변수가 삭제되었습니다.

-- Execute at the Subscriber
DECLARE @publication AS sysname
DECLARE @publisher AS sysname
DECLARE @publicationDB AS sysname
DECLARE @subscriber AS sysname
DECLARE @subscriptionDB AS sysname
SET @publication = N'NwdCustomersMerge' 
SET @publisher = N'PUBSERVER' 
SET @publicationDB = N'Northwind' 
SET @subscriber = N'SUBSERVER' 
SET @subscriptionDB = N'NorthwindReplica'

-- At the subscription database, create a pull subscription 
-- to a merge publication.
USE [NorthwindReplica]
EXEC sp_addmergepullsubscription 
    @publisher = @publisher, 
    @publication = @publication, 
    @publisher_db = @publicationDB

-- Add an agent job to synchronize the pull subscription. 
EXEC sp_addmergepullsubscription_agent 
    @publisher = @publisher, 
    @publisher_db = @publicationDB, 
    @publication = @publication, 
    @subscriber = @subscriber, 
    @subscriber_db = @subscriptionDB, 
    @distributor = @publisher
GO

-- Execute at the Publisher.
DECLARE @publication AS sysname
DECLARE @subscriber AS sysname
DECLARE @subscriptionDB AS sysname
SET @publication = N'NwdCustomersMerge' 
SET @subscriber = N'MYSUBSERVER' 
SET @subscriptionDB = N'NorthwindReplica' 

-- Add a Subscriber, using the defaults.
USE [master]
EXEC sp_addsubscriber 
  @subscriber = @subscriber

-- Add a pull subscription to a merge publication.
USE [Northwind]
EXEC sp_addmergesubscription 
  @publication = @publication, 
  @subscriber = @subscriber, 
  @subscriber_db = @subscriptionDB, 
  @subscription_type = N'pull',
  @subscriber_type = N'local',
  @sync_type = N'automatic'
GO

다음은 병합 게시에 끌어오기 구독을 만드는 앞의 스크립트를 SQL Server 2005 이상 버전에서 성공적으로 실행되도록 업그레이드하는 예제입니다. 새 매개 변수의 기본값이 명시적으로 선언되었습니다.

[!참고]

Windows 자격 증명은 sqlcmd 스크립팅 변수를 사용하여 런타임에 제공됩니다.

-- 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 at the Subscriber
DECLARE @publication  AS sysname;
DECLARE @publisher AS sysname;
DECLARE @publicationDB AS sysname;
DECLARE @login AS sysname;
DECLARE @password AS sysname;
SET @publication = N'NwdCustomersMerge'; 
SET @publisher = $(Publisher); 
SET @publicationDB = N'Northwind'; 
-- Specify the Windows account to run the Merge Agent.
SET @login = $(Login); 
-- Pass the password at runtime.
SET @password = $(Password); 

-- At the subscription database, create a pull subscription 
-- to a merge publication.
USE [NorthwindReplica]
EXEC sp_addmergepullsubscription 
  @publisher = @publisher, 
  @publication = @publication, 
  @publisher_db = @publicationDB;

-- Add an agent job to synchronize the pull subscription. 
EXEC sp_addmergepullsubscription_agent 
  @publisher = @publisher, 
  @publisher_db = @publicationDB, 
  @publication = @publication,
  @distributor = @publisher,
  @job_login = @login,
  @job_password = @password;
GO

-- Execute at the Publisher.
DECLARE @publication AS sysname;
DECLARE @subscriber AS sysname;
DECLARE @subscriptionDB AS sysname;
SET @publication = N'NwdCustomersMerge';
SET @subscriber = $(Subscriber);
SET @subscriptionDB = N'NorthwindReplica';

-- Add a pull subscription to a merge publication.
USE [Northwind]
EXEC sp_addmergesubscription 
  @publication = @publication, 
  @subscriber = @subscriber, 
  @subscriber_db = @subscriptionDB, 
  @subscription_type = N'pull',
  @subscriber_type = N'local',
  @sync_type = N'automatic';
GO