Copy the records in a table to another database table in SQLServer

Source: Internet
Author: User
Welcome to the Windows community forum and interact with 3 million technical staff. In SQLServer, copy the records in a table to the tables in another database. The existing databases a and B have table1 in database, table 2 in database B needs to copy the records in table 1 to table 2. Different methods are used in different situations: 1.tab

Welcome to the Windows community forum and interact with 3 million technical staff> go to SQLServer and copy the records in a table to the existing database a and database B in another database table. Database a contains table 1, database B has a table table2. now we need to copy the records in table 1 to table 2. Different methods are used in different situations: 1. tab

Welcome to the Windows community forum and interact with 3 million technicians>

Copy the records in a table to another database table in SQLServer

Existing databases a and B. Database a contains table 1, and database B contains table 2.

Now we need to copy the records in table 1 to table 2. Different methods are used in different situations:

1. Table 1 and Table 2 have the same structure.

Use B

Insert into table2 select * from a. dbo. table1

2. The table structures of Table 1 and Table 2 are different.

Use B

Insert into table2 (c1, c2) select c1, c2 from a. dbo. table1

Coolxiaoyi: c1, c2 is the column to be copied.

3. If Table 2 has not been created, you can directly copy the table structure and records of Table 1 to database B.

Use B

Select * into table2 from a. dbo. table1

Coolxiaoyi.

4. Check whether the auto-increment field exists in table 2.

If an auto-increment field exists, an error is returned when the preceding SQL statement is used: only when the column list is used and IDENTITY_INSERT is ON can an explicit value be specified for the ID column in table 'table2.

Coolxiaoyi: you can modify table 2 first, remove the auto-increment identifier, and copy the record before modifying it.

You can also use the SQL statement in step 2 to copy other fields instead of auto-increment fields.

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.