Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Auto delete some records in the table

Author  Topic 

Hippi
Yak Posting Veteran

63 Posts

Posted - 2005-01-23 : 13:46:40
Hi,

I have a table with some colums and one of them is Date_created one. I want to keep the records for 10 days since they're inserted into the table and How can I do it automatically?

Thanks a bunch.

jhermiz

3564 Posts

Posted - 2005-01-23 : 14:08:55
quote:
Originally posted by Hippi

Hi,

I have a table with some colums and one of them is Date_created one. I want to keep the records for 10 days since they're inserted into the table and How can I do it automatically?

Thanks a bunch.



If you want you can create a job that deletes these records. In the job step just have it call a stored procedure that does a DELETE on all records that are past 10 days. Lookup DATE functions in BOL (GetDate() to get todays date).

BTW, be careful with deletes, someone usually may ask I accidentally deleted a record the other day can you magically bring it back. You may want a bit field in your table: "DeletedRecord" and set it to true. In your presentation layer you may pull back all records where DeletedRecord = False.

Jon



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-01-23 : 14:21:03
If you don't want to set up a scheduled job then you could delete whenever you insert a new one.

DELETE D
FROM MyTable D
WHERE Date_created < DATEADD(Day, -10, GetDate())

INSERT INTO MyTable
...

This will, of course, keep things older than 10 days if nothing new gets inserted for a while ...

NOTE: DATEADD(Day, -10, GetDate()) will delete anything 10 days older than CURRENT TIME, rather than anything older than "10 Whole Days ago"

Kristen
Go to Top of Page
   

- Advertisement -