How to Create an Upload Subscription with MS SQL Server Replication
I ran across this problem recently with a mobile application I am writing. I wanted to do some activity and system logs on the device and have it sync to the server with the publication I had setup, but I did not want each devices log data being synced to all the other devices. I wanted it simply to sync and upload the data only.
Sadly, this is where the problem comes in. There are generally only two options when using MS SQL Server replication: bi-directional and download only. Neither of these are really helpful for my problem because I need an upload only option. So how did I solve my problem?
I first thought I could have a job that ran often that would archive the tables and therefore, clear them out. That would reduce the amount of records being synced to all the devices. The problem with this is there is still the overhead of processing all the inserts and deletes. Another problem is the devices sync very often due to them needing the data as near to real time as possible, so there is still a possibility that unneeded data is getting pushed to multiple devices. Also, having a job do this just seemed like a really hacked up way of solving this problem, although I am not sure if my solution is much better.
Along with my production database, I also have an archive database where I want these logs to go anyway. So to get my upload only replication working, I did the a trick with an INSTEAD OF trigger. On my tables, I setup the trigger where it would write the data to my archive table instead of the table I had setup for replication. This allows the data to bypass the production table, and not worry with getting replicated to all the other devices. Since an INSTEAD OF trigger will always execute first, and I am not actually inserting anything into my production table, the replication triggers are never called for insert.
To keep the tables clean on the devices, I cleanup (clear out) the tables on login after the initial sync.
I realize this is probably a really hacked up solution, but I searched around and could not find anything better and this works. So if you have a better solution, please let me know.