Sql Merge-如何在匹配后从Target中删除原始文件时从源表中插入行?

时间:2015-12-22 16:46:56

标签: sql-server tsql stored-procedures merge

我目前有一个存储过程,可以将目标表(Ticket_Report)与我的数据源表(New_Tickets)进行比较。

我使用MERGE INTO语句来比较这两个。当它在两个表之间找到匹配时,它会使用源表中的相应信息更新目标表中的当前行。如果它找到匹配项,它会将源表中的数据插入到目标表中。

MERGE INTO Ticket_REPORT T1
USING @New_Tickets T2
ON T1.TICKET_NO=T2.TICKET_NO
WHEN MATCHED THEN
UPDATE SET 
  T1.TICKET_NO = T2.TICKET_NO, 
  T1.ASSIGNED_GROUP = T2.ASSIGNED_GROUP, 
  T1.ASSIGNEE = T2.ASSIGNEE, 
  T1.FNAME = T2.FNAME, 
  T1.LNAME = T2.LNAME
WHEN NOT MATCHED THEN
 INSERT VALUES(
  T2.TICKET_NO, 
  T2.ASSIGNED_GROUP, 
  T2.ASSIGNEE, 
  T2.FNAME, 
  T2.LNAME

);

我需要更改它,以便在Ticket Number上找到匹配时,而不仅仅是更新它,我需要A.)通过删除它来替换Target表中的当前行,然后B.)插入来自源表的相应行。

我目前有

MERGE INTO Ticket_REPORT T1
  USING @New_Tickets T2
  ON T1.Ticket_NO=T2.Ticket_NO
  WHEN MATCHED THEN DELETE
//Now I need to replace what I deleted with the row from the source table

将删除目标表中的行。现在我想从源表中插入相应的行。我在WHEN MATCHED子句中尝试做多个事情时遇到了麻烦。有谁知道我怎么能做到这一点?

*旁注:匹配后,我可以从Source插入行,但是我如何删除原始行?

2 个答案:

答案 0 :(得分:0)

严格解决为您的老板

  1. 首先将匹配的记录放入一个临时表

  2. 接下来使用合并查询从目标表中删除匹配的记录并插入不匹配的记录

  3. 最后将临时表中的记录插入目标表

  4. 尝试这样的事情

    select * into #temp 
    from  @New_Tickets T2 
    where exists(select 1 
                 from Ticket_REPORT T1
                 where T1.Ticket_NO=T2.Ticket_NO)
    
    
    MERGE INTO Ticket_REPORT T1
    USING @New_Tickets T2
    ON T1.TICKET_NO=T2.TICKET_NO
    WHEN MATCHED THEN DELETE
    WHEN NOT MATCHED THEN
     INSERT VALUES(
      T2.TICKET_NO, 
      T2.ASSIGNED_GROUP, 
      T2.ASSIGNEE, 
      T2.FNAME, 
      T2.LNAME
    );
    
    insert into Ticket_REPORT (col1,col2,..)
    select col1,col2,..
    from #temp
    

    注意:

    必须做的是从目标表中删除匹配的记录,然后将所有记录从源表插入目标表

答案 1 :(得分:0)

一种方法是子查询您的MERGE语句。您可以根据操作(INSERT,UPDATE或DELETE)过滤输出。然后可以对已过滤的记录进行INSERTED,UPDATED或DELETED。

这是将缓慢变化的维度加载到数据仓库中的常用技术。

我的示例使用以下临时表:

SETUP

/* Create and populate sample tables.
 */
CREATE TABLE #NewTicket
    (
        Id INT
    )
;

CREATE TABLE #TicketReport
    (
        Id INT
    )
;

INSERT INTO #NewTicket
    (
        Id
    )
VALUES
    (1),
    (2),
    (3)
;

INSERT INTO #TicketReport
    (
        Id
    )
VALUES
    (3),
    (4),
    (5)
;

在示例1中,2和3出现在新票证中。票证报告中出现3,4和5。 MERGE语句删除3,外部查询删除。

实施例

/* Filter the results from the sub query 
 * for deleted records.
 * These are then appended in the main
 * outer query.
 */
INSERT INTO #TicketReport
    (
        Id
    )
SELECT
    Id
FROM
    (
        /* MERGE statments can be used as a sub query.
         * You'll need the OUTPUT clause for this to work.
         * The column $action describes what happened to each record.
         */
        MERGE
            #TicketReport AS t
                USING #NewTicket AS s        ON s.Id = t.Id
        WHEN MATCHED THEN
            DELETE
        WHEN NOT MATCHED BY TARGET THEN
            INSERT
                (
                    Id
                )
            VALUES
                (
                    Id
                )
        OUTPUT 
            $action,
            s.*
    ) AS r
WHERE
    [$Action] = 'DELETE'
;

/* View the final result.
 */
SELECT
    *
FROM
    #TicketReport
ORDER BY
    Id
;