Solved

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

Posted on 2012-03-20
8
2,426 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
  • 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
 

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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to delete duplicate records within a SQL Server 2008 table using a SQL statement? 5 30
Authentication error 1 38
Storage Spaces 3 39
tempdb latch contention 12 48
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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now