Sql Server replication table

 

SELECT INTO and INSERT INTO SELECT statement to copy two kinds of table 

 1. INSERT INTO the SELECT statement

      Statement in the form of: Insert INTO Table2 (field1, Field2, ...) value1 the SELECT, value2, ... from Table1

      Table2 required target table must exist, since the target Table2 table already exists, we insert the source table Table1 except field, but also can be inserted constants. Examples are as follows:

 

 1 --1.创建测试表
 2 
 3     create TABLE Table1
 4 
 5     (
 6 
 7         a varchar(10),
 8 
 9         b varchar(10),
10 
11         c varchar(10),
12 
13         CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
14 
15         (
16 
17             a ASC
18 
19         )
20 
21     ) ON [PRIMARY]
22 
23 
24 
25     Create TABLE Table2
 26 is  
27      (
 28  
29          A VARCHAR ( 10 ),
 30  
31 is          (C VARCHAR 10 ),
 32  
33 is          D int ,
 34 is  
35          CONSTRAINT [PK_Table2] a PRIMARY KEY the CLUSTERED
 36  
37 [          (
 38 is  
39              A the ASC
 40  
41 is          )
 42 is  
43 is      ) the ON [a PRIMARY]
 44 is  
45      the GO
 46 is  
47      - 2 . Create test data
 48  
49     Insert into Table1 values('','asds','90')
50 
51     Insert into Table1 values('','asds','100')
52 
53     Insert into Table1 values('','asds','80')
54 
55     Insert into Table1 values('','ASDS ' , null )
 56 is  
57 is      the GO
 58  
59      SELECT * from Table2
 60  
61 is  
62 is  
63 is      - . 3 .insert the INTO the SELECT statement replication table data
 64  
65      the Insert INTO Table2 (A, C, D) SELECT A, C, . 5  from the Table1
 66  
67      the GO
 68  
69  
70  
71 is      - . 4 . updated result is displayed
 72  
73 is      SELECT * from Table2
 74  
75      the GO
76  
77      - . 5 . Remove the test table
 78  
79      drop the Table1 TABLE
 80  
81      drop TABLE Table2
View Code

2. 2. the SELECT INTO statement to the FROM

      Statement in the form of: the SELECT vale1, value2 INTO Table1 from Table2

      Table2 required destination table does not exist, because the table is automatically created upon insertion of Table2, and replication fields specified in Table1 data in Table2 . Examples are as follows:

 

- . 1 . Create Test Table 

    Create the Table1 TABLE 

    ( 

        A VARCHAR ( 10 ), 

        B VARCHAR ( 10 ), 

        C VARCHAR ( 10 ), 

        CONSTRAINT [PK_Table1] the CLUSTERED a PRIMARY KEY 

        ( 

            A the ASC 

        ) 

    ) the ON [a PRIMARY] 

    the GO



     - 2 . create test data 

    Insert INTO Table1 values ( ' Zhao ' , ' ASDS ' , ' 90 ' ) 

    Insert INTO Table1 values ( ' money ' ,' ASDS ' , ' 100 ' ) 

    Insert INTO Table1 values ( ' Sun ' , ' ASDS ' , ' 80 ' ) 

    Insert INTO Table1 values ( ' Li ' , ' ASDS ' , null ) 

    GO



     - 3 .Select INTO statement creates the FROM and copying the data table Table2 

    SELECT a, C the INTO Table2 from the Table1 

    the GO



     - . 4 . the results show updated 

    SELECT * from Table2

    GO

     - 5 . Delete table test 

    drop TABLE Table1 

    drop TABLE Table2
View Code

 

 

 

 

 

    

Reproduced in: https: //www.cnblogs.com/tutuyforever/p/3454060.html

Guess you like

Origin blog.csdn.net/weixin_34327761/article/details/94682037