SQL Server Central is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

Smart data archiving stored procedure

By Christopher House, 2007/09/05

Total article views: 237 | Views in the last 30 days: 5

This stored procedure uses a series of input parametes to generate, and optionally execute a series of SQL commands to move production data to an archive table.  The procedure assumes that the production and archive tables will have the same structure. 

The procedure uses the following input parameters:

@SourceTable:  This is the name of the table containing the production data to be archived.

@DestinationTable: This is the name of the archive table where data will be moved to.

@CutOffInterval:  This is the number of days/weeks/years to keep in the production table.

@CutOffType:  This defines what the value of @CutOffInterval represents, days/weeks/years.  Must be a valid datepart argument for the DATEADD function.

@DateColumnName:  This is the name of a datetime column in the source table that is used to determine which records to archive.

@PrintOnly:  Indicates whether to only print the generated SQL command (1) or to execute it as well (0).  Note that the default value is 1, print only.

This procedure has been tested on SQL 2000.  I'm guessing it would work on SQL 2005 but I don't have the environment to test it.  Also assumes that the source and destination tables are in the same database as the stored procedure.  See comments in the procedure and the script header for more information on what's going on.  Hope someone else finds this useful.

By Christopher House, 2007/09/05

Total article views: 237 | Views in the last 30 days: 5
Your response
 
 
Related tags
 
Like this? Try these...
Already registered?  

Free registration required

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Register

E-mail address:
Password:
Password (confirm):

  

Subscriptions

We ask you to register on the site and subscribe to our newsletters. Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

We ask that you give the newsletter a try for a week. Over 200,000 SQL Server Professionals a day find it entertaining and useful. If not, you are welcome to unsubscribe at anytime.

Steve Jones
Editor, SQLServerCentral.com