Merge Confusion Help please. RRS feed

  • Question

  • Ok so i have an ETL process that right now im Importing 5 days worth of data and then putting that in a temp table, deleting from the production table where the record exists in the temp table and then inserting everything from the temp table into production table.  May seem weird but this is the way i need to do this.  

    However i understand Merge might be able to do this better.  So i wrote a statement and im afraid to run it as i want to make sure its right.

    Merge Prod_Table As A Using Temp_Table As B
    On A.Col1= B.Col1
    When Matched Then Delete
    When Not Matched Then Insert
    (List of columns)
    (List of columns);

    So my questions are 1, does this delete everything from prod and insert from temp at the same time which means that the records it deleted do not get re-inserted?

    2 - Is it doing what i want?

    3 - Can i make this easier to replicate in the future by involving an insert into Prod_Table select * From temp_table rather than listing out all the columns each time?

    • Edited by Fanatic1074 Friday, November 22, 2019 12:36 AM
    Friday, November 22, 2019 12:33 AM

All replies

  • Hi Fanatic1074,

    >> So i wrote a statement and im afraid to run it as i want to make sure its right.

    I suggest you import your data into a test environment and check the correct of the statement. I think it is the best method to check this.

    Best regards,

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to  MSDN Support, feel free to contact

    Monday, November 25, 2019 8:54 AM