Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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: 557 | Views in the last 30 days: 2
 
Related Articles
FORUM

Archiving Data

Archive Data From Production DB into Archive DB

FORUM

Archive Data

Archive Data From Production DB into Archive DB

FORUM

Archive and Restore procedureProblem

Archive and Restore procedure Problem

FORUM

Data Archiving

Data Archiving

FORUM

data archiving

how to do data archiving

 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones