DB2 Merge Query Example (Adding WC SHPARRANGE Records)

So, I was recently working on a client engagement and was needing to update the WebSphere Commerce SHPARRANGE table, to enable certain functionality in an external application. Not knowing what data already existed in the production system, I wanted the ability to write a query that would update existing or insert new data, to verify the records that I needed to exist.

I decided to use a DB2 MERGE statement. Per IBM: “The MERGE statement updates a target (a table or view, or the underlying tables or views of a fullselect) using data from a source (result of a table reference). Rows in the target that match the source can be deleted or updated as specified, and rows that do not exist in the target can be inserted. Updating, deleting or inserting a row in a view updates, deletes or inserts the row in the tables on which the view is based.”

You’ll find an example of the MERGE query below that I used to add/update records into the SHPARRANGE table, combining the proper store, fulfillment center, and shipping mode.

The following variables were used in the query below:

FFMCENTER_NAME: The fulfillment center I want to associate with the store identified by the variable STORE_IDENTIFIER_HOSTED.

FFM_ORGENTITY_NAME: The ORGENTITY name that is used to resolve the ORGENTITY_ID (MEMBER_ID) of the fulfillment center store.

FFM_ORGENTITY_DN: The ORGENTITY DN that is used to resolve the ORGENTITY_ID (MEMBER_ID) of the fulfillment center store.

STORE_IDENTIFER_HOSTED: The identifier of the hosted store I wanted to associate with the fulfillment center, and shipping code.

STORE_IDENTIFIER_CATALOG: The identifier of the catalog store I wanted to associate with the store identified by the variable STORE_IDENTIFIER_HOSTED.

SHIPPING_CODE: The shipping code that I want to associate to the fulfillment center, and store identified by the STORE_IDENTIFIER_HOSTED variable.

CARRIER_NAME: The carrier name of the shipping code associated with the fulfillment center and store.

The query below will generate a temporary table, using the query defined, which will generate a temporary ID, pull a store ID, a fulfillment center ID, and a shipping mode ID. The query below, will only create a single record in the temporary table; however, the MERGE query could work with multiple records in that temporary table, but for my purposes I kept it at a single record.

If the data matches data existing in the table as specified by the ON query part, then the WHEN MATCHED query takes effect, if no data matches existing data in the table, the WHEN NOT MATCHED query takes effect.

Its also important to note, that the UPDATE and INSERT statements aren’t traditional statements, they’re specific to the MERGE statement.