Avatar of Charles Baldo
Charles Baldo
Flag for United States of America

asked on 

Need to merge 3 large tables into one Table in SQL server

Hello

I need to merge three tables into one.  They all have the exact same structure.

Table_A_original
Table_A_Archive_1
Table_A_Archive_2

Then a target table new_TABLE_all

They were supposed to be archived over two new tables and when it happened the records in Table_A_original that were Archived were suppose to be removed.  Some/many were not removed.   I need to rebuild into one table now. the document_id column is the primary key and can be searched. The tables have over 100,000 million records. So I want to be as efficient as possible.

Something like this:???

insert into new_TABLE_all
(
    document_id,
    doc_filename
)
SELECT
    document_id,
    doc_filename
FROM
     Table_A_original where document_id not in  (select document_id from  new_TABLE_all) --- (There must be more efficient way)

then repeat for Archive1 and Archive2

Note:s    document_id is an identity id and is primary key
                Database is old SQL 2005, job is migrating out.

Thank You
Microsoft SQL Server 2005Microsoft SQL Server

Avatar of undefined
Last Comment
Charles Baldo

8/22/2022 - Mon