0

I am trying to write a query to extract records updated in the last 2 hours in SQL Server 2008.

Could anyone help me write this?

Bridge
  • 29,818
  • 9
  • 60
  • 82

2 Answers2

1
select * from table where table.date1>=dateadd(hh,-2,getdate())

dateadd() function lets you subtract hours from getdate() letting you choose records updated past 2 hours

vhadalgi
  • 7,027
  • 6
  • 38
  • 67
1

First, you have to design the table so you have a field where the time of the last change will be stored

Then, whenever you update a row, update the value in the 'last update' field. After that, you can use a script such as suggested by Vijaykumar

The downside of this method is that when a single record was changed more than once in the specified time period, you will be notified only about the time of the last update.

Another solution for tracking the updates is to read the database online transaction log file, but you'll need a third party tool for that

Milena Petrovic
  • 2,651
  • 21
  • 18