How to expire a particular row in SQL Server once a date is reached?

vivek picture vivek · Aug 23, 2012 · Viewed 10.6k times · Source

It may be very simple scenario for experienced developers, but as a beginner I have been hanging around this for a long time. I am creating a website in asp.net where a user can purchase a packages. These packages have some expiry date associated with them (e.g. 2 months after purchase, 6 months, etc.).

The problem is how to expire each package exactly at the same date and time that i have calculated on the basis of purchase date. How to do this thing?

I am using SQL Server express edition 2008 and asp.net 4.0 .

Answer

Curt picture Curt · Aug 23, 2012

Rather than deleting the records, you could put a WHERE clause in place which would exclude the records based on their expiry date:

SELECT PackageName
FROM Package
WHERE ExpiryDate>GETDATE()

However if you still wish to remove them from the database, you could put a SQL Job in place which is ran every day/hour etc which would delete records which have expired:

DELETE FROM Package
WHERE ExpiryDate<GETDATE()