title | description | author | ms.author | ms.date | ms.service | ms.subservice | ms.topic | ms.custom | helpviewer_keywords | monikerRange | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Create a push subscription |
Learn how to create a push subscription in SQL Server by using SQL Server Management Studio, Transact-SQL, or Replication Management Objects. |
MashaMSFT |
mathoma |
09/25/2024 |
sql |
replication |
how-to |
|
|
=azuresqldb-current||>=sql-server-2016 |
[!INCLUDEsql-asdb] This topic describes how to create a push subscription in [!INCLUDEssnoversion] by using [!INCLUDEssManStudioFull], [!INCLUDEtsql], or Replication Management Objects (RMO). For information about creating a push subscription for a non- [!INCLUDEssNoVersion] Subscriber, see Create a subscription for a non-SQL Server Subscriber.
[!INCLUDEazure-sql-db-replication-supportability-note]
Create a push subscription at the Publisher or the Subscriber by using the New Subscription Wizard. Follow the pages in the wizard to:
-
Specify the Publisher and publication.
-
Select where replication agents will run. For a push subscription, select Run all agents at the Distributor (push subscriptions) on the Distribution Agent Location page or Merge Agent Location page, depending on the type of publication.
-
Specify Subscribers and subscription databases.
-
Specify the logins and passwords used for connections made by replication agents:
-
For subscriptions to snapshot and transactional publications, specify credentials on the Distribution Agent Security page.
-
For subscriptions to merge publications, specify credentials on the Merge Agent Security page.
For information about the permissions that each agent requires, see Replication agent security model.
-
-
Specify a synchronization schedule and when the Subscriber should be initialized.
-
Specify additional options for merge publications: subscription type and values for parameterized filtering.
-
Specify additional options for transactional publications that allow updating subscriptions. One option is to decide whether Subscribers should commit changes at the Publisher immediately or write them to a queue. Another option is setting up credentials used to connect from the Subscriber to the Publisher.
-
Optionally, script the subscription.
-
Connect to the Publisher in [!INCLUDEmsCoName] [!INCLUDEssManStudioFull], and then expand the server node.
-
Expand the Replication folder, and then expand the Local Publications folder.
-
Right-click the publication for which you want to create one or more subscriptions, and then select New Subscriptions.
-
Complete the pages in the New Subscription Wizard.
-
Connect to the Subscriber in [!INCLUDEssManStudioFull], and then expand the server node.
-
Expand the Replication folder.
-
Right-click the Local Subscriptions folder, and then select New Subscriptions.
-
On the Publication page of the New Subscription Wizard, select <Find SQL Server Publisher> or <Find Oracle Publisher> from the Publisher drop-down list.
-
Connect to the Publisher in the Connect to Server dialog box.
-
Select a publication on the Publication page.
-
Complete the pages in the New Subscription Wizard.
You can create push subscriptions programmatically by using replication stored procedures. The stored procedures used will depend on the type of publication to which the subscription belongs.
Important
When possible, prompt users to enter security credentials at runtime. If you must store credentials in a script file, you must secure the file to prevent unauthorized access.
-
At the Publisher on the publication database, verify that the publication supports push subscriptions by running sp_helppublication.
-
If the value of allow_push is 1, push subscriptions are supported.
-
If the value of allow_push is 0, run sp_changepublication. Specify allow_push for @property and true for @value.
-
-
At the Publisher on the publication database, run sp_addsubscription. Specify @publication, @subscriber, and @destination_db. Specify a value of push for @subscription_type. For information about how to update subscriptions, see Create an updatable subscription to a transactional publication.
-
At the Publisher on the publication database, run sp_addpushsubscription_agent. Specify the following:
-
The @subscriber, @subscriber_db, and @publication parameters.
-
The [!INCLUDEmsCoName] Windows credentials under which the Distribution Agent at the Distributor runs for @job_login and @job_password.
[!NOTE] Connections made through Windows Integrated Authentication always use the Windows credentials specified by @job_login and @job_password. The Distribution Agent always makes the local connection to the Distributor by using Windows Integrated Authentication. By default, the agent will connect to the Subscriber by using Windows Integrated Authentication.
-
(Optional) A value of 0 for @subscriber_security_mode and the [!INCLUDEmsCoName] [!INCLUDEssNoVersion] login information for @subscriber_login and @subscriber_password. Specify these parameters if you need to use SQL Server Authentication when connecting to the Subscriber.
-
A schedule for the Distribution Agent job for this subscription. For more information, see Specify synchronization schedules.
-
Important
When you're creating a push subscription at a Publisher with a remote Distributor, the values supplied for all parameters, including job_login and job_password, are sent to the Distributor as plain text. You should encrypt the connection between the Publisher and its remote Distributor before running this stored procedure. For more information, see Enable encrypted connections to the database engine (SQL Server Configuration Manager).
-
At the Publisher on the publication database, verify that the publication supports push subscriptions by running sp_helpmergepublication.
-
If the value of allow_push is 1, the publication supports push subscriptions.
-
If the value of allow_push is not 1, run sp_changemergepublication. Specify allow_push for @property and true for @value.
-
-
At the Publisher on the publication database, run sp_addmergesubscription. Specify the following parameters:
-
@publication. This is the name of the publication.
-
@subscriber_type. For a client subscription, specify local. For a server subscription, specify global.
-
@subscription_priority. For a server subscription, specify a priority for the subscription (0.00 to 99.99).
For more information, see Advanced merge replication conflict detection and resolution.
-
-
At the Publisher on the publication database, run sp_addmergepushsubscription_agent. Specify the following:
-
The @subscriber, @subscriber_db, and @publication parameters.
-
The Windows credentials under which the Merge Agent at the Distributor runs for @job_login and @job_password.
[!NOTE] Connections made through Windows Integrated Authentication always use the Windows credentials specified by @job_login and @job_password. The Merge Agent always makes the local connection to the Distributor by using Windows Integrated Authentication. By default, the agent will connect to the Subscriber by using Windows Integrated Authentication.
-
(Optional) A value of 0 for @subscriber_security_mode and the [!INCLUDEssNoVersion] login information for @subscriber_login and @subscriber_password. Specify these parameters if you need to use SQL Server Authentication when connecting to the Subscriber.
-
(Optional) A value of 0 for @publisher_security_mode and the [!INCLUDEssNoVersion] login information for @publisher_login and @publisher_password. Specify these values if you need to use SQL Server Authentication when connecting to the Publisher.
-
A schedule for the Merge Agent job for this subscription. For more information, see Specify synchronization schedules.
-
Important
When you're creating a push subscription at a Publisher with a remote Distributor, the values supplied for all parameters, including job_login and job_password, are sent to the Distributor as plain text. You should encrypt the connection between the Publisher and its remote Distributor before running this stored procedure. For more information, see Enable encrypted connections to the database engine (SQL Server Configuration Manager).
The following example creates a push subscription to a transactional publication. Login and password values are supplied at runtime through sqlcmd scripting variables.
:::code language="sql" source="codesnippet/tsql/create-a-push-subscription_1.sql":::
The following example creates a push subscription to a merge publication. Login and password values are supplied at runtime through sqlcmd scripting variables.
:::code language="sql" source="codesnippet/tsql/create-a-push-subscription_2.sql":::
You can create push subscriptions programmatically by using Replication Management Objects (RMO). The RMO classes that you use to create a push subscription depend on the type of publication to which the subscription is created.
Important
When possible, prompt users to enter security credentials at runtime. If you must store credentials, use the cryptographic services that the [!INCLUDEmsCoName] Windows .NET Framework provides.
-
Create a connection to the Publisher by using the xref:Microsoft.SqlServer.Management.Common.ServerConnection class.
-
Create an instance of the xref:Microsoft.SqlServer.Replication.TransPublication class by using the Publisher connection from step 1. Specify xref:Microsoft.SqlServer.Replication.Publication.Name%2A, xref:Microsoft.SqlServer.Replication.Publication.DatabaseName%2A, and xref:Microsoft.SqlServer.Replication.ReplicationObject.ConnectionContext%2A.
-
Call the xref:Microsoft.SqlServer.Replication.ReplicationObject.LoadProperties%2A method. If this method returns false, either the properties specified in step 2 are incorrect or the publication does not exist on the server.
-
Perform a bitwise logical AND (& in Visual C# and And in Visual Basic) between the xref:Microsoft.SqlServer.Replication.Publication.Attributes%2A property and xref:Microsoft.SqlServer.Replication.PublicationAttributes.AllowPush. If the result is xref:Microsoft.SqlServer.Replication.PublicationAttributes.None, set xref:Microsoft.SqlServer.Replication.Publication.Attributes%2A to the result of a bitwise logical OR (| in Visual C# and Or in Visual Basic) between xref:Microsoft.SqlServer.Replication.Publication.Attributes%2A and xref:Microsoft.SqlServer.Replication.PublicationAttributes.AllowPush. Then, call xref:Microsoft.SqlServer.Replication.ReplicationObject.CommitPropertyChanges%2A to enable push subscriptions.
-
If the subscription database does not exist, create it by using the xref:Microsoft.SqlServer.Management.Smo.Database class. For more information, see Creating, altering, and removing databases.
-
Create an instance of the xref:Microsoft.SqlServer.Replication.TransSubscription class.
-
Set the following subscription properties:
-
The xref:Microsoft.SqlServer.Management.Common.ServerConnection to the Publisher created in step 1 for xref:Microsoft.SqlServer.Replication.ReplicationObject.ConnectionContext%2A.
-
Name of the subscription database for xref:Microsoft.SqlServer.Replication.Subscription.SubscriptionDBName%2A.
-
Name of the Subscriber for xref:Microsoft.SqlServer.Replication.Subscription.SubscriberName%2A.
-
Name of the publication database for xref:Microsoft.SqlServer.Replication.Subscription.DatabaseName%2A.
-
Name of the publication for xref:Microsoft.SqlServer.Replication.Subscription.PublicationName%2A.
-
The xref:Microsoft.SqlServer.Replication.IProcessSecurityContext.Login%2A and xref:Microsoft.SqlServer.Replication.IProcessSecurityContext.Password%2A fields of xref:Microsoft.SqlServer.Replication.Subscription.SynchronizationAgentProcessSecurity%2A to provide the credentials for the [!INCLUDEmsCoName] Windows account under which the Distribution Agent runs at the Distributor. This account is used to make local connections to the Distributor and to make remote connections by using Windows Authentication.
[!NOTE] Setting xref:Microsoft.SqlServer.Replication.Subscription.SynchronizationAgentProcessSecurity%2A is not required when the subscription is created by a member of the sysadmin fixed server role, but we recommend it. In this case, the agent will impersonate the SQL Server Agent account. For more information, see Replication Agent security model.
-
(Optional) A value of true (the default) for xref:Microsoft.SqlServer.Replication.Subscription.CreateSyncAgentByDefault%2A to create an agent job that is used to synchronize the subscription. If you specify false, the subscription can only be synchronized programmatically.
-
(Optional) Set the xref:Microsoft.SqlServer.Replication.ConnectionSecurityContext.WindowsAuthentication%2A to False, xref:Microsoft.SqlServer.Replication.ConnectionSecurityContext.SqlStandardLogin%2A and xref:Microsoft.SqlServer.Replication.ConnectionSecurityContext.SqlStandardPassword%2A or xref:Microsoft.SqlServer.Replication.ConnectionSecurityContext.SecureSqlStandardPassword%2A fields of xref:Microsoft.SqlServer.Replication.Subscription.SubscriberSecurity%2A when using SQL Server Authentication to connect to the Subscriber.
-
-
Call the xref:Microsoft.SqlServer.Replication.Subscription.Create%2A method.
Important
When you're creating a push subscription at a Publisher with a remote Distributor, the values supplied for all properties, including xref:Microsoft.SqlServer.Replication.Subscription.SynchronizationAgentProcessSecurity%2A, are sent to the Distributor as plain text. You should encrypt the connection between the Publisher and its remote Distributor before calling the xref:Microsoft.SqlServer.Replication.Subscription.Create%2A method. For more information, see Enable encrypted connections to the database engine (SQL Server Configuration Manager).
-
Create a connection to the Publisher by using the xref:Microsoft.SqlServer.Management.Common.ServerConnection class.
-
Create an instance of the xref:Microsoft.SqlServer.Replication.MergePublication class by using the Publisher connection from step 1. Specify xref:Microsoft.SqlServer.Replication.Publication.Name%2A, xref:Microsoft.SqlServer.Replication.Publication.DatabaseName%2A, and xref:Microsoft.SqlServer.Replication.ReplicationObject.ConnectionContext%2A.
-
Call the xref:Microsoft.SqlServer.Replication.ReplicationObject.LoadProperties%2A method. If this method returns false, either the properties specified in step 2 are incorrect or the publication does not exist on the server.
-
Perform a bitwise logical AND (& in Visual C# and And in Visual Basic) between the xref:Microsoft.SqlServer.Replication.Publication.Attributes%2A property and xref:Microsoft.SqlServer.Replication.PublicationAttributes.AllowPush. If the result is xref:Microsoft.SqlServer.Replication.PublicationAttributes.None, set xref:Microsoft.SqlServer.Replication.Publication.Attributes%2A to the result of a bitwise logical OR (| in Visual C# and Or in Visual Basic) between xref:Microsoft.SqlServer.Replication.Publication.Attributes%2A and xref:Microsoft.SqlServer.Replication.PublicationAttributes.AllowPush. Then, call xref:Microsoft.SqlServer.Replication.ReplicationObject.CommitPropertyChanges%2A to enable push subscriptions.
-
If the subscription database does not exist, create it by using the xref:Microsoft.SqlServer.Management.Smo.Database class. For more information, see Creating, altering, and removing databases.
-
Create an instance of the xref:Microsoft.SqlServer.Replication.MergeSubscription class.
-
Set the following subscription properties:
-
The xref:Microsoft.SqlServer.Management.Common.ServerConnection to the Publisher created in step 1 for xref:Microsoft.SqlServer.Replication.ReplicationObject.ConnectionContext%2A.
-
Name of the subscription database for xref:Microsoft.SqlServer.Replication.Subscription.SubscriptionDBName%2A.
-
Name of the Subscriber for xref:Microsoft.SqlServer.Replication.Subscription.SubscriberName%2A.
-
Name of the publication database for xref:Microsoft.SqlServer.Replication.Subscription.DatabaseName%2A.
-
Name of the publication for xref:Microsoft.SqlServer.Replication.Subscription.PublicationName%2A.
-
The xref:Microsoft.SqlServer.Replication.IProcessSecurityContext.Login%2A and xref:Microsoft.SqlServer.Replication.IProcessSecurityContext.Password%2A fields of xref:Microsoft.SqlServer.Replication.Subscription.SynchronizationAgentProcessSecurity%2A to provide the credentials for the [!INCLUDEmsCoName] Windows account under which the Merge Agent runs at the Distributor. This account is used to make local connections to the Distributor and to make remote connections through Windows Authentication.
[!NOTE] Setting xref:Microsoft.SqlServer.Replication.Subscription.SynchronizationAgentProcessSecurity%2A is not required when the subscription is created by a member of the sysadmin fixed server role, but we recommend it. In this case, the agent will impersonate the SQL Server Agent account. For more information, see Replication Agent security model.
-
(Optional) A value of true (the default) for xref:Microsoft.SqlServer.Replication.Subscription.CreateSyncAgentByDefault%2A to create an agent job that is used to synchronize the subscription. If you specify false, the subscription can only be synchronized programmatically.
-
(Optional) Set the xref:Microsoft.SqlServer.Replication.ConnectionSecurityContext.SqlStandardLogin%2A and xref:Microsoft.SqlServer.Replication.ConnectionSecurityContext.SqlStandardPassword%2A or xref:Microsoft.SqlServer.Replication.ConnectionSecurityContext.SecureSqlStandardPassword%2A fields of xref:Microsoft.SqlServer.Replication.Subscription.SubscriberSecurity%2A when using SQL Server Authentication to connect to the Subscriber.
-
(Optional) Set the xref:Microsoft.SqlServer.Replication.ConnectionSecurityContext.WindowsAuthentication%2A to False, xref:Microsoft.SqlServer.Replication.ConnectionSecurityContext.SqlStandardLogin%2A and xref:Microsoft.SqlServer.Replication.ConnectionSecurityContext.SqlStandardPassword%2A or xref:Microsoft.SqlServer.Replication.ConnectionSecurityContext.SecureSqlStandardPassword%2A fields of xref:Microsoft.SqlServer.Replication.PullSubscription.PublisherSecurity%2A when using SQL Server Authentication to connect to the Publisher.
-
-
Call the xref:Microsoft.SqlServer.Replication.Subscription.Create%2A method.
Important
When you're creating a push subscription at a Publisher with a remote Distributor, the values supplied for all properties, including xref:Microsoft.SqlServer.Replication.Subscription.SynchronizationAgentProcessSecurity%2A, are sent to the Distributor as plain text. You should encrypt the connection between the Publisher and its remote Distributor before calling the xref:Microsoft.SqlServer.Replication.Subscription.Create%2A method. For more information, see Enable encrypted connections to the database engine (SQL Server Configuration Manager).
This example creates a new push subscription to a transactional publication. The Windows account credentials that you use to run the Distribution Agent job are passed at runtime.
[!code-csHowTo#rmo_CreateTranPushSub]
[!code-vbHowTo#rmo_vb_CreateTranPushSub]
This example creates a new push subscription to a merge publication. The Windows account credentials that you use to run the Merge Agent job are passed at runtime.
[!code-csHowTo#rmo_CreateMergePushSub]
[!code-vbHowTo#rmo_vb_CreateMergePushSub]