Learn how to use MERGE to synchronize data between tables with just one SQL statement
by Rafael Victória-Pereira
Editor’s note: This article is excerpted from chapter 12 of SQL for IBM i: A Database Modernization Guide, by Rafael Victória-Pereira.
Often, we’re faced with the need to synchronize two tables, in such a way that the target table gets all the relevant information from the other one. For instance, imagine that you have an inventory table that receives daily updates from several stock movement tables. There are new entries to register (INSERT statements), but also item quantity fluctuations (UPDATE statements), which require separate processing. Naturally, you can also have some items disappearing from stock, and depending on how those situations are handled, these can be updates or actual deletes in the inventory table. With the MERGE statement, you can mash up those statements into a single one.