Replicating data across libraries in SQL Server

Source: Internet
Author: User
Tags one table

An action method in SQL Server that copies the records from one table to the table in another database.

Scene

Existing database A and database B, database A has table table1, database B has table table2.
Now to copy the records from the table Table1 to the table2, different ways are used.

Here are the syntax and examples

1. table1 and TABLE2 table structures are the same

 Use b inch  into *  from A.dbo.table1

2. table1 and TABLE2 table structures are not the same

 Use b inch  into  from A.dbo.table1

Note: C1,C2 is the column that needs to be copied.
3. If you have not created a table table2, you can copy the table structure and records of Table1 directly into database b

use * to from A.dbo.table1 

Note: One problem with this is that sometimes the primary foreign key relationship cannot replicate the past (for unknown reasons) and you need to set the primary foreign key yourself after executing the SQL statement.
4. Note whether there are self-growing fields in Table2
If you have a self-growing field, you will get an error when using the above SQL statement: You IDENTITY_INSERT can specify an explicit value for the Identity column in table ' table2 ' only if the column list is used and on.
Note: You can modify the Table2 table first, remove the self-growth logo, copy the record and then modify it back. Or use 2 the SQL statement in, do not copy the self-growing field, copy the other fields.

Replicating data across libraries in SQL Server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.