简体   繁体   中英

Move data from one table to another in the same Database

I realize this question has been asked before, but nothing I've read really answers my question.

I have a table with millions of rows of data that is used in multiple queries a day. I want to move the majority of the data to another table with the same schema. The second table will be an "archive" table.

I would like a list of options to archive data, so I can present them to my boss. So far I'm considering an insert into select statement, SQLBulkCopy in a C# console application, and I'm starting to dig in to SSIS to see what it can do. I plan on doing this over a weekend or multiple weekends.

  • The table has an ID as the primary key
  • The table also has a few foreign key constraints

Thanks for any help.

I assume that this is for SQL Server. In that case, partitioned tables might be an additional option. Otherwise I'd always go for a INSERT ... SELECT run by a job in SQL Server, or - if you can't run it directly in SQL Server - create a stored procedure and run it through a little C# tool that you schedule.

Try execute something like

CREATE TABLE mynewtable as select * from myoldtable where any_filter..;

You could create new table with data copy with one instruction on most database engines.

Use this, in case of SQL Server 2008

Select * into new_table  from  old_table 

In the event that you have a set data archive interval, you may be able to leverage the partition-to-archive solution described in the following article.

http://blogs.msdn.com/b/felixmar/archive/2011/02/14/partitioning-amp-archiving-tables-in-sql-server-part-1-the-basics.aspx

Our team has leveraged a similar partition / archive solution in the past with good success.

Regards,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM