?
Solved

Delete oldest records after maximum row count is reached in a SQL Server table?

Posted on 2012-03-20
8
Medium Priority
?
2,770 Views
Last Modified: 2012-03-30
(Need an answer for either SQL Server 2005 or 2008.)

I have a SQL Server table whose size I want to limit.  I want to be able to specify that only, say, 50,000 records are allowed.  I do not want to prevent the addition of new records.  Rather, I want to drop the oldest records as new records are added so that I retain only the most recent 50,000.

Each record has an auto-incremented field with an increment of 1.  It is possible I could be inserting hundreds of records at once, several times a day.

I am guessing this could be handled by some type of insert trigger.  I would also consider some kind of scheduled process that would run each day (or week) and simply delete all the unwanted records at that time to keep reducing the record count to 50,000.

Thanks.
0
Comment
Question by:AutomateMyOffice
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 58

Accepted Solution

by:
amit_g earned 2000 total points
ID: 37743547
You could use the script below but this should certainly be not run as a trigger. Why would you want to slow down every single insert. Run it as a job daily or if the row count is very important to keep low, every hr or so...

delete TableName
from TableName T1
inner join (
	select ROW_NUMBER() OVER (order by AutoID desc) RowNum, AutoID  from TableName
) T2 on T1.AutoID = T2.AutoID
where T2.RowNum > 50000

Open in new window

0
 

Author Comment

by:AutomateMyOffice
ID: 37743643
I definitely want to accomplish this in the most efficient way.  What is the best approach for scheduling such a thing?  Would I create the script as a stored procedure?  Please be as detailed as possible.  Thanks very much!
0
 
LVL 58

Expert Comment

by:amit_g
ID: 37743713
Yes make it a stored procedure. First test it in a test environment and then make it a SQL job that runs at every x hrs/days.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:AutomateMyOffice
ID: 37743731
I have tested it and it works perfectly.  I am sorry but I still need help in how to make and schedule a "SQL job."  Thanks.
0
 

Author Comment

by:AutomateMyOffice
ID: 37743799
Oh-oh.  I did a little research and it looks like a scheduled job demands SQL Server AGENT, which is not available in my SQL Server EXPRESS environment.  Is there an alternative approach?
0
 
LVL 58

Expert Comment

by:amit_g
ID: 37743801
0
 
LVL 58

Expert Comment

by:amit_g
ID: 37743871
You can use the SqlCmd and schedule the windows job. The command line would be

sqlcmd -S ServerName\SQLExpress -d DatabaseName -Q "exec YourSPName"

Open in new window


Using the sqlcmd Utility (SQL Server Express)

The instructions to created a windows task would depend on the version of the windows. Here are instructions for Windows 7
0
 

Author Comment

by:AutomateMyOffice
ID: 37788383
Fantastic.  This worked perfectly.  I am running your procedure using sqlcmd in Task Scheduler under Windows 7.  Also, I added a parameter so that the desired number of rows could be specified without having to modify the stored procedure:
 
"exec TruncateTable @MaxRows=$(myparam)" /v myparam=1000

Thanks!
0

Featured Post

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question