我想使用合并更新表,并且我的表源将使用交叉联接

时间:2019-06-21 14:18:43

标签: sql sql-server stored-procedures merge cross-join

我有一个Table1:

IdT1 : 11, 12, 13

IdT1Group : 30,30,30

表2:

IdT2 : 1, 2, 3, 4

IdT1 : 11, 11, 12, 12

Detail : A, B, A, B

AND用户定义的表类型@ T2:

IdT2 : 1, 2, 3

IdT1 : 11,11,11

Detail: A,B,C

我想将Table2更新为:

IdT2 : 1,2,5, 3,4,6, 7,8,9

IdT1 : 11,11,11, 12,12,12, 13,13,13

Detail : A,B,C, A,B,C, A,B,C

因此,我在StoredProcedure中使用了合并:

;WITH Table2
    AS (SELECT Table2.* FROM Table2 INNER JOIN Table1 ON Table2.IdT1 = Table1.IdT1 AND IdT1Group = 30)
    MERGE INTO Table2 AS tblTarget
    USING (SELECT @T2.*, T1Item.IdT1 AS T1Id FROM @T2 CROSS JOIN Table1 where IdT1Group = 30)
            AS tblSource
        ON tblTarget.IdT1 = tblSource.T1Id And tblTarget.IdT2 = tblSource.IdT2

WHEN MATCHED THEN
    UPDATE 
    SET Detail = tblSource.Detail
WHEN NOT MATCHED BY SOURCE THEN
    DELETE
WHEN NOT MATCHED BY TARGET THEN            
    INSERT (IdItem, IdT2, Detail)
    VALUES (tblSource.IdT1,
            tblSource.IdT2, tblSource.Detail);

但是在我尝试了代码之后,我的table2变成了:

IdT2 : 1,2,5, 6,7,8, 9,10,11

IdT1 : 11,11,11, 12,12,12, 13,13,13

Detail: A,B,C, A,B,C, A,B,C

应该是这样:

IdT2 : 1,2,5, 3,4,6, 7,8,9

IdT1 : 11,11,11, 12,12,12, 13,13,13

Detail : A,B,C, A,B,C, A,B,C

1 个答案:

答案 0 :(得分:0)

我认为输出应如下所示,因为其中一个值被删除了。此外,我认为您在IdT2的Table2中具有标识列或增量值。 当您删除项目时,增加的价值将不会被扣除。

输出

        IdT2        IdT1        Detail
        ----------- ----------- -------
        1           11          A
        2           11          B
        5           11          C
        3           12          C
        6           12          A
        7           12          B
        8           13          A
        9           13          B
        10          13          C

SQL语句(注意:创建了#个表而不是物理表来实现此目的)

    create table #Table1
    (
    IdT1    int , IdT1Group int
    )

    insert into #Table1
    select 11 IdT1,30  IdT1Group union all
    select 12 IdT1,30  IdT1Group union all
    select 13 IdT1,30  IdT1Group

    create table #Table2
    (
    IdT2 int identity(1,1),   IdT1     int , Detail varchar(100)
    )

    insert into #Table2
    SELECT   11 IdT1 ,'A' Detail union all
    SELECT   11 ,'B'      union all
    SELECT   12 ,'A'      union all
    SELECT   12 ,'B'

    declare @T2  table  
    (
    IdT2 int ,   IdT1     int , Detail varchar(100)
    )

    insert into @T2
    SELECT 1 IdT2 ,  11 IdT1 ,'A' Detail union all
    SELECT 2 ,  11 ,'B' union all
    SELECT 3 ,  12 ,'C'

    MERGE INTO #Table2 AS tblTarget
    USING (SELECT T2.*, T1Item.IdT1 AS T1Id FROM @T2 T2 CROSS JOIN #Table1 T1Item where IdT1Group = 30)
            AS tblSource
        ON  tblTarget.IdT1 = tblSource.T1Id   And tblTarget.IdT2 = tblSource.IdT2--tblTarget.IdT1 = tblSource.T1Id And tblTarget.IdT2 = tblSource.IdT2 

    WHEN MATCHED THEN
        UPDATE 
        SET Detail = tblSource.Detail
    WHEN NOT MATCHED BY SOURCE THEN
        DELETE
    WHEN NOT MATCHED BY TARGET THEN            
        INSERT (IdT1,  Detail)
        VALUES (tblSource.T1Id,
                 tblSource.Detail);

    select * from #Table2
    order by 2,1

    drop table #Table1
    drop table #Table2