Aktualisieren von Replikationsskripts (Replikationsprogrammierung mit Transact-SQL)

Gilt für:SQL ServerAzure SQL Managed Instance

Transact-SQL-Skriptdateien können verwendet werden, um eine Replikationstopologie programmgesteuert zu konfigurieren. Weitere Informationen finden Sie unter Replikationskonzepte für gespeicherte Systemprozeduren.

Wichtig

Skripts, die von Mitgliedern der sysadmin -Rolle ausgeführt werden, müssen nicht aktualisiert werden. Dennoch empfiehlt es sich, vorhandene Skripts wie in diesem Thema beschrieben zu ändern. Geben Sie ein Konto mit Mindestberechtigungen für jeden Replikations-Agent an, wie im Abschnitt zu den für die Agents erforderlichen Berechtigungen im Thema Replication Agent Security Modelbeschrieben.

Diese Sicherheitsverbesserungen, die eine bessere Kontrolle über Berechtigungen ermöglichen, indem Sie explizit die Microsoft Windows-Konten angeben können, unter denen Replikations-Agent-Aufträge ausgeführt werden, wirken sich auf die folgenden gespeicherten Prozeduren in vorhandenen Skripts aus:

  • sp_addpublication_snapshot:

    Sie sollten nun die Windows-Anmeldeinformationen als @job_login und beim Ausführen von sp_addpublication_Momentaufnahme (Transact-SQL) angeben, um den Auftrag zu erstellen, unter dem die Momentaufnahmen-Agent im @job_password Distributor ausgeführt wird.

  • sp_addpushsubscription_agent:

    Sie sollten jetzt sp_addpushsubscription_agent (Transact-SQL) ausführen, um explizit einen Auftrag hinzuzufügen und die Windows-Anmeldeinformationen (@job_login und @job_password) anzugeben, unter denen der Verteilungs-Agent Auftrag im Distributor ausgeführt wird. In Versionen von SQL Server vor SQL Server 2005 (9.x) erfolgte dies automatisch, wenn ein Pushabonnement erstellt wurde.

  • sp_addmergepushsubscription_agent:

    Sie sollten jetzt sp_addmergepushsubscription_agent (Transact-SQL) ausführen, um einen Auftrag explizit hinzuzufügen und die Windows-Anmeldeinformationen (@job_login und @job_password) anzugeben, unter denen der Merge-Agent Auftrag im Distributor ausgeführt wird. In Versionen von SQL Server vor SQL Server 2005 (9.x) erfolgte dies automatisch, wenn ein Pushabonnement erstellt wurde.

  • sp_addpullsubscription_agent:

    Sie sollten nun die Windows-Anmeldeinformationen als @job_login und beim Ausführen von sp_addpullsubscription_agent (Transact-SQL) angeben, um den Auftrag zu erstellen, unter dem die Verteilungs-Agent beim @job_password Abonnenten ausgeführt wird.

  • sp_addmergepullsubscription_agent:

    Sie sollten nun die Windows-Anmeldeinformationen als @job_login und beim Ausführen von sp_addmergepullsubscription_agent (Transact-SQL) angeben, um den Auftrag zu erstellen, unter dem die Merge-Agent beim @job_password Abonnenten ausgeführt wird.

  • sp_addlogreader_agent:

    Sie sollten nun sp_addlogreader_agent (Transact-SQL) ausführen, um den Auftrag manuell hinzuzufügen und die Windows-Anmeldeinformationen anzugeben, unter denen der Log Reader-Agent im Distributor ausgeführt wird. In Versionen von SQL Server vor SQL Server 2005 (9.x) erfolgte dies automatisch, wenn eine Transaktionsveröffentlichung erstellt wurde.

  • sp_addqreader_agent:

    Sie sollten jetzt sp_addqreader_agent (Transact-SQL) ausführen, um den Auftrag manuell hinzuzufügen und die Windows-Anmeldeinformationen anzugeben, unter denen der Warteschlangenleser-Agent im Distributor ausgeführt wird. In Versionen von SQL Server vor SQL Server 2005 (9.x) erfolgte dies automatisch, wenn eine Transaktionsveröffentlichung erstellt wurde, die die Aktualisierung in die Warteschlange unterstützte.

Im in SQL Server 2005 (9.x) eingeführten Sicherheitsmodell stellen Replikations-Agents immer Verbindungen mit der lokalen Instanz von SQL Server mit Windows-Authentifizierung mithilfe der in @job_name und .@job_password Informationen zu den Anforderungen für beim Ausführen von Replikations-Agentaufträgen verwendeten Windows-Konten finden Sie unter Replication Agent Security Model.

Wichtig

Benutzer sollten nach Möglichkeit dazu aufgefordert werden, Anmeldeinformationen zur Laufzeit anzugeben. Wenn Anmeldeinformationen in einer Skriptdatei gespeichert werden, müssen Sie sicherstellen, dass die Datei geschützt ist.

So aktualisieren Sie Skripts, mit denen eine Momentaufnahme- oder eine Transaktionsveröffentlichung konfiguriert werden

  1. Führen Sie im vorhandenen Skript vor sp_addpublication (Transact-SQL) sp_addlogreader_agent (Transact-SQL) im Publisher in der Publikationsdatenbank aus. Geben Sie die Windows-Anmeldeinformationen für @job_name und @job_password an, unter denen der Protokolllese-Agent ausgeführt wird. Wenn der Agent sql Server-Authentifizierung beim Herstellen einer Verbindung mit dem Publisher verwendet, müssen Sie auch einen Wert von 0 für @publisher_security_mode und die SQL Server-Anmeldeinformationen für @publisher_login und @publisher_password. Dadurch wird ein Auftrag des Protokolllese-Agents für die Veröffentlichungsdatenbank erstellt.

    Hinweis

    Dieser Schritt muss nur bei Transaktionsveröffentlichungen, nicht bei Momentaufnahmeveröffentlichungen ausgeführt werden.

  2. (Optional) Führen Sie vor sp_addpublication (Transact-SQL) sp_addqreader_agent (Transact-SQL) beim Distributor in der Verteilungsdatenbank aus. Geben Sie die Windows-Anmeldeinformationen, unter denen der Warteschlangenlese-Agent ausgeführt wird, für @job_name und @job_password an. Dadurch wird ein Auftrag des Warteschlangenlese-Agents für den Verteiler erstellt.

    Hinweis

    Dieser Schritt muss nur bei Transaktionsveröffentlichungen ausgeführt werden, die Abonnenten mit verzögertem Update über eine Warteschlange unterstützen.

  3. (Optional) Aktualisieren Sie die Ausführung von sp_addpublication (Transact-SQL), um alle Nicht-Standardwerte für Parameter festzulegen, die neue Replikationsfunktionen implementieren.

  4. Führen Sie nach sp_addpublication (Transact-SQL) sp_addpublication_Momentaufnahme (Transact-SQL) im Publisher in der Publikationsdatenbank aus. Geben Sie @publication und die Windows-Anmeldeinformationen, unter denen der Momentaufnahmen-Agent ausgeführt wird, für @job_name und @job_password an. Wenn der Agent sql Server-Authentifizierung beim Herstellen einer Verbindung mit dem Publisher verwendet, müssen Sie auch einen Wert von 0 für @publisher_security_mode und die SQL Server-Anmeldeinformationen für @publisher_login und @publisher_password. Dadurch wird ein Auftrag des Momentaufnahme-Agents für die Veröffentlichung erstellt.

  5. (Optional) Aktualisieren Sie die Ausführung von sp_addarticle (Transact-SQL), um alle Nicht-Standardwerte für Parameter festzulegen, die neue Replikationsfunktionen implementieren.

So aktualisieren Sie Skripts, mit denen einer Momentaufnahme- oder Transaktionsveröffentlichung Abonnements hinzugefügt werden

  1. Nach dem Ausführen der gespeicherten Prozedur, mit der das Abonnement erstellt wird, müssen Sie die gespeicherte Prozedur ausführen, mit der der Auftrag des Verteilungs-Agents erstellt wird, damit das Abonnement synchronisiert wird. Welche gespeicherte Prozedur Sie verwenden, hängt vom Typ des Abonnements ab.

So aktualisieren Sie Skripts, mit denen eine Mergeveröffentlichung konfiguriert wird

  1. (Optional) Aktualisieren Sie im vorhandenen Skript die Ausführung von sp_addmergepublication (Transact-SQL), um alle Nicht-Standardwerte für Parameter festzulegen, die neue Replikationsfunktionen implementieren.

  2. Führen Sie nach sp_addmergepublication (Transact-SQL) sp_addpublication_Momentaufnahme (Transact-SQL) im Publisher in der Publikationsdatenbank aus. Geben Sie @publication und die Windows-Anmeldeinformationen, unter denen der Momentaufnahmen-Agent ausgeführt wird, für @job_name und @job_password an. Wenn der Agent sql Server-Authentifizierung beim Herstellen einer Verbindung mit dem Publisher verwendet, müssen Sie auch einen Wert von 0 für @publisher_security_modeund die SQL Server-Anmeldeinformationen für @publisher_login und @publisher_password. Dadurch wird ein Auftrag des Momentaufnahme-Agents für die Veröffentlichung erstellt.

  3. (Optional) Aktualisieren Sie die Ausführung von sp_addmergearticle (Transact-SQL), um alle Nicht-Standardwerte für Parameter festzulegen, die neue Replikationsfunktionen implementieren.

So aktualisieren Sie Skripts, mit denen einer Mergeveröffentlichung Abonnements hinzugefügt werden

  1. Nach dem Ausführen der gespeicherten Prozedur, mit der das Abonnement erstellt wird, müssen Sie die gespeicherte Prozedur ausführen, mit der der Auftrag des Merge-Agents erstellt wird, damit das Abonnement synchronisiert wird. Welche gespeicherte Prozedur Sie verwenden, hängt vom Typ des Abonnements ab.

Beispiele

.A SQL Server 2000-Skript zum Erstellen einer Transaktionsveröffentlichung

Es folgt ein Beispiel für ein SQL Server 2000 (8.x)-Skript, das eine Transaktionsveröffentlichung für die Produkttabelle erstellt. Diese Veröffentlichung unterstützt sofortige Updates mit Updates über eine Warteschlange als Failover. Standardparameter wurden zwecks besserer Lesbarkeit entfernt.

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

B. Skript zum Erstellen einer Transaktionsveröffentlichung für SQL Server 2005 und höher

Im Folgenden sehen Sie ein Beispiel für ein Upgrade des vorherigen Skripts, das eine Transaktionsveröffentlichung erstellt, um erfolgreich für SQL Server 2005 (9.x) und höhere Versionen auszuführen. Diese Veröffentlichung unterstützt sofortige Updates mit Updates über eine Warteschlange als Failover. Standardwerte für neue Parameter wurden explizit deklariert.

Hinweis

Windows-Anmeldeinformationen werden zur Laufzeit mithilfe von sqlcmd -Skriptvariablen angegeben.

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

C. SQL Server 2000-Skript zum Erstellen einer Mergeveröffentlichung

Im Folgenden sehen Sie ein Beispiel für ein SQL Server 2000 (8.x)-Skript, das eine Zusammenführungsveröffentlichung für die Tabelle "Customers" erstellt. Standardparameter wurden zwecks besserer Lesbarkeit entfernt.

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

D: Skript für SQL Server 2005 und höher zum Erstellen einer Mergeveröffentlichung

Im Folgenden sehen Sie ein Beispiel für das vorherige Skript, das eine Zusammenführungsveröffentlichung erstellt und für SQL Server 2005 (9.x) und höhere Versionen erfolgreich ausgeführt wird. Standardwerte für neue Parameter wurden explizit deklariert.

Hinweis

Windows-Anmeldeinformationen werden zur Laufzeit mithilfe von sqlcmd -Skriptvariablen angegeben.

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

E. SQL Server 2000-Skript zum Erstellen eines Pushabonnements für eine Transaktionsveröffentlichung

Im Folgenden sehen Sie ein Beispiel für ein SQL Server 2000 (8.x)-Skript, das ein Pushabonnement für eine transaktionsbasierte Publikation erstellt. Standardparameter wurden zwecks besserer Lesbarkeit entfernt.

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

F. Skript für SQL Server 2005 und höher zum Erstellen eines Pushabonnements für eine Transaktionsveröffentlichung

Im Folgenden sehen Sie ein Beispiel für das vorherige Skript, das ein Pushabonnement für eine transaktionsbasierte Publikation erstellt und für SQL Server 2005 (9.x) und höhere Versionen erfolgreich ausgeführt wird. Standardwerte für neue Parameter wurden explizit deklariert.

Hinweis

Windows-Anmeldeinformationen werden zur Laufzeit mithilfe von sqlcmd -Skriptvariablen angegeben.

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

G. SQL Server 2000-Skript zum Erstellen eines Pushabonnements für eine Mergeveröffentlichung

Im Folgenden sehen Sie ein Beispiel für ein SQL Server 2000 (8.x)-Skript, das ein Pushabonnement für eine Zusammenführungsveröffentlichung erstellt. Standardparameter wurden zwecks besserer Lesbarkeit entfernt.

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

H. Skript für SQL Server 2005 und höher zum Erstellen eines Pushabonnements für eine Mergeveröffentlichung

Nachfolgend sehen Sie ein Beispiel für das vorherige Skript, das ein Pushabonnement für eine Zusammenführungsveröffentlichung erstellt und für SQL Server 2005 (9.x) und höhere Versionen erfolgreich ausgeführt wird. Standardwerte für neue Parameter wurden explizit deklariert.

Hinweis

Windows-Anmeldeinformationen werden zur Laufzeit mithilfe von sqlcmd -Skriptvariablen angegeben.

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

I. SQL Server 2000-Skript zum Erstellen eines Pullabonnements für eine Transaktionsveröffentlichung

Im Folgenden sehen Sie ein Beispiel für ein SQL Server 2000 (8.x)-Skript, das ein Pullabonnement für eine transaktionsbasierte Publikation erstellt. Standardparameter wurden zwecks besserer Lesbarkeit entfernt.

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

J. Skript für SQL Server 2005 und höher zum Erstellen eines Pullabonnements für eine Transaktionsveröffentlichung

Im Folgenden sehen Sie ein Beispiel für das vorherige Skript, das ein Pullabonnement für eine transaktionsbasierte Publikation erstellt und für SQL Server 2005 (9.x) und höhere Versionen erfolgreich ausgeführt wird. Standardwerte für neue Parameter wurden explizit deklariert.

Hinweis

Windows-Anmeldeinformationen werden zur Laufzeit mithilfe von sqlcmd -Skriptvariablen angegeben.

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

K. SQL Server 2000-Skript zum Erstellen eines Pullabonnements für eine Mergeveröffentlichung

Im Folgenden sehen Sie ein Beispiel für ein SQL Server 2000 (8.x)-Skript, das ein Pullabonnement für eine Zusammenführungsveröffentlichung erstellt. Standardparameter wurden zwecks besserer Lesbarkeit entfernt.

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

L. Skript für SQL Server 2005 und höher zum Erstellen eines Pullabonnements für eine Mergeveröffentlichung

Im Folgenden sehen Sie ein Beispiel für das vorherige Skript, das ein Pullabonnement für eine Seriendruckveröffentlichung erstellt und für SQL Server 2005 (9.x) und höhere Versionen erfolgreich ausgeführt wird. Standardwerte für neue Parameter wurden explizit deklariert.

Hinweis

Windows-Anmeldeinformationen werden zur Laufzeit mithilfe von sqlcmd -Skriptvariablen angegeben.

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

Weitere Informationen

Erstellen einer Veröffentlichung
Erstellen eines Pushabonnements
Create a Pull Subscription
Anzeigen und Ändern von Replikationssicherheitseinstellungen
MSSQL_ENG021797
MSSQL_ENG021798
Replication System Stored Procedures Concepts
Aktualisieren von replizierten Datenbanken