MS SQL 2008 Bulk Copy Records between two tables in same database

Posted on 2012-08-17
Last Modified: 2012-08-23
I have a Current table and an Archive table.  I want to move records from the Current (has an identity field) table to the Archive table for all records that are greater than 30 days old.  I know I can do this with SQL scripting which will probably work ok for all archiving after the initial archiving routine.  To start I have 300+ million records in the Current table and will be moving all but about 25 million of those records to the Archive table.  Is there any fast way to do this?

Question by:j_heck
    LVL 68

    Assisted Solution

    Copy all rows to the archive table and then delete the 25M you don't need.

    For the current table, pull out the 25M you do need, truncate the table, then put the good 25M back.
    LVL 26

    Accepted Solution

    1. Rename your existing table to table_archive.
    2. Create back the table with the same structure as the table_archive.
    3. Copy the 25M rows from table_archive to table
    4. Make sure you have a timestamp column with a default of GETDATE() that is indexed and then you can remove easily the ones that are more that 30days old.

    Author Closing Comment

    Both are great ways to complete the task.  Thanks.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
    If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    779 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

    14 Experts available now in Live!

    Get 1:1 Help Now