Monitoring for zero records in the from-file

There are some special considerations for copy when the from-file is a physical or logical file and one or more members to be copied are empty.

A member is considered empty in the following cases:

Members copied involving record selection (CPYFRMQRYF command or the INCCHAR and INCREL parameters of the CPYF command) that produce no records are not considered empty.

When the to-file is a printer file (including *PRINT), or when the to-file is a physical file and you specified MBROPT(*ADD) or MBROPT(*UPDADD), empty from-file members are copied because no existing data will be destroyed. Each member that is copied is identified by a normal copy completion message. If the to-file is spooled, an empty spooled file is produced for each empty from-file member. If the PRINT parameter on the CPYF command specifies *COPIED, *EXCLD, or *ERROR, the empty members are shown in the lists, and no records are printed.

Except for the CPYFRMQRYF command, an empty from-file member is never copied to a diskette or tape file, or to a physical file when MBROPT(*REPLACE) is specified. Empty from-file members are skipped for these types of to-files, and a CPF2869 message is sent (as either an informational or diagnostic message) to identify each empty member. The empty members are skipped to avoid destroying existing data. When an empty from-file member is skipped, the following considerations apply:

When the copy command specifies a generic name or *ALL for the FROMMBR parameter, each empty from-file member skipped is identified by message CPF2869, sent as an informational message. If all the from-file members are skipped, a CPF2870 diagnostic message is sent after all the CPF2869 informational messages, followed by a CPF2817 escape message.

When the copy command specifies a single member name or FROMMBR(*FIRST), or when there is an override for the from-file that forces a single member to be processed, an empty member that is skipped is identified by the diagnostic message CPF2869. The CPF2869 diagnostic message is followed by a CPF2817 escape message.

In the following example, the from-file and to-file are both database files, and EMPTY1 and EMPTY2 are empty members in the from-file.
Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.
PGM
          /* No need to monitor for zero records
             when MBROPT(*ADD) specified    */
CPYF    FROMFILE(D504/GEORGE) TOFILE(D504/KEN) +
  FROMMBR(EMPTY1) TOMBR(MBR1) MBROPT(*ADD)
CPYF    FROMFILE(D504/GEORGE) TOFILE(D504/KEN) +
  FROMMBR(EMPTY2) TOMBR(MBR2) MBROPT(*REPLACE)
MONMSG  MSGID(CPF2817) CMPDTA(CPF2869) +
  EXEC(CLRPFM  FILE(D504/KEN) MBR(MBR2))
          /* Monitor for zero records and
             send a message when all members
             to copy are empty */
CPYF    FROMFILE(D504/GEORGE) +
  TOFILE(D504/NEWFILE) FROMMBR(EMPTY*) +
  TOMBR(NEWMBR) MBROPT(*REPLACE)
MONMSG  MSGID(CPF2817) CMPDTA(CPF2870) +
  EXEC(SNDPGMMSG TOPGMQ(*EXT) +
  MSG('All members to copy are empty'))
ENDPGM

For the first CPYF command, MBROPT(*ADD) is specified, so an escape message is not sent to the program because of the empty from-file member. Note that if MBR1 does not exist before the copy, it is added to the to-file (if either the from-file member is empty or contains data).

For the second CPYF command, copy does not clear the to-file member when the from-file member is empty, so the MONMSG command after the second CPYF command starts the CLRPFM command to clear the to-file member when the from-file member is empty.

For the third CPYF command, the CPF2817 escape message has compare data of CPF2870 if all members to be copied are empty because the generic from-file member name, EMPTY*, requests that multiple members be copied.