SQL Server:合并两个表中的数据

时间:2016-09-16 18:00:40

标签: sql-server tsql

我有两个表,如下所示,尝试使用merge指定结果。有可能吗?

表1

    Table1ID   Q1A    Q2A      Q3A    Active 
    ----------------------------------------
    1           2      Test     1       1
    2           3      Test2    1       1
    3           4      Test3    1       1
    4           5      Test4    1       1
    5           5      Test5    1       0

我发现了另一个条件并且遇到了问题,第5行也存在数据,在这种情况下处于非活动状态而不是以任何方式更新该行,我需要添加具有相同细节但是活动行的新行。有关如何更新脚本的任何建议。 table1存在标识列,因此数据不会重复。

在查看现有记录时,比较table2中的数据并更新表1中的数据。

表2

Table1ID   E1       E2          E3      
----------------------------------------
1           2      TestData1     1       
2           3      TestData2     1      
3           4      TestData3     1      
5           5      TestData5     1     
6           7      TestData6     0

结果:

Table1ID   Q1A    Q2A      Q3A    Active 
-----------------------------------------
1           2      Test     1       1 -- Rows with Id 1,2,3 already exists in table 1 dont do anything
2           3      Test2    1       1
3           4      Test3    1       1
4           5      Test4    1       0 -- Rows with Id 4 exists in table 1 but not in table 2 update it to inactive
5           5      Test5    1       0
5           5      Test5    1       1 -- Rows with Id 5,6 does not exist in table 1  so insert it
6           7      Test6    0       1

2 个答案:

答案 0 :(得分:2)

我能够使用以下脚本完全匹配结果。希望这可以帮助!如果您有任何疑问,请告诉我。

            /* 
                Script to create the tables and merge the data
            */
            -- drop table #Table1; 
            create table #Table1 ( 
                Table1ID INT, 
                Q1A INT, 
                Q2A varchar(10), 
                Q3A INT, 
                Active BIT 
            );  
            INSERT INTO #Table1 VALUES (1, 2, 'Test', 1, 1); 
            INSERT INTO #Table1 VALUES (2, 3, 'Test2', 1, 1); 
            INSERT INTO #Table1 VALUES (3, 4, 'Test3', 1, 1); 
            INSERT INTO #Table1 VALUES (4, 5, 'Test4', 1, 1); 

            -- drop table #Table2; 
            create table #Table2 ( 
                Table1ID INT, 
                E1 INT, 
                E2 varchar(10), 
                E3 INT
            );  
            INSERT INTO #Table2 VALUES (1, 2, 'TestData1', 1); 
            INSERT INTO #Table2 VALUES (2, 3, 'TestData2', 1); 
            INSERT INTO #Table2 VALUES (3, 4, 'TestData3', 1); 
            INSERT INTO #Table2 VALUES (5, 5, 'TestData5', 1); 
            INSERT INTO #Table2 VALUES (6, 7, 'TestData6', 0); 

            ;MERGE INTO #Table1 AS TGT
                USING #Table2 AS SRC 
                    ON TGT.Table1ID = SRC.Table1ID
            WHEN NOT MATCHED BY SOURCE THEN UPDATE 
                SET TGT.Active = CAST(0 AS BIT) 
            WHEN NOT MATCHED THEN INSERT (Table1ID, Q1A, Q2A, Q3A, Active) 
                VALUES (SRC.Table1ID, SRC.E1, REPLACE(SRC.E2, 'Data', ''), SRC.E3, CAST(1 AS BIT)); 

            select * 
            from #Table1;  

您也可以使用变量代替表格,如下所示:

DECLARE @Table1ID INT = 1, 
    @E1 INT = 2, 
    @E2 varchar(10) = 'TestData1', 
    @E3 INT = 1; 

;MERGE INTO #Table1 AS TGT
USING (VALUES(@Table1ID, @E1, @E2, @E3)) 
        AS SRC (Table1ID, E1, E2, E3) 
    ON TGT.Table1ID = SRC.Table1ID
WHEN NOT MATCHED BY SOURCE THEN UPDATE 
SET TGT.Active = CAST(0 AS BIT) 
WHEN NOT MATCHED THEN INSERT (Table1ID, Q1A, Q2A, Q3A, Active) 
VALUES (SRC.Table1ID, SRC.E1, REPLACE(SRC.E2, 'Data', ''), SRC.E3, CAST(1 AS BIT)); 

select * 
from #Table1; 

答案 1 :(得分:1)

请试试这个 -

merge @table1 t1
using @table2 t2 on t2.table1_id = t1.id
when not matched by source
then update set t1.active = 0
when not matched by target then
insert (id, q1a, q2a, q3a, active)
values (t2.table1_id, t2.e1, t2.e2, t2.e3, 1);

数据脚本 -

declare @table1 table (id int, q1a int, q2a varchar(10), q3a int, active bit)
declare @table2 table (table1_id int, e1 int, e2 varchar(10), e3 int)

insert into @table1
select 1,2,'test',1,1 union
select 2,3,'test2',1,1 union
select 3,4,'test3',1,1 union
select 4,5,'test4',1,1

insert into @table2
select 1,2,'testdata1',1 union
select 2,3,'testdata2',1 union
select 3,4,'testdata3',1 union
select 5,5,'testdata5',1 union
select 6,7,'testdata6',0

select * from @table1
select * from @table2