ALWCPYDTA(*OPTIMIZE) Doesn’t Like Invalid Decimal Data
Most of us in the AS/400 community are fortunate enough to work on data that has integrity. That is, we expect the data in a field to be in the format that has been defined for it.
This is not always the case. If you are using data that originated on another system, such as a mainframe or a S/36, you may be in for a surprise. Many old COBOL programs define numeric fields as alpha so they can be tested for valid numeric data. As long as the file is internally defined, this poses no problem.
Fast-forward now to the AS/400. A file is defined with DDS, and data may be copied to the new definition with a Copy File (CPYF) command using the FMTOPT(*NOCHK) parameter. A numeric field may have invalid decimal data in it, but this does not cause a problem. Programs using this file with invalid decimal data are written. They work well, provided they never refer to the field with invalid decimal data.
Then, one day, you try to improve the performance of a program that uses this file by specifying ALWCPYDTA(*OPTIMIZE) in an Open Query File (OPNQRYF) command. ALWCPYDTA(*OPTIMIZE) allows the query engine to use a sort routine if that appears to be the fastest way to access the data. However, in the process of sorting, it also performs integrity checking on the data fields, even those that are not key fields, mapped fields, or selection fields!
The OPNQRYF command completes normally, but the program that uses the opened file will fail with a CPF5029 error.
The solution is to fix the data, of course. In the meantime, changing ALWCPYDTA (*OPTIMIZE) to ALWCPYDTA(*YES) allows the program to run.
— David Abramowitz Atlas—The Software Co.
This email address is being protected from spambots. You need JavaScript enabled to view it.
Select Records Across the Centuries
Suppose you have a program that selects purchase orders created over a range of dates, as in Figure 1. This program selects the correct records if &FROMDT and &THRUDT are both from the 1900s, but it doesn’t work when &FROMDT is from the 1900s and &THRUDT is in the year 2000 or after.
An easy way to solve this problem is to use different record selection logic in different circumstances, as I demonstrate in Figure 2. If &FROMDT is less than or equal to &THRUDT, the record selection logic is unchanged. But if &FROMDT is greater than &THRUDT, the program gets all records with creation dates greater than or equal to &FROMDT and records with creation dates less than or equal to &THRUDT.
— Ted Holt Senior Technical Editor Midrange Computing
CL Can Read, but It Can’t Write
IBM could have given CL more I/O capabilities, but the company chose not to. Maybe IBM was afraid CL would replace RPG as the AS/400 programming language of choice. In any event, I’ve found it helpful to stretch CL’s file-processing abilities through the years. Figure 3 contains the source code for the Write File (WRTF) command. Its command processing program (CPP) is WRT001CL (Figure 4), which calls RPG program WRT001RG (Figure 5). This program writes up to 12 records to a physical file.
Once you grant CL the ability to write to disk files, you won’t have any trouble coming up with useful things to do with it. The following is a list of just a few things you can do:
• Log the contents of CL variables to a disk file to aid in debugging
• Record each time a CL program runs, with full information about it (e.g., user profile, date, and time)
• Build a source member from user input and/or other disk files and immediately compile it
• Load a single-record data member with record-selection values and/or general information to be passed into a query and then join that member to a query with a Cartesian product
If you implement this command in your shop, you may want to change the limiting numbers. I usually use this command to write a single record to a source file, so allowing 12 records of 96 bytes is more than adequate for my purposes.
By the way, you may already have a utility like this on your system. If you have QUSRTOOL or the TAA Productivity Tools, look for Write Source File (WRTSRCF).
—Ted Holt Senior Technical Editor Midrange Computing
Run COMMANDS Through QCMDEXC to Retain Significant Trailing Blanks
To copy selected records that include trailing blanks into selection criteria, you need to use single quotes in the Copy File (CPYF) command’s records by character test option. For example, if you need to copy all records from position one through four that contain
the characters ABC followed by a blank, you must enter the value in single quotes (‘ABC’). Otherwise, the system will consider only the leading, nonblank characters, and you will also get records that contain values like ABCD. Your CPYF command needs to look as follows:
CPYF FROMFILE(OLDFILE) +
TOFILE(NEWFILE) +
MBROPT(*ADD) +
INCCHAR(*RCD 1 *EQ ‘ABC ‘)
However, what if this CPYF command is in a CL program and record selection depends on a variable passed to the program? You might think you could replace the fourth value in the INCCHAR parameter with a variable, like this:
CPYF FROMFILE(OLDFILE) +
TOFILE(NEWFILE) +
MBROPT(*ADD) +
INCCHAR(*RCD 1 *EQ &PATTERN)
If you do, the CPYF command will ignore the trailing blanks on the &PATTERN and will match only the leading, nonblank characters. To make this work correctly, build the CPYF command in a CL variable to include apostrophes in the command string, and call QCMDEXC to execute the command, as in Figure 6.
— Ronald Katz RK Consulting This email address is being protected from spambots. You need JavaScript enabled to view it.
DCL &FROMDT *CHAR 6 /* YYMMDD FORMAT */
DCL &THRUDT *CHAR 6 /* YYMMDD FORMAT */
OVRDBF FILE(PURCHORD) SHARE(*YES)
OPNQRYF FILE((PURCHORD)) +
QRYSLT(‘PODATE = %RANGE(‘ *CAT +
&FROMDT *BCAT &THRUDT *CAT ‘)’) +
KEYFLD((VENDOR))
CALL PGM(PUR0110RG)
CLOF OPNID(PURCHORD)
DLTOVR FILE(PURCHORD)
DCL &QRYSLT *CHAR 256 /* YYMMDD FORMAT */
IF (&FROMDT *LE &THRUDT) DO
CHGVAR &QRYSLT (‘PODATE = %RANGE(‘ *CAT +
&FROMDT *BCAT &THRUDT *CAT ‘)’)
ENDDO
ELSE DO
CHGVAR &QRYSLT (‘(PODATE *GE ‘ *CAT &FROMDT *BCAT ‘*OR +
PODATE *LE ‘ *CAT &THRUDT *CAT ‘)’)
ENDDO
OVRDBF FILE(PURCHORD) SHARE(*YES)
OPNQRYF FILE((PURCHORD)) QRYSLT(&QRYSLT) KEYFLD((VENDOR))
(etc.) /*==================================================================*/
/* To compile: */
/* */
/* CRTCMD CMD(XXX/WRTF) PGM(XXX/WRT001CL) + */
/* SRCFILE(XXX/QCMDSRC) */
/* */
/*==================================================================*/
CMD PROMPT(‘Write to a file’)
PARM KWD(TEXT) TYPE(*CHAR) LEN(96) MIN(1) MAX(12) +
Figure 1: This record selection logic works if the first two digits of two dates are the same
Figure 2: This program considers that an ending date may be less than a beginning date
EXPR(*YES) PROMPT(‘Text to be written’)
PARM KWD(FILE) TYPE(QUAL1) MIN(1) PROMPT(‘File’)
PARM KWD(FILETYPE) TYPE(*CHAR) LEN(6) RSTD(*YES) +
DFT(SOURCE) VALUES(SOURCE DATA) +
PROMPT(‘Source or data file?’)
PARM KWD(MEMBER) TYPE(*NAME) DFT(*FIRST) +
SPCVAL((*FIRST)) EXPR(*YES) PROMPT(‘Member’)
QUAL1: QUAL TYPE(*NAME) MIN(1) EXPR(*YES)
QUAL TYPE(*NAME) DFT(*LIBL) SPCVAL((*CURLIB) +
(*LIBL)) EXPR(*YES) PROMPT(‘Library’) /*==================================================================*/
/* To compile: */
/* */
/* CRTCLPGM PGM(XXX/WRT001CL) SRCFILE(XXX/QCLSRC) */
/* */
/*==================================================================*/
PGM PARM(&TEXT &FILE &FILETYPE &MEMBER)
DCL VAR(&TEXT) TYPE(*CHAR) LEN(1154)
DCL VAR(&FILE) TYPE(*CHAR) LEN(20)
DCL VAR(&FILETYPE) TYPE(*CHAR) LEN(6)
DCL VAR(&MEMBER) TYPE(*CHAR) LEN(10)
/* Declare error processing variables */
DCL VAR(&ERRBYTES) TYPE(*CHAR) LEN(4) +
VALUE(X’00000000’)
DCL VAR(&ERROR) TYPE(*LGL) VALUE(‘0’)
DCL VAR(&MSGKEY) TYPE(*CHAR) LEN(4)
DCL VAR(&MSGTYP) TYPE(*CHAR) LEN(10) VALUE(‘*DIAG’)
DCL VAR(&MSGTYPCTR) TYPE(*CHAR) LEN(4) +
VALUE(X’00000001’)
DCL VAR(&PGMMSGQ) TYPE(*CHAR) LEN(10) VALUE(‘*’)
DCL VAR(&STKCTR) TYPE(*CHAR) LEN(4) +
VALUE(X’00000001’)
MONMSG MSGID(CPF0000) EXEC(GOTO CMDLBL(ERRPROC))
CHKOBJ OBJ(%SST(&FILE 11 10)/%SST(&FILE 1 10)) +
OBJTYPE(*FILE) MBR(&MEMBER) AUT(*ADD)
OVRDBF FILE(FILEOUT) TOFILE(%SST(&FILE 11 +
10)/%SST(&FILE 1 10)) MBR(&MEMBER)
CALL PGM(WRT001RG) PARM(&TEXT &FILETYPE)
DLTOVR FILE(FILEOUT)
RETURN
/*==================================================================*/
/* Error processing routine */
/*==================================================================*/
ERRPROC:
IF COND(&ERROR) THEN(GOTO CMDLBL(ERRDONE))
ELSE CMD(CHGVAR VAR(&ERROR) VALUE(‘1’))
/* Move all *DIAG messages to previous program queue */
CALL PGM(QMHMOVPM) PARM(&MSGKEY &MSGTYP +
&MSGTYPCTR &PGMMSGQ &STKCTR &ERRBYTES)
/* Resend last *ESCAPE message */
ERRDONE:
CALL PGM(QMHRSNEM) PARM(&MSGKEY &ERRBYTES)
MONMSG MSGID(CPF0000) EXEC(DO)
SNDPGMMSG MSGID(CPF3CF2) MSGF(QCPFMSG) +
MSGDTA(‘QMHRSNEM’) MSGTYPE(*ESCAPE)
MONMSG MSGID(CPF0000)
ENDDO
ENDPGM *===============================================================
* To compile:
*
* CRTRPGPGM PGM(XXX/WRT001RG) SRCFILE(XXX/QRPGSRC)
Figure 3: The WRTF command lets CL append records to disk files
Figure 4: WRT001CL is the CPP for the WRTF command
*
*===============================================================
FFILEOUT O F 108 DISK
E DTA 12 96
IDATA DS 1154
I B 1 20DCT
I 31154 DTA
C *ENTRY PLIST
C PARM DATA
C PARM FILTYP 1 SOURCE, DATA
C*
C FILTYP IFEQ ‘D’
C MOVE *ON *IN21
C ENDIF
C*
C X DOWLTDCT
C X ANDLT12
C ADD 1 X 30
C EXCPTDLINE
C ENDDO
C*
C MOVE *ON *INLR
OFILEOUT E DLINE
O N21 DTA,X 108
O 21 DTA,X 96 DCL VAR(&PATTERN) TYPE(*CHAR) LEN(4)
DCL VAR(&CMD) TYPE(*CHAR) LEN(512)
CHGVAR VAR(&CMD) VALUE(‘CPYF FROMFILE(QTEMP/ONE) +
TOFILE(QTEMP/TWO) MBROPT(*ADD) +
INCCHAR(*RCD 1 *EQ ‘’’ *CAT &PATTERN *CAT +
‘’’) FMTOPT(*NOCHK)’)
CALL PGM(QCMDEXC) PARM(&CMD 512)
Figure 5: WRT001RG, the brawn behind WRTF
Figure 6: Executing CPYF through QCMDEXC embeds needed apostrophes in the INCCHAR parameter
LATEST COMMENTS
MC Press Online