简体   繁体   中英

SQL code to copy data from 1 database table to another

I have messed up the data. I have been ammended ids and values to the point where i cannot remember what the originals were. (testing a few things).

I have a table called query_string_interpretation and its part of the DB called, test1_db

I have a backup database which i have restored called, test2_db

How can I restore its contents from one database table to another?

Without more information on your part...generally you can use INSERT INTO/SELECT FROM to move data between tables.

The syntax is roughly:

INSERT INTO
test1_db..query_string_interpretation ( <your columns here>)
select <your columns here>
FROM test2_db..query_string_interpretation

You can use a SQL wizard to do this for you. In Management Studio, right click on either database, select Tasks then Import Data... or Export Data...

At first, you need to be sure that you have all your data in source table, or data is placed in both tables - source and destination. In first case you need to truncate the destination table:

TRUNCATE TABLE test1_db..query_string_interpretation

Second, you need to be sure that you will insert the right values into IDENTITY fields, if these fields exists. Use SET INDENITY_INSERT ON statement. Third, you need to insert the values:

insert into test1_db..query_string_interpretation
select * from test2_db..query_string_interpretation

And don't forget to switch the INDENITY_INSERT to OFF, you you switched it to ON in the second step.

try this:

delete from test1_db..query_string_interpretation

insert into test1_db..query_string_interpretation
select * from test2_db..query_string_interpretation

if you have an identity field you may have to write the name of the columns (except for the identity one). if you need to keep the IDs, take a look at the SET IDENTITY_INSERT ON statement here

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