Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Replication Latency Script - SQL 2005

Posted on 2010-08-22
3
Medium Priority
?
529 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 43

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

598 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