SQL Server Replication Table

Source: Internet
Author: User

Q: How to copy the structure of other tables into a specified table, such as the following example:

The following three tables are available:

Table A: Field 1, Field 2, Field 3

Table B: Field 1, Field 2, Field 3

Table C: Field 1, Field 2, Field 3

Now you want to copy the field of Table 1,b table field 2,c table field 3 to table 4. The data in the table is not required.

How should SQL statements be implemented?

A: In fact, here is a question to consider, the specified table is not an empty table? Therefore, we have the following two kinds of circumstances, respectively, said:

1,/*table4 does not exist when * *

Select A.col1, B.col2, c.col3
Into Table4
From TableA A, TableB B, TableC c
where 1 = 0

2, however, once the table 4 already exists, will be an error. If you want to add several fields based on table 4, and then copy all the fields to another table, the data for those fields does not need to be added to the table
SELECT * into table 5 from table 4 m
Left Join
(Select a. field 1,b. field 2,c. field 3 from Table A, table b b, table C c where 1<>1) n
On 1=1

DROP table 4

EXEC sp_rename ' table 5 ', ' Table 4 '

Transfer from http://blog.163.com/zhaoyanping_1125/blog/static/20132915320120645416135/

SQL Server Replication Table

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.