Search code examples
sqlsql-serverrepeat

How to fetch record using SQL in five minutes window


I am writing a SQL query which will run once a day it will execute every 5 minutes and load data from 24 hours ago in that 5 minute window. The important thing is it should not have records which is fetched in previous 5 minutes window.

I have started writing the query:

SELECT
    Qn.Name, Qn.Email, Qn.Price 
FROM
    Quotation Qn
INNER JOIN 
    Product P ON P.ProductId = Qn.ProductId
WHERE
    Qn.ProductId = 10 
    AND Qn.Date = DATEADD(D, -1, CONVERT(DATE, GETDATE()))
    AND Qn.Bname= 'CaraVan'

Thanks in advance for the help


Solution

  • You could create a table to store the fetched rows (or their primary key set if any) and hence to exclude the rows already fetched (contained in this new table). I would delete the rows of the new table older that 24 hours to make the process more efficient in the long run. You could to this in the same scheduled task you are using. Please let me know if you need some example code.