?
Solved

Replication Latency Script - SQL 2005

Posted on 2010-08-22
3
Medium Priority
?
527 Views
Last Modified: 2012-08-14
Hi Experts,
I am trying to come up with a simple script (MS SQL 2005) to check the latency of my replication.
The case:
There is a publication database called PubDB and a replica of PubDB called ReplDB.
I need to query a table in PubDB  called TABLE1,  column LastUpdate , then do the same on the ReplDB and compare the two values (the two resulting dates).
If the difference between PubDb and ReplDB is more than 90 min an email alert should send an email.
Please feel free to build upon the provided code  snippets or provide even a better, more elegant solution.

Thanks,
RICUser


--This runs from PubDB

USE PubDB

select top 1 lastupdate

from TABLE1 (NOLOCK)

order by lastupdate desc 

 

/*
The next does the same in the replicated ReplDB via Linked Server.Wouldn't it be it be  better here to use OPENQUERY, so I can use the (NOLOCK) hint ?
*/

select top 1 lastupdate

from [REM_SERVER].[ReplDB].dbo.TABLE1

order by lastupdate desc 

--now the two results will look like that (just an example)

--PubDB
2010-08-21 03:30:000

--ReplDB
2010-08-21 01:30:000

--Now the comparison should follow,  and if  PubDB minus --ReplDB >90 min. an email should notify DBAONCALL@ACME.com

Open in new window

0
Comment
Question by:RICuser
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 42

Accepted Solution

by:
pcelba earned 2000 total points
ID: 33494865
If I understand it well, you just need to subtract two datetime values and it is easy.

The mail sending SP is described at many places, e.g. http://www.sqlteam.com/article/sending-smtp-mail-using-a-stored-procedure  or  http://classicasp.aspfaq.com/email/how-do-i-send-e-mail-from-sql-server.html

--This runs from PubDB

DECLARE @PubTime datetime, @ReplTime datetime

USE PubDB

SET @PubTime = (select top 1 lastupdate from TABLE1 (NOLOCK) order by lastupdate desc) 


/*
The next does the same in the replicated ReplDB via Linked Server.Wouldn't it be it be  better here to use OPENQUERY, so I can use the (NOLOCK) hint ?
*/

SET @ReplTime = 
 (select top 1 lastupdate from [REM_SERVER].[ReplDB].dbo.TABLE1 order by lastupdate desc)


--now the two results will look like that (just an example)

--PubDB
2010-08-21 03:30:000

--ReplDB
2010-08-21 01:30:000

--Now the comparison should follow,  and if  PubDB minus --ReplDB >90 min. an email should notify DBAONCALL@ACME.com

IF DATEDIFF(minute, @ReplTime, @PubTime) > 90
  EXEC sp_SMTPMail ....

Open in new window

0
 
LVL 3

Expert Comment

by:mnachu
ID: 33496629
We do a similar stuff only thing is instead of the linked server we used SSIS to do it:

1. Connect  to Server A
2. Runs the queries and have it in variables.
3. Connect to Server B
4. Compare two values
5. Send mail.

Another reason we did this is, we had multiple replicated servers were we wanted to check/compare. All we had to do was change the configuration when running the SSIS using a SQL job. Also we didn't have to use linked servers which our DBA doesn't encourage having on either server.

Regards,
Nachi
0
 
LVL 2

Expert Comment

by:marat-oz
ID: 33506808
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this article, we’ll look at how to deploy ProxySQL.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question