Click here to Skip to main content
15,901,982 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have database with many records.when i am inserting a record it will take the current time after some time like 10 mins the record should be deleted.how we can do any help frds

What I have tried:

i have database with many recordswhen i am inserting a record it will take the current time after some time like 10 mins the record should be deleted.how we can do any help frds
Posted
Updated 4-Jan-17 8:29am
Comments
[no name] 4-Jan-17 13:46pm    
Create a SQL job that will run periodically and delete the records that have expired.

1 solution

Creating a job is one possibility, for examples see Schedule a Job[^] and Create a Transact-SQL Job Step[^] . However, keep in mind that this approach won't work with Express Editions since the SQL Agent is disabled for them.

But to give an alternative, are you sure you want to permanently delete the records? A much more common approach is to control the validity of a record. In other words if it's older than the 10 minutes, don't fetch it but let it still reside in the database. An easy way to achieve this is that you have a datetime field which has current datetime as default in a validity field and in your fetch you eliminate rows that are older than the desired amount of time.

Consider the following:
SQL
CREATE TABLE ValidityTest (
   col1 varchar(100),
   Created datetime DEFAULT GETDATE()
);

INSERT INTO ValidityTest (col1) VALUES ('Some row');

SELECT *
FROM ValidityTest 
WHERE Created >= DATEADD(minute, -10, GETDATE())

if you want, you can make the query easier by creating a view to query from, for example
SQL
CREATE VIEW ValidRows AS
SELECT *
FROM ValidityTest 
WHERE Created >= DATEADD(minute, -10, GETDATE());

Now you can easily query valid data
SQL
SELECT * FROM ValidRows;
If you like, you can run a cleanup delete every once in a while to remove records older than desiread amount of time (months for example) but as long as the space or speed isn't an issue, this isn't necessary.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900