Click to See Complete Forum and Search --> : delete record after a given time


vertige
March 20th, 2003, 08:15 PM
Sorry for the newbe question but I can't seem to find the answer in any of the 8 t-sql books I bought.

I'm running ms sql 2000. I have a table called cart. Some of the items in the cart need to expire after a given amount of time. One of the fields is a datatime field called added. What i need to do is delete the record after 10 minutes.

I'm tring to user the job wizard to add a job that will run every 30 seconds. If the job finds an item in the cart that is older than 10 minutes it will delete it.

My imaginary query:

"Delete from cart where the difference between added and NOW is greater than 10 minutes"

I can do this programatically on the webserver end but it seems more logical and robust to do it using the sql server.

Any help would be greatly apreciated.

antares686
March 21st, 2003, 06:18 AM
The query would look something like this.

DELETE cart where added < DATEADD(mi,-10,GETDATE())

However, if this system has a high number of transactions and a good number of indexes you may find it better to set your apps queries up to ignore items over 10 minutes and do a mass delete at an offpeak time instead.