Replication between Different Schemas
By: Dan Stewart
November 25, 2011
How do I replicate tables from different schemas?
Set the destination owner in the publication to be the schema name in the subscriber.
Here's the scenario, you have two databases. The first database is the publisher. The second database is the subscriber. You want to replicate tables from the publisher to the subscriber. You do not want to give the subscriber full rights to the replicated tables.
So you want to replicate the tables to a special schema on the subscriber. Setting up a schema will give you the power to control what the subscriber can do with the tables.
I am not a DBA, but I play one at work sometimes. I welcome your feedback.
Open SQL Server 2008. Create two databases. Name the first PublisherDatabase and the second SubscriberDatabase.
Inside the PublisherDatabase, run the following script to create a table.
USE PublisherDatabase GO CREATE TABLE Person ( Id UNIQUEIDENTIFIER PRIMARY KEY, FirstName NVARCHAR(50) NULL, LastName NVARCHAR(50) NULL ) ON [PRIMARY] GO
Let's insert a person.
INSERT INTO Person (Id, FirstName, LastName) VALUES (NEWID(), 'Dan', 'Stewart')
The goal is to replicate this table to the SubscriberDatabase, but the user of the SubscriberDatabase should only have select rights on the table. They cannot insert, update, or delete records from the table. Creating a schema and replicating the table to this schema is a good way to limit rights.
Let's create the schema on the subscriber database.
- On the SubscriberDatabase, go to the Security folder, Schemas.
- Right-click the Schemas folder and choose new schema.
- Name it "repl".
- Make "dbo" the owner.
- On the Permissions page, search for your user.
- Allow your user to Control, Select, and View definition.
- Deny everything else.
- Click OK.
We now have a schema named "repl" and our user has limited rights in the schema.
Let's set up replication.
- Right-click on Local Publications and choose New Publication…
- This launches the New Publication Wizard.
- Click next through the initial screens until you get to the Publication Database.
- Choose PublisherDatabase and click next.
- Choose transactional publication and click next.
- Checkmark the tables that you want included in the replication.
- Click on the article properties dropdown and select All table articles.
- There are a ton of settings here. I would at least set copy nonclustered indexes to true.
- Keep all of the other defaulted values.
- Change the destination owner to repl.
- Click next until you get to Snapshot Agent. Check the box to create the snapshot immediately.
- Click next and choose a Windows account to run the replication under.
- Click Finish to skip to the end.
- Give the replication a name.
- Click finish.
You might get an error that the SQL Agent is not started. Just right-click on the SQL Server Agent and start it.
Now we need to set up a subscription.
- Right-click on the publication and choose New Subscriptions…
- The New Subscription Wizard will launch.
- Click next and choose the publication.
- Click next until you get to the Subscribers page.
- Choose the SubscriberDatabase.
- Click next and choose a user to run the distribution agent under.
- Click Finish to skip to the end.
- Click Finish.
You would think the Person table would be in the SubscriberDatabase right? Well, we have one more step to go.
- Right-click the publication.
- View Snapshot Agent Status.
- Click Start.
You can click the monitor button to watch the process.
Eventually you will replicate the table to the SubscriberDatabase.
Congratulations, you have now replicated a table from one schema to a different schema.
Don't forget to use the schema name with querying the table.
USE SubscriberDatabase SELECT * FROM repl.Person