Solved

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

Posted on 2012-03-20
8
2,685 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 500 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

707 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