I am trying to move records from a table in one database to a table
with identical structure in another database for archiving purposes.
My tables are very large with lots of fields, so for a simplified
example, suppose I have a database called People which has a table
Personal_Info with the fields First_Name, Last_Name, Birth_Year
and another database called Old_People with a matching Personal_Info table.
I want to be able to move entries from the People table into the
Old_People table based on how old they are. Is there any
straightforward way to do this? I am sending the db queries from C#,
so I could query the People db to get the entries I want to move,
generate the inserts into the Old_People db, then do the deletes in
the People db, but I'm hoping for a better (cleaner / faster)
solution.
I read about using an IN statement to specify a different database,
but when I tried the statement below it gave me a syntax error near
IN.
SELECT * INTO Personal_Info IN Old_People FROM Personal_Info WHERE
Birth_Year < 1900
To clarify what I want to have happen, here is an example:
Starting State:
-------------------------------------------
DB People
TABLE Personal_Info
FIRST_NAME LAST_NAME BIRTH_YEAR
John Doe 1873
Suzy Q 1955
Bobby Smith 1984
Alice Black 1884
DB Old_People
TABLE Personal_Info
FIRST_NAME LAST_NAME BIRTH_YEAR
Peter Piper 1564
Jane Doe 1689
-------------------------------------------
Should become:
-------------------------------------------
DB People
TABLE Personal_Info
FIRST_NAME LAST_NAME BIRTH_YEAR
Suzy Q 1955
Bobby Smith 1984
DB Old_People
TABLE Personal_Info
FIRST_NAME LAST_NAME BIRTH_YEAR
Peter Piper 1564
Jane Doe 1689
John Doe 1873
Alice Black 1884
-------------------------------------------
I cannot make any major changes to the structure of the People
database or the tables within it, but I have complete control over the
Old_People db.
I'm looking for either:
-the query statement for moving records between two databases (not
using "IN" unless I'm using it incorrectly and you can get it to work
for me!)
OR
-if it can't be done with a query, then tell me! and provide
suggestions on the best way to make it happen through code. For
example, can I use my resultset directly for an insert into another
db? Or do I really have to generate and run all of those insert
statements?
I will give extra tip if you also provide a way to create the
Old_People db and Personal_Info table within on the fly if it doesn't
already exist.
Thank you in advance! |