?
Solved

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

Posted on 2012-03-20
8
Medium Priority
?
2,862 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article I will describe the Detach & Attach 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 video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

649 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