Suppose I have a table in my database like
Registrants
=======================================================================
id | name | email | registration_date
=======================================================================
1 | "Sam" | "sammypie49@gmail.com" | "2016-03-26T14:25:10"
-----------------------------------------------------------------------
2 | "Bob" | "bthebuilder@msn.com" | "2015-12-01T10:09:30"
-----------------------------------------------------------------------
. | . | . | .
-----------------------------------------------------------------------
. | . | . | .
-----------------------------------------------------------------------
. | . | . | .
-----------------------------------------------------------------------
. | . | . | .
and I want every registrant to automatically be deleted, say, 100 days after registering. What is a proper way to do this, and what is the best way?
The shoddy way I was planning on doing it was going to be to create a sproc
CREATE PROCEDURE FlushOldRegistrants
AS
BEGIN
DELETE FROM Registrants WHERE DATEADD(day,100,registration_date) < GETDATE()
END
and in my server-side code invoke that sproc every once in a while like
Script.SetInterval(delegate {
using (var conn = new SqlConnection(connectionString))
using (var command = new SqlCommand("FlushOldRegistrants", conn) {
CommandType = CommandType.StoredProcedure }) {
conn.Open();
command.ExecuteNonQuery();
conn.Close();
}
}, 60000); // flush old registrants every hour
But, can I do that all at the database level? Is it possible to create a T-SQL TRIGGER that does this? Or is there an even better way?