Solved

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

Posted on 2012-03-20
8
2,368 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

20 Experts available now in Live!

Get 1:1 Help Now