Technology Encyclopedia Home >How to create a publication subscription in SQL Server?

How to create a publication subscription in SQL Server?

To create a publication subscription in SQL Server, you need to set up a replication topology where a Publisher database shares data with one or more Subscriber databases. This is typically done using Transactional Replication or Merge Replication. Below are the steps and an example:

Steps to Create a Publication Subscription:

  1. Configure the Publisher:

    • Enable the database for replication.
    • Create a publication (a collection of articles, which can be tables, views, stored procedures, etc.).
    • Define the replication type (e.g., Transactional or Merge).
  2. Create a Subscription:

    • On the Subscriber, create a subscription to the Publisher's publication.
    • Specify the subscription type (Push or Pull).
    • Configure the connection details between the Publisher and Subscriber.
  3. Initialize the Subscription:

    • The Subscriber database must be initialized with the schema and initial data from the Publisher. This can be done via a snapshot or by copying existing data.
  4. Start the Distribution Agent:

    • For Transactional Replication, the Distribution Agent moves transactions from the Publisher to the Subscriber.
    • For Merge Replication, the Merge Agent synchronizes changes between the Publisher and Subscriber.

Example:

Suppose you have a Publisher database named PublisherDB and a Subscriber database named SubscriberDB. You want to replicate a table named Orders from PublisherDB to SubscriberDB.

  1. Enable Replication on the Publisher:

    EXEC sp_replicationdboption @dbname = 'PublisherDB', @optname = 'publish', @value = 'true';
    
  2. Create a Publication:

    EXEC sp_addpublication 
        @publication = 'OrdersPublication',
        @description = 'Replicates the Orders table',
        @sync_method = 'native',
        @repl_freq = 'continuous',
        @status = 'active',
        @independent_agent = 'true';
    
  3. Add an Article to the Publication:

    EXEC sp_addarticle 
        @publication = 'OrdersPublication',
        @article = 'Orders',
        @source_object = 'Orders',
        @type = 'logbased';
    
  4. Create a Subscription on the Subscriber:

    EXEC sp_addsubscription 
        @publication = 'OrdersPublication',
        @subscriber = 'SubscriberServer',
        @destination_db = 'SubscriberDB',
        @subscription_type = 'Push',
        @sync_type = 'automatic';
    
  5. Initialize the Subscription:

    • Use a snapshot agent to initialize the Subscriber. This can be done via SQL Server Management Studio (SSMS) or by running the snapshot agent manually.

Using Tencent Cloud Services:

If you are using Tencent Cloud's SQL Server instances, you can leverage Tencent Cloud Database for SQL Server to set up replication. Tencent Cloud provides managed SQL Server services that simplify the configuration and management of replication, including snapshot agents and distribution agents. You can use Tencent Cloud Database Migration Service (DTS) to assist in initializing the Subscriber database with the Publisher's data.

For more advanced replication scenarios, Tencent Cloud's Tencent Cloud Database Proxy can help manage connections and improve performance between the Publisher and Subscriber. Additionally, Tencent Cloud Monitoring can be used to track the health and performance of the replication process.