Question : Replication Latency Script - SQL 2005

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

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
--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 [email protected]

Answer : Replication Latency Script - SQL 2005

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
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
--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 [email protected]

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