2

我有一个表格,数据如下(Table 1

id          valueId     Value
----------- ----------- --------------------------------------------------
1           1           Value 1
1           1           Value 2
1           1           Value 3
1           2           Value 1
1           2           Value 2
1           2           Value 3

我有另一个数据集,如下所示(DataSet

id          valueId     Value
----------- ----------- -------
1           1           Value 1
1           1           Value 2
1           1           Value 4

现在我需要插入不存在Table 1的记录(你可以看到记录

id          valueId     Value
----------- ----------- -------
1           1           Value 4

不在 中Table 1,该记录应该插入)并且需要从中删除Table 1不在DataSet其中的记录

id          valueId     Value
----------- ----------- -------
1           1           Value 3

,但不影响其他记录(其他记录表示 的记录id=1 and valueId=2)。

我使用了以下我编写的 T-SQL MERGE,它插入了丢失的记录,但它删除了所有不在DataSet

DECLARE @tmp_value AS TABLE
    (
      id INT ,
      valueId INT ,
      [Value] NVARCHAR(50)
    ) ;
INSERT  @tmp_value
        ( [id], [valueId], [Value] )
VALUES  ( 1, 1, N'Value 1' ),
        ( 1, 1, N'Value 2' ),
        ( 1, 1, N'Value 3' ),
        ( 1, 2, N'Value 1' ),
        ( 1, 2, N'Value 2' ),
        ( 1, 2, N'Value 3' ) ;

--SELECT  *
--FROM    @tmp_value AS TV ;
WITH    cte
          AS ( SELECT   1 AS id , 1 AS valueId , 'Value 1' AS [Value]
               UNION
               SELECT   1 AS id , 1 AS valueId , 'Value 2' AS [Value]
               UNION
               SELECT   1 AS id , 1 AS valueId , 'Value 4' AS [Value]
             )
    MERGE @tmp_value AS TV
        USING cte
        ON [cte].[id] = [TV].[id]
            AND [cte].[valueId] = [TV].[valueId]
            AND [cte].[Value] = [TV].[Value]
        WHEN NOT MATCHED 
            THEN INSERT VALUES   ( id , [valueId] , [Value] )
        WHEN NOT MATCHED BY SOURCE 
            THEN DELETE ;

SELECT  *
FROM    @tmp_value

预期结果:

id          valueId     Value
----------- ----------- --------------------------------------------------
1           1           Value 1
1           1           Value 2
1           1           Value 4
1           2           Value 1
1           2           Value 2
1           2           Value 3
4

1 回答 1

1

Update :

DECLARE @tmp_value AS TABLE
    (
      id INT ,
      valueId INT ,
      [Value] NVARCHAR(50)
    ) ;
INSERT  @tmp_value
        ( [id], [valueId], [Value] )
VALUES  ( 1, 1, N'Value 1' ),
        ( 1, 1, N'Value 2' ),
        ( 1, 1, N'Value 3' ),
        ( 1, 2, N'Value 1' ),
        ( 1, 2, N'Value 2' ),
        ( 1, 2, N'Value 3' ) ;

--SELECT  *
--FROM    @tmp_value AS TV ;
WITH    cte
          AS ( SELECT   1 AS id , 1 AS valueId , 'Value 1' AS [Value]
               UNION
               SELECT   1 AS id , 1 AS valueId , 'Value 2' AS [Value]
               UNION
               SELECT   1 AS id , 1 AS valueId , 'Value 4' AS [Value]
              )
    MERGE @tmp_value AS TV
        USING cte
        ON [cte].[id] = [TV].[id]
            AND [cte].[valueId] = [TV].[valueId]
            AND [cte].[Value] = [TV].[Value]
        WHEN NOT MATCHED 
            THEN INSERT VALUES   ( id , [valueId] , [Value] )
        ;


WITH    cte
          AS ( SELECT   1 AS id , 1 AS valueId , 'Value 1' AS [Value]
               UNION
               SELECT   1 AS id , 1 AS valueId , 'Value 2' AS [Value]
               UNION
               SELECT   1 AS id , 1 AS valueId , 'Value 4' AS [Value]
             )
    MERGE @tmp_value AS TV
        USING cte
        ON ([cte].[id] = [TV].[id]
            AND [cte].[valueId] = [TV].[valueId]
           AND [cte].[Value] = [TV].[Value])
            or not( [TV].[id] in (select distinct id from cte)
            and [TV].[valueId] in (select distinct valueid from cte))
        WHEN NOT MATCHED 
            THEN INSERT VALUES   ( id , [valueId] , [Value] )
        WHEN NOT MATCHED BY Source 
            THEN DELETE ;

SELECT  *
FROM    @tmp_value
于 2013-05-06T11:08:12.593 回答