While many of you might have tested and implemented the SQL Server Replication stored procedure execution already, many might still be considering it and surely would be Googling or Binging around to find the right solution. I have done some testing that might help you .For testing I have I have used instances of SQL Server 2005 SP3 (CU2) and SQL Server is 2008 SP1.


Imagine a situation where your databases are in TBs or in hundreds of GBs and you are using transactional Replication without updatable subscription option. The subscribers are used for reporting propose only. The OLTP database goes thorough millions of inserts and updates everyday (with no deletes).Since there are no big batches of inserts and updates, it does not make sense to replicate the execution of stored procedure to subscriber in this case. However, if we decide to archive the data to keep the huge database size in control, then it makes sense to replicate the execution of stored procedures. Yes this is a real life scenario .We have a very large OLTP environment where millions of small queries do inserts and updates (No deletes) .The same is replicated to other subscribers .The data is so much that most of the time we firefight latency . So because of the size of the data we started archiving it .Archiving had two steps: Archive and then delete the data which is archived. All the deletes (in batches) from production environment are replicated to multiple subscribers through one distributor server.This adds to latency sometimes.

Proof of concept:

To reduce latency, we started thinking of replicating the execution of Stored Proc that deletes the rows in batches .This would not only give respite to log reader agent (which is one for every database ,irrespective of number of publications for that database ),But to multiple distributor agents as well that rely on msrepl_commands and msrepl_transactions tables in the distribution database .We will also save a lot of network bandwidth .As a result we will almost SKIP using distribution database for archiving because now we are just replicating the execution of Stored proc and not the actual deletes.

Issue that we thought we might face:
Things were not so easy for us (or might be for you as well as you read through) as I am typing here J .We already had all the required tables added as articles in the respective publications .So we had 2 doubts:

1)If we add the stored procedures in the current publication then we thought Replication will try to delete the data twice. For example Lets say there are 2 articles in the publication .The first one is a table (say REPL_TAB) and the second one is an SP (say REPL_SP) .REPL_SP deletes x rows from REPL_TAB.We feared that if we execute REPL_SP,it might affect the Subscriber table twice i.e. deletes 2 times .One: when the SP deletes the rows and two: since the rows are being deleted REPL_TAB should also replicate the same .So we thought this might not work .

2)We then thought of creating another publication with this SP added as an article but had same reservations. We cannot remove the previous articles because we not only delete but also insert and update.

But we think logically, Log Reader agent should pick up the command from the T-Log and should be smart enough to replicate it only once .I mean if I run EXEC XYZ which deletes 10 rows in a table ABC ,then it should only replicate EXEC XYZ and not the delete command because I am not firing the individual delete commands .
I first tested this in SQL Server 2005 SP3 CU2 and got it working .Let me show you what I mean:

Once this is done you need to generate the snapshot to sync up the subscriber (If your subscriber already has the data, then please do not initialize the subscriber.


Let’s execute the stored procedure now and see if the data is replicated twice:


it worked like a charm and the logReader agent did not replicate the transaction twice .Our 1st assumption was proved wrong and we were happy .Then we were asked to test the same by creating 2 new publications but keeping the subscriber database same. One with the table as an article and other with the SP as an article .This was asked (and it was a good question by the way )because many publications had same articles and different subscriptions .

We did that setup (skipping the script part):


We tested it again with fingers crossed:

Execution of SP :


Deleting top 10 rows :


This worked well too .With this test we were satisfied that Replication will not try to replicate twice to the subscriber database.You can query msrepl_transactions and ms_repl_commands as well to make sure that replication is behaving correctly .

Though this looks simple but many a time we are asked some questions to which we are unable to answer with 100% surity .This was one such example.

Hope you have found this post helpful .



Abhay Chaudhary

Like us on FaceBook Follow us on Twitter

Follow me on FaceBook | Join the fastest growing SQL Server group on FaceBook