SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Smart data archiving stored procedure

By Christopher House,

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.

Total article views: 604 | Views in the last 30 days: 6
Related Articles

Archiving Data

Archive Data From Production DB into Archive DB


Archive Data

Archive Data From Production DB into Archive DB


Archive and Restore procedureProblem

Archive and Restore procedure Problem


Data Archiving

Data Archiving


data archiving

how to do data archiving