Merge replication failed due to views applied to subscriber before tables - sql-server

I am using SQL Server 2016 for pull merge replication. It inclueds tables, views, procedureds and functions.
For some databases the merge agent is tring to apply views to subscribers before the tables are in first sync, causing it to error.
Any idea why, or how to change it?
Removing all views from replication in SSMS, rebuild snapshot, sync and adding the views back will solve it. Only normally there are multiple subscribers, so that is not a good solution.

Related

Sql Server replication - add articles without using wizard

I have done transnational replication to my SQL Server 2014 instance.
There are two db servers subscribing to it. Currently I have added
only some of the tables (10 tables) as articles to replicate. However, in the future there will be new tables added to the distributor database that needs to be included as replication articles.
I know that whenever I want to add a new article to the replication, I can go to the Properties of publication and add new articles through the wizard.
However, my problem is that there are some tables added by an application through T-SQL queries.Those tables need to be replicated. So there is no way
I can go to the wizard and add those to articles.
Is there a way that I can add articles to the replication through a T-SQL? or Is there any system stored procedure or anything we can use to add articles to replication.

One-Way Replication Into Different Schema

I'm looking for the best (best practice) option for one way replication between two databases. I would like to keep this purely SQL but, can write something in C# or use an ETL tool if there are no other good options.
Current setup:
DB1 - There are three instances of this database. It is a large relational database, the schema is the same for each but, they are separate data pots (no replication). Two databases on a 2012 server and one on a 2014 server
DB2 - There are two instances of this database on seperate servers (Europe, Americas) and the data is merge replicated between the two. The publisher is the 2014 server.
The Goal:
DB2 is tied to some reports. It has one table and a small application attached to that table. Users from many different countries enter data via a small application into DB2 and generate reports out of the application.
DB1 is a relational database that has a very large application on top of it but with fewer users. If users are using the application for DB1 then they should not need to duplicate their records into DB2.
There should be one-way replication from the multiple seperate DB1s into DB2. How quickly this happens is not too important.
The important things are:
No backwards replication occurs from DB2s > DB1s (Data only flows from DB1s into one of the DB2s)
Create, Update, and Delete actions should occur in DB2 based on the results
of a comparisson with DB1 (the one way replication)
Current Approach:
I currently have a flat sql view on each DB1 database that has the same schema as the table in the DB2 db's that the data needs to go into.
The servers are also joined as linked servers.
My though was to do a sort of manually written replication script on one of the DB2 databases that calls the views from the DB1s and does the CUD actions on a timed basis.
It seems to me that there should be an easier way though!?
Any thoughts on how to do this would be very much appreciated.
Keep in mind that since several of the DB1s exist on a SQL 2012 server that there may be some issues as 2012 might not be allowed to be a publisher for replication to a 2014 server.

MS SQL Server: central database and foreign keys

I'm am currently developing one project of many to come which will be using its own database and also data from a central database.
Example:
the database "accountancy" with all accountancy package specific tables.
the database "personelladministration" with its specific tables
But we also use data which is general and will be used in all projects like "countries", "cities", ...
So we have put these tables in a separate database called "general"
We come from a db2 environment where we could create foreign keys between databases.
However, we are switching to MS SQL server where it is not possible to put foreign keys between databases.
I have seen that a workaround would be to use triggers, but I'm not convinced that is a clean solution.
Are we doing something wrong in our setup? Because it seems right to me to put tables with general data in a separate database instead of having a table "countries" in every database, that seams difficult to maintain and inefficiƫnt.
What could be a good approach to overcome this?
I would say that countries is not a terrible table to reproduce in multiple databases. I would rather duplicate static data like that than use more elaborate techniques. There is one physical schema per database in sql server and the schema can not be shared. That is why people use replication or triggers for shared data.
I can across this problem a while back. We have one database for authentication, however, those users have to be shared across multiple applications some of which have their own database.
Here is my question on this topic.
We resorted to replication and using an custom Authentication/Registration service agent to keep the data up to data.
Using views, in what Sourav_Agasti suggested in his answer, would be the most straight forward approach for static data. You can create views and indexed views and join data from databases on linked servers.
Create a loopback linked server and then create a view(if required, on each database) which accesses the table in this "central database" through this linked server. There will be a minor performance impact but it more than enough compensates by being very simiplistic.

SSIS Moving Content From Views from one database to Another

One question though lets say publisher database had 100 tables and I use Transactional Replication to move the data from those 100 tables to Subscriber Database that would be fine.
But lets say I don't want the 100 tables but i want to create 3-4 Views which contain the key information I want from those 100 tables. How Would I achieve this.
1) Firstly I guess the views need to be created on the publisher database
2) Secondly Do i need to create then 3/4 Tables in the Subscriber database which have the same columns as the view from publisher database.
3) What sort of replication or maybe even SSIS or something to move the data from the publisher view to subscriber database
Replication probably wouldn't be viable or as performant an option as creating a SSIS package for transferring data from those views and into the small set of tables in the remote database. SSIS's strongest feature is it's ability to transfer large volumes of data quickly from a source and into a destination. With a little upkeep, you could potentially just transfer the differences between the two databases at some scheduled interval and have a fairly flexible solution.
SSIS will be the better solution. You would create the tables on your target database. Then, you can create the SSIS pacakge(s) to populate the target tables.
SSIS can use queries on tables or views. And, it can also execute a stored procedure to retrieve the data.

SQL Trigger that runs ONLY at Publisher

I have an in house app that has both a Web Interface and a Desktop Interface(is an OCA using Merge Replication). We are still using SQL 2005 and have many 'Archive' tables set up. These are filled by Triggers on there relating Table. tblPersonArchive for tblPerson, etc. To keep the Replication Sets as small as possible I would like to exclude ALL of the Archive tables from replicating.
This shouldn't be an issue from a Business standpoint as that data is never accessed directly by the user's. There is literally no need for it to exist on the Desktop app that is using replication.
What I am trying to figure out, then, is how I accomplish that. My "guess" is that I set the Publication Properties --> Article Properties --> Copy User Triggers = FALSE and then exclude the Archive Tables from the Replication Set. Theoretically the Triggers will still fire, and thus still maintain, the Archive tables through the Web App and on Replication.
Unfortunately, this is only a guess at this point and I was hoping for a little reassurance before plowing in.
Could you not accomplish Publisher only triggers by using the NOT FOR REPLICATION clause in the trigger creation?

Resources