29
Fri, Nov
0 New Articles

TOP TIPS: CL September 1998

CL
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

 

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

 

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: