There is a big table (5-6 million records). I have to move 90% of old records to other database(table). Solution?


Couple of additions to Rolando's suggestion.

If you're clearing out the old table, as well as populating new you could use the OUTPUT clause. Be mindful of the potential for log growth, consider a loop/batch approach if this may be a problem.

    , DELETED.col2
    , DELETED.col3

BCP is a handy alternative to be aware of. Note this is using SQLCMD syntax.

:setvar SourceServer OldServer
:setvar SourceDatabase OldDatabase
:setvar DestinationServer NewServer
:setvar DestinationDatabase NewDatabase
:setvar BCPFilePath "C:\"

!!bcp "$(SourceDatabase).dbo.MyTable" FORMAT nul -S "$(SourceServer)" -T -n -q -f "$(BCPFilePath)MyTable.fmt"
!!bcp "SELECT * FROM $(SourceDatabase).dbo.MyTable WHERE col1=x AND col2=y" queryout "$(BCPFilePath)MyTable.dat" -S "$(SourceServer)" -T -q -f "$(BCPFilePath)MyTable.fmt" -> "$(BCPFilePath)MyTable.txt"
!!bcp "$(DestinationDatabase).dbo.MyTable" in $(BCPFilePath)MyTable.dat -S $(DestinationServer) -T -E -q -b 2500 -h "TABLOCK" -f $(BCPFilePath)MyTable.fmt

For SQL Server the real problem is avoiding the logging of the deleted rows.

My proposal is
Set up a second database in simple recovery mode. Copy the the whole table with

Select * into SEM..Copy from Original

Truncate Original

Insert into Original Select * from SEM..Copy

If target server is SQL Server of the same version you can move the data by backup and restore.

In other cases take some bulkcopy option.


Although it is time to learn about partitioned tables, but that seems to be an option only for Enterprise Edition.


If the TableA is really big, you can use bulk-operations and bcp utility

  1. export the data from TableA using bcp utility into file
  2. Create the TableB
  3. Import the data using bulk insert or openrowset (bulk...)
  4. delete old data from TableA
  5. rename the tables, if needed

see the About Bulk Import and Bulk Export Operations article.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.