Merge

PURPOSE:  The MERGE command merges all or selected records of the current (master) Data Set (DS) with all or selected records from the transaction DS.  The merged DS of records can overwrite the master DS, the transaction DS, or be written to a separate output DS.

Syntax

MERGE WITH clause MATCH ON clause /MERGED/ TO ds_name

[IN DBL dbl_name]

[IF/:M/; UNLESS/:M/ clause]

[IF:T; UNLESS:T clause]

[IF:A; UNLESS:A clause]

[ADVANCE clause]

[VIA clause]

[SHOWING clause]

[SET clause]

[STOP; END clause]

MERGE WITH clause

described in detail in Chapter 7.

MATCH ON clause

serves a somewhat different purpose in the MERGE command than in the other Join commands.  The match fields are not used to select records having identical match field values, as is the case in most commands, since in a merging operation all records that pass the master and transaction conditional clauses and the after conditional clause, are merged.  The match fields serve only to control the order of the records in the output DS, not actually to match field values: e.g., if the two DS’s are being merged on a numeric field, the value 1.25 in the transaction DS will be placed after 1.24 and before 1.26 from the master DS.  For records whose match fields are identical in master and transaction DS’s, the transaction records are merged before the master records by default. Described in detail in Chapter 7.

/MERGED/ TO ds_name

directs the merged records to the output DS.  The output DS must already exist, and its Schema Definition (SD) must be identical to that of both the master and transaction DS’s.  If there are records in the output DS, they will be cleared before the new merged DS is written out.  Either the master DS or the transaction DS can also be used as the output DS.

IN DBL dbl_name

is used to specify an output DS in another Data Base Library (DBL).

IF/:M/; UNLESS/:M/ clause

can be used to exclude matching records from the output DS. Described in detail in Chapter 7.

IF:T; UNLESS:T clause

described in detail in Chapter 7.

IF:A; UNLESS:A clause

described in detail in Chapter 7.

ADVANCE clause

is used to control the order in which matched records are merged when match field values are identical in master and transaction records.  ADVANCE TRANSACTION, the default, puts transaction records before the matching master records.  ADVANCE MASTER causes master records to precede matching transaction records.  ADVANCE BOTH causes each matching transaction record to be interleaved after a matching master record.  Described in detail in Chapter 7.

VIA clause

specifies a Process Module (PM) to be executed.  When a PM is specified in a MERGE command, the records are merged before the record processing sections of the PM execute.

SHOWING clause

shows all merged records after the SET clause has been applied. Described in detail in Chapter 7.

SET clause

affects only master records in the output DS. Described in detail in Chapter 7.

STOP; END clause

is described in detail in Chapter 7.

EXAMPLE

*USE DS BOOKS
*MERGE WITH NEW IF:T PRICE > 0 MATCH ON TITLE_CODE TO BOOKS

All records from the transaction DS NEW that have a value in the field PRICE greater than zero are merged into the current DS BOOKS.  Both the transaction and master must be sorted on TITLE_CODE.  The transaction records are merged before master records if the TITLE_CODE values are the same.

The following example shows a transaction DS, a master DS, and then the results of merging them with the three ADVANCE options.  The first MERGE command is the default, and so ADVANCE TRANSACTION need not be expressed.

*USE DS MAT2
*EXTRACT
A  TRANS1
A  TRANS2
B  TRANS1
B  TRANS2
*USE DS MAT1
*EXTRACT
A  MASTER1
A  MASTER2
B  MASTER1
B  MASTER2
*MERGE WITH MAT2 ON FLDA TO MAT3 ADVANCE TRANSACTION
*USE DS MAT3
*EXTRACT
A  TRANS1
A  TRANS2
A  MASTER1
A  MASTER2
B  TRANS1
B  TRANS2
B  MASTER1
B  MASTER2
*USE DS MAT1
*MERGE WITH MAT2 ON FLDA TO MAT3 ADVANCE MASTER
*USE DS MAT3
*EXTRACT
A  MASTER1
A  MASTER2
A  TRANS1
A  TRANS2
B  MASTER1
B  MASTER2
B  TRANS1
B  TRANS2
*USE DS MAT1
*MERGE WITH MAT2 ON FLDA TO MAT3 ADVANCE BOTH
*USE DS MAT3
*EXTRACT
A  MASTER1
A  TRANS1
A  MASTER2
A  TRANS2
B  MASTER1
B  TRANS1
B  MASTER2
B  TRANS2

SEE ALSO:  PURGE, SELECT, UPDATE Commands