Keeping track of rows already selected
Hi.  I'm somewhat a T-SQL noob.  The answer to my question may be obvious, but I just want to see if I have any options.  I need to scan a table for rows where a column contains a certain value, and then send an email to notify users of the resultset.  I'll run that scan say hourly. The table I'm scanning is in a vendor-owned database that I can't modify.  So I guess I need to keep track of previously selected rows so I don't keep notifying users of the same resultset, and store those in a persistent table in a separate utility database.  Right so far?  Thanks for helping me get started.
March 20th, 2015 4:58pm

Do these records have a timestamp on them?
Free Windows Admin Tool Kit Click here and download it now
March 20th, 2015 5:05pm

The answer is, "it depends"... The most reliable way is record the latest record scanned every time you ran a scan. If you can't make any modifications (aka add a table to hold the scan logs) you may be forced to rely on timing...

If you run it hourly, you only need to scan rows that were inserted in the last hour...

You haven't provided enough information for us to give you a "best option".

March 20th, 2015 5:09pm

You can flag the column with some value while selecting a result set and notify the users and update the flag with Processed status. 

Do the same steps for successive iterations.

--Prashanth

Free Windows Admin Tool Kit Click here and download it now
March 20th, 2015 5:09pm

You can flag the column with some value while selecting a result set and notify the users and update the flag with Processed status. 

Do the same steps for successive iterations.

--Prashanth

OP says he cannot modify the table.
March 20th, 2015 5:10pm

Yes, there is a created_on datetime column and a last_updated_on datetime column.
Free Windows Admin Tool Kit Click here and download it now
March 20th, 2015 5:12pm

Ok, so what you'll want to do then is just select rows that have been edited in the last hour:

SELECT *
  FROM myTable
 WHERE last_updated_on >= DATEADD(HOUR,-1,CURRENT_TIMESTAMP)

And you'll only get the rows that have been updated in the last hour :)

March 20th, 2015 5:14pm

I agree with your statement "The most reliable way", but I think for my purposes, Patrick Hurst's solution is good enough to get me started.  If it proves unreliable, I'll come back here and ask further guidance.  Thanks.
Free Windows Admin Tool Kit Click here and download it now
March 20th, 2015 6:20pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics