23
Tue, Apr
1 New Articles

What Does That Query Do?

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

Query/400 is a flexible piece of software commonly used to produce ad hoc reports and temporary extraction files for batch jobs. Adding to Query/400’s flexibility is its ease of use. With minimal training, users can produce their own reports and views of data. This user independence is a blessing in terms of freeing your IT staff from creating simple reports and inquiries but becomes a burden when these queries abend because of file changes such as those made by AS/400 shops to prepare for Y2K. Joe in accounting can crunch the monthly numbers at will, but when you change the general receivables file, those queries he has built over it feel the pain. Any file change necessitates a “refresh” before a Query/400 query can be executed with the Run Query (RUNQRY) command. Without a refresh, using RUNQRY results in error QRY1058 (Level for file XXX in YYY does not match query.)

We have developed a utility that builds a cross-reference file of queries and the files they read. We call it Build Query Cross-reference (BLDQRYXREF). Not only can it help you prepare for Y2K, but it is also useful for much more.

All you need to understand about BLDQRYXREF is its command interface and the physical file it loads, QRYXREF. Besides these objects, BLDQRYXREF also has three CL programs and one RPG III program. BLDQRYXREF loads information about queries and the files they read into physical file QRYXREF (see Figure 1). You can place this file into any library you want, but make sure that library is on the library list when you run BLDQRYXREF.

The Quest for Queries

BLDQRYXREF uses two parameters, as shown in Figure 2. The first parameter, which specifies the libraries to search for queries, can be a list of up to 50 library names or one of these special values: *LIBL, *USRLIBL, *CURLIB, *ALL, or *ALLUSR. The second parameter lets you tell BLDQRYXREF whether or not to clear the QRYXREF physical file before loading it.

BLDQRYXREF executes CL program QRY003CL, which extracts library names from the list passed by the command (see Figure 3). Each time it extracts a library name, it calls CL program QRY004CL to retrieve the source code of all queries in a single library or in all the libraries to which a special value such as *ALLUSR refers (see Figure 4). QRY004CL also uses the Display Object Description (DSPOBJD) command to find query objects. For each query found, it executes Retrieve Query Management Query

(RTVQMQRY) to generate source code for the query and place that source code into a new member of a temporary source file. Members are given system-generated names in case queries in different libraries have the same name, and the name of the query and name of the library in which it resides are stored in the source member’s text description for later retrieval.

After QRY003CL has processed the last library in the parameter list, it issues an override to the temporary source file so RPG III program QRY004RG can process all the members, not just the first one. QRY003CL calls QRY004RG, which reads the temporary source file members, searching for lines beginning with two blanks and the word FROM. Such source records indicate the beginning of the list of files used by a query. If a query reads more than one file, the subsequent lines begin with blanks and contain other file names. Each time this RPG program finds a record containing a file name, it writes a record to file QRYXREF (see Figure 5). Because QRYXREF contains other information not found in the query source code, QRY004RG calls CL program QRY005CL, which is shown in Figure 6. QRY005CL executes Retrieve Member Description (RTVMBRD) for this additional information.

Unfortunately, this utility cannot detect outfiles produced by Query/400 queries. We wish it could, but the source code produced by RTVQMQRY does not contain that information.

BLDQRYXREF has completed its task, and physical file QRYXREF now contains a list of files read by Query/400 queries from selected libraries. What you do with this information is up to you. Use relational tools, such as Query and SQL, or write programs to read QRYXREF, and coordinate a query refresh with the file information when you roll file changes into production.

Clean Up the Quiescent

Many queries are used once and forgotten. These derelict objects occupy valuable disk space. One shop had more than 12,000 Query/400 queries cluttering up its system. Pay attention to the last used date field in your QYRXREF file. Delete obsolete queries and consolidate redundant queries into one version.

This utility can manage not only your Y2K efforts but also any other file changes. With BLDQRYXREF, managing Query/400 queries is a breeze.

*===============================================================

* To compile:

*

* CRTPF FILE(XXX/QRYXREF) SRCFILE(XXX/QDDSSRC)

*

*===============================================================

A R QRYXREFR

A QNAME 10 COLHDG(' ' 'Query' 'name')

A QLIB 10 COLHDG(' ' 'Query' 'library')

A FNAME 10 COLHDG(' ' 'File' 'name')

A FLIB 10 COLHDG(' ' 'File' 'library')

A CRTUSR 10 COLHDG(' ' 'Created' 'by')

A CRTDAT 13 COLHDG(' ' 'Date' 'created')

A USEDAT 7 COLHDG('Date' 'last' 'used')

A USECT 5 0 COLHDG(' Number' 'of times' +

' used') /*==================================================================*/

/* To compile: */
/* */

/* CRTCMD CMD(XXX/BLDQRYXREF) PGM(XXX/QRY003CL) + */
/* SRCFILE(XXX/QCMDSRC) */
/* */

/*==================================================================*/

CMD PROMPT('Build query cross reference')

Figure 1: QRYXREF stores information about queries and the files they use.

PARM KWD(LIB) TYPE(*NAME) LEN(10) SNGVAL((*LIBL) +

(*USRLIBL) (*CURLIB) (*ALL) (*ALLUSR)) +

MIN(1) MAX(50) PROMPT('Libraries to search')

PARM KWD(CLEAR) TYPE(*LGL) RSTD(*YES) DFT(*NO) +

SPCVAL((*NO '0') (*YES '1')) +

CHOICE('*YES, *NO') PROMPT('Clear cross +

reference file?') /*==================================================================*/

/* Load the query cross reference file for a list of libraries. */
/*==================================================================*/

/* To compile: */
/* */

/* CRTCLPGM PGM(XXX/QRY003CL) SRCFILE(XXX/QCLSRC) */
/* */

/*==================================================================*/

PGM PARM(&LISTOFLIBS &CLEARXREF)

DCL VAR(&LISTOFLIBS) TYPE(*CHAR) LEN(502)

DCL VAR(&LIB) TYPE(*CHAR) LEN(10)

DCL VAR(&CLEARXREF) TYPE(*LGL)

DCL VAR(&QUERYCOUNT) TYPE(*DEC) LEN(9)

DCL VAR(&CURMBR) TYPE(*CHAR) LEN(10) VALUE(Q)

DCL VAR(&LIBCOUNT) TYPE(*DEC) LEN(5)

DCL VAR(&PROCESSED) TYPE(*DEC) LEN(5)

DCL VAR(&OFFSET) TYPE(*DEC) LEN(5) VALUE(3)

DCL &ERRBYTES *CHAR 4 VALUE(X'00000000')

DCL &ERROR *LGL VALUE('0')

DCL &MSGKEY *CHAR 4

DCL &MSGTYP *CHAR 10 VALUE('*DIAG')

DCL &MSGTYPCTR *CHAR 4 VALUE(X'00000001')

DCL &PGMMSGQ *CHAR 10 VALUE('*')

DCL &STKCTR *CHAR 4 VALUE(X'00000001')

MONMSG MSGID(CPF0000) EXEC(GOTO ERRPROC)

/* Process the next library in the list in the &LIB parameter */

CHGVAR VAR(&LIBCOUNT) VALUE(%BIN(&LISTOFLIBS 1 2))
NEXT_LIB:

IF COND(&PROCESSED *LT &LIBCOUNT) THEN(DO)

CHGVAR VAR(&LIB) VALUE(%SST(&LISTOFLIBS &OFFSET 10))

CALL PGM(QRY004CL) PARM(&LIB &QUERYCOUNT)

CHGVAR VAR(&PROCESSED) VALUE(&PROCESSED + 1)

CHGVAR VAR(&OFFSET) VALUE(&OFFSET + 10)

GOTO CMDLBL(NEXT_LIB)

ENDDO

/* Write to the cross reference file, QRYXREF */

IF COND(&CLEARXREF) THEN(CLRPFM FILE(QRYXREF))

OVRDBF FILE(QRYSRC) TOFILE(QTEMP/QRYSRC) MBR(*ALL)

CALL PGM(QRY003RG)

DLTOVR FILE(QRYSRC)

RETURN

/*==================================================================*/

/* Error processing routine */

/*==================================================================*/

ERRPROC:

IF COND(&ERROR) THEN(GOTO 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 /*==================================================================*/

/* Load the query reference file with information about queries */

Figure 2: BLDQRYXREF is easy to use because it has only two parameters.

Figure 3: Program QRY003CL processes the list of selected libraries.

/* from one library. */
/*==================================================================*/

/* To compile: */
/* */

/* CRTCLPGM PGM(XXX/QRY004CL) SRCFILE(XXX/QCLSRC) */
/* */

/*==================================================================*/

PGM PARM(&LIB &QUERYCOUNT)

DCL VAR(&LIB) TYPE(*CHAR) LEN(10)

DCL VAR(&QUERYCOUNT) TYPE(*DEC) LEN(9)

DCL VAR(&CURMBR) TYPE(*CHAR) LEN(10) VALUE(Q)

DCLF FILE(QADSPOBJ)

DCL &ERRBYTES *CHAR 4 VALUE(X'00000000')

DCL &ERROR *LGL VALUE('0')

DCL &MSGKEY *CHAR 4

DCL &MSGTYP *CHAR 10 VALUE('*DIAG')

DCL &MSGTYPCTR *CHAR 4 VALUE(X'00000001')

DCL &PGMMSGQ *CHAR 10 VALUE('*')

DCL &STKCTR *CHAR 4 VALUE(X'00000001')

MONMSG MSGID(CPF0000) EXEC(GOTO ERRPROC)

CRTSRCPF FILE(QTEMP/QRYSRC) TEXT('Source for +

Query/400 Files')

MONMSG MSGID(CPF7302)

DSPOBJD OBJ(&LIB/*ALL) OBJTYPE(*QRYDFN) +

OUTPUT(*OUTFILE) OUTFILE(QTEMP/QADSPOBJ)

MONMSG MSGID(CPF0000)

OVRDBF FILE(QADSPOBJ) TOFILE(QTEMP/QADSPOBJ) +

SHARE(*YES)
RCVF:

RCVF RCDFMT(QLIDOBJD)

MONMSG MSGID(CPF0864) EXEC(GOTO CMDLBL(EOF))

CHGVAR VAR(&QUERYCOUNT) VALUE(&QUERYCOUNT + 1)

CHGVAR VAR(%SST(&CURMBR 2 9)) VALUE(&QUERYCOUNT)

RTVQMQRY QMQRY(&ODLBNM/&ODOBNM) SRCFILE(QTEMP/QRYSRC) +

SRCMBR(&CURMBR) ALWQRYDFN(*YES)

CHGPFM FILE(QTEMP/QRYSRC) MBR(&CURMBR) +

TEXT(&ODLBNM *CAT &ODOBNM)

MONMSG MSGID(CPF0000)

GOTO CMDLBL(RCVF)

EOF:

DLTOVR FILE(QADSPOBJ)

RETURN

/*==================================================================*/

/* Error processing routine */

/*==================================================================*/

ERRPROC:

IF COND(&ERROR) THEN(GOTO 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

*===============================================================

* Extract file & library names from query source and load the

* query cross reference file.

*===============================================================

* To compile:

*

* CRTRPGPGM PGM(XXX/QRY003RG) SRCFILE(XXX/QRPGSRC)

*

*===============================================================

*

FQRYSRC IF F 92 DISK KINFDS INFDS

FQRYXREF O E DISK

Figure 4: Program QRY004CL finds queries and retrieves their source code.

ISRCREC DS

I 13 92 SRCDTA

I 13 22 RECID

IINFDS DS

I 129 138 MBRNAM

C MOVE *OFF FROM 1

C EXSR READ

C EOF DOWEQ*OFF

C* If new member, retrieve member info

C MBRNAM IFNE MBRSAV

C CALL 'QRY005CL'

C PARM MBRNAM

C PARM QLIB

C PARM QNAME

C PARM CRTDAT

C PARM CRTUSR

C PARM USECT

C PARM USEDAT

C MOVELMBRNAM MBRSAV

C *LIKE DEFN MBRNAM MBRSAV

C ENDIF

C SELEC

C RECID WHEQ ' FROM '

C MOVE *ON FROM

C RECID WHNE *BLANKS

C MOVE *OFF FROM

C ENDSL

C FROM IFEQ *ON

C EXSR GETFIL

C WRITEQRYXREFR

C ENDIF

C EXSR READ

C ENDDO

C MOVE *ON *INLR

***********

C READ BEGSR

C*

C READ QRYSRC SRCREC 91

C MOVE *IN91 EOF 1

C*

C ENDSR

***********

* Extract the file name & library name from the query source

***********

C GETFIL BEGSR

C*

C* W = position of first character of library/file name

C* X = position of slash separator

C* Y = position of first character of file name

C* Z = position of first blank after library/file name

C*

C MOVE *ZERO W 30

C MOVE *ZERO X 30

C MOVE *ZERO Y 30

C MOVE *ZERO Z 30

C MOVE *BLANKS FNAME

C MOVE *BLANKS FLIB

C*

C ' ' CHECKSRCDTA:9 W

C W IFGT *ZERO

C '/' SCAN SRCDTA:W X

C ' ' SCAN SRCDTA:W Z

C ENDIF

C*

C X IFGT *ZERO SLASH FOUND

C X SUB W LEN 30

C LEN SUBSTSRCDTA:W FLIB

C X ADD 1 Y

C Z SUB Y LEN

C LEN SUBSTSRCDTA:Y FNAME

C ELSE

C Z SUB W LEN

C LEN SUBSTSRCDTA:W FNAME

C ENDIF

C*

C ENDSR /*==================================================================*/

/* Retrieve member info for a query definition. The query name and */
/* library name are stored in the text field of the source member. */
/*==================================================================*/

/* To compile: */
/* */

/* CRTCLPGM PGM(XXX/QRY005CL) SRCFILE(XXX/QCLSRC) */
/* */

/*==================================================================*/

Figure 5: Program QRY003RG reads query source and writes to the cross-reference file.

PGM PARM(&MEMBER &QRYLIB &QRYNAME &CRTDATE &CRTUSER &USECT +

&LASTUSEDAT)

DCL VAR(&MEMBER) TYPE(*CHAR) LEN(10)

DCL VAR(&QRYLIB) TYPE(*CHAR) LEN(10)

DCL VAR(&QRYNAME) TYPE(*CHAR) LEN(10)

DCL VAR(&CRTDATE) TYPE(*CHAR) LEN(13)

DCL VAR(&CRTUSER) TYPE(*CHAR) LEN(10)

DCL VAR(&USECT) TYPE(*DEC) LEN(5)

DCL VAR(&LASTUSEDAT) TYPE(*CHAR) LEN(7)

DCL VAR(&TEXT) TYPE(*CHAR) LEN(50)

MONMSG MSGID(CPF0000)

CHGVAR VAR(&QRYLIB) VALUE(' ')

CHGVAR VAR(&QRYNAME) VALUE(' ')

CHGVAR VAR(&CRTDATE) VALUE(' ')

CHGVAR VAR(&CRTUSER) VALUE(' ')

CHGVAR VAR(&USECT) VALUE(0)

CHGVAR VAR(&LASTUSEDAT) VALUE(' ')

RTVMBRD FILE(QTEMP/QRYSRC) MBR(&MEMBER) TEXT(&TEXT)

CHGVAR VAR(&QRYLIB) VALUE(%SST(&TEXT 1 10))

CHGVAR VAR(&QRYNAME) VALUE(%SST(&TEXT 11 10))

RTVOBJD OBJ(&QRYLIB/&QRYNAME) OBJTYPE(*QRYDFN) +

CRTDATE(&CRTDATE) CRTUSER(&CRTUSER) +

USEDATE(&LASTUSEDAT) USECOUNT(&USECT)

RETURN

ENDPGM

Figure 6: Program QRY005CL retrieves information about query objects.

TED HOLT

Ted Holt is IT manager of Manufacturing Systems Development for Day-Brite Capri Omega, a manufacturer of lighting fixtures in Tupelo, Mississippi. He has worked in the information processing industry since 1981 and is the author or co-author of seven books. 


MC Press books written by Ted Holt available now on the MC Press Bookstore.

Complete CL: Fifth Edition Complete CL: Fifth Edition
Become a CL guru and fully leverage the abilities of your system.
List Price $79.95

Now On Sale

Complete CL: Sixth Edition Complete CL: Sixth Edition
Now fully updated! Get the master guide to Control Language programming.
List Price $79.95

Now On Sale

IBM i5/iSeries Primer IBM i5/iSeries Primer
Check out the ultimate resource and “must-have” guide for every professional working with the i5/iSeries.
List Price $99.95

Now On Sale

Qshell for iSeries Qshell for iSeries
Check out this Unix-style shell and utilities command interface for OS/400.
List Price $79.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

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: