29
Mon, Apr
1 New Articles

SQL Minimalism

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

As a consultant, you run into a lot of interesting situations, and some days you walk away thinking, "How do these people get anything done?" In one shop where I worked, the backlog of custom reports and screens seemed to have been piling up since before the dawn of civilization. Yet, the basic tools needed to develop some quick custom queries were nonexistent. Maybe you've worked in a shop like that: one in which the AS/400 is so stripped down that it's a wonder that the machine can even IPL in the morning. I coined a name for these shops. I called them "OS/400 minimalists," and I soon learned to love them. Why? Because it's in these minimalist shops that you really discover the robust nature of OS/400 and your own creative resources to make them work. It was in these shops that I discovered the AS/400's Query Management/400 (QM/400) facility, a no-cost SQL compiler built right into the heart of DB2/400. With this built-in facility, you can knock out a lot of sophisticated custom reports to satisfy a minimalist customer's craving for real and immediate information. And it runs standard SQL without your having to buy other expensive reporting tools. QM/400 is an easy tool to use and takes just a moment to learn.

In this article, I'll demonstrate how to use QM/400 by building a simple program that passes a wild-card selection string and then builds a report. In the process, you'll learn how to build and store custom queries and how to run them from a command program. All you need to create and follow this tutorial is SEU.

QM/400 is a built-in facility that compiles SQL statements from source members and runs those statements against OS/400's DB2/400 relational database. It's not a program product you have to purchase; it's a built-in facility of the system. The process of using it is simple: you create a special source member containing an SQL statement, compile that source using the Create Query Management Query (CRTQMQRY) command, and then execute the query with the Start Query Management Query (STRQMQRY) command. If you compose the source member properly, you can pass selection and sorting criteria to the query from a CL program and command, making the user interface as native as possible. I'll walk you through these processes.

In this sample, I'll assume you have a CUSTOMER file composed of a NAME field and a CUST# field. I've designed the sample application so that, if you key in just a part of a customer's name, the program returns an entire list of names and their associated customer numbers.

The first thing you have to do is create a file to hold your QM/400 source program. This is a special source file that can exist in any library you choose. It's special because the SQL statement you put into it can't exceed a length of 79 characters. Since source files also require sequence numbers and other key information, the total length of a QM/400 source file should be 91 bytes. Create this source file by keying in this command:

 CRTSRCPF FILE(MYLIB/QQMQRYSRC) RCDLEN(91) 

This creates source file QQMQRYSRC in the MYLIB library. I've chosen the name QQMQRYSRC because it's the standard source file name that QM/400 expects, but you can call it anything you like.

Now what? Well, key in a sample SQL statement. Start SEU and create a new member in your QQMQRYSRC source file. Call this new member CUSTSEARCH. When you get a new blank work area, key in the following SQL statement:

 SELECT NAME, CUST# FROM MYLIB/CUSTOMER WHERE NAME LIKE &SEARCH 

The SQL code reads almost like an English paragraph. To paraphrase, the above statement reads "Select (the fields) NAME (and) CUST# from (the file) MYLIB/CUSTOMER where (the) NAME (field is) like (the) &SEARCH (field)." SQL syntax is pretty easy to read, and it's a snap to pick up.

There are some restrictions to source members compiled by QM/400. Most significantly, the SQL source member can only be a single SQL statement. This SQL statement, however, can be spread over multiple SEU lines and can be used to build sophisticated selection and sorting criteria utilizing multiple substitution parameters, each up to 55 characters in length.

Now, notice the third line of the code: WHERE NAME LIKE &SEARCH. The ampersand (&) identifies the field &SEARCH as a substitution parameter. Keep an eye on this parameter; it will go through many permutations throughout this article. This is the parameter I'll pass to this SQL program.

Also notice that I'm comparing the NAME field with a LIKE predicate, instead of the old standby equals (=) predicate. This is because I want my users to be able to search for a name by keying in a pattern of letters, instead of having to spell the name exactly as it is in the CUSTOMER file. This is sometimes called wild carding, and, in QM/400, the wild-card symbol is the percent character (%). I'll pass this wild-card character (%) to SQL and then let SQL do all the work of matching the NAME patterns.

Save this source member. It's now ready to be compiled. To compile this SQL statement into a QMQRY, you use this CRTQMQRY command:

 CRTQMQRY QMQRY(MYLIB/CUSTSEARCH) 

If the compile is successful, no error messages will be generated. Instead, a new object of the type QMQRY will be in your library.

You could theoretically execute this query interactively, but it's not much more work to create a CL program and a command for our new CUSTSEARCH QMQRY. So, crank up SEU again, this time in your CL source file, and key in the following CL statements:

 PGM (&SEARCH) DCL VAR(&SEARCH) TYPE(*CHAR) LEN(25) DCL VAR(&ARG) TYPE(*CHAR) LEN(27) CHGVAR VAR(&ARG) + VALUE('''' *TCAT &SEARCH + *TCAT '''') STRQMQRY QMQRY(CUSTSEARCH) + SETVAR((SEARCH &ARG)) ENDPGM 

The program is very simple: it receives a parameter called &SEARCH, and then it executes the STRQMQRY command. The query that it calls is the CUSTSEARCH QMQRY object.

There are only a couple of things to note in this program. Look at the CHGVAR line and notice that the &ARG parameter receives a concatenated version of the &SEARCH parameter. This line reformulates the &SEARCH string into something the CL syntax checker can accept. The newly reformulated variable &ARG is then passed on to the STRQMQRY command as the SEARCH parameter.

Save this CL program under the name CUSTSEARCH and compile it. Now, you have two objects called CUSTSEARCH: a QMQRY object and a PGM object. I'll show you how to make one more.

Crank up SEU again-this time in your QCMDSRC source file-and key the following code into a new member:

 CMD PROMPT('Search for Customer') PARM KWD(SEARCH) TYPE(*CHAR) + LEN(25) RSTD(*NO) MIN(1) + PROMPT('"Name" or + "%Partial Name%"') 

All this command does is call the CUSTSEARCH CL program, passing the SEARCH keyword. Notice that I used the MIN(1) parameter, telling the command that there must be at least one parameter passed. Doing this prevents a null string from being passed to the SQL.

Name this source member CUSTSEARCH, and then compile it. We now have the three CUSTSEARCH objects: the QMQRY object (which holds the compiled SQL statements), the PGM object (created from the CL source member that parses out the search string), and the CMD object (which is now the user interface). To see how this triad of program engineering works, key in CUSTSEARCH and press the F4 key. If you followed all the directions, you should see the screen shown in 1.

Name this source member CUSTSEARCH, and then compile it. We now have the three CUSTSEARCH objects: the QMQRY object (which holds the compiled SQL statements), the PGM object (created from the CL source member that parses out the search string), and the CMD object (which is now the user interface). To see how this triad of program engineering works, key in CUSTSEARCH and press the F4 key. If you followed all the directions, you should see the screen shown in Figure 1.

Now, if you key a name without wild-card characters, SQL attempts to use the LIKE statement to find exactly the same string in the NAME field of the CUSTOMER file. But if you key in a wild-card name, such as %Electrical%, SQL searches for any records that have the string Electrical embedded within them. SQL then returns a screen that looks like the one shown in 2.

Now, if you key a name without wild-card characters, SQL attempts to use the LIKE statement to find exactly the same string in the NAME field of the CUSTOMER file. But if you key in a wild-card name, such as %Electrical%, SQL searches for any records that have the string Electrical embedded within them. SQL then returns a screen that looks like the one shown in Figure 2.

So that's how QM/400 works. It takes a simple SQL statement recorded in a source member and compiles it into an object type of *QMQRY. You can pass parameters to the *QMQRY objects from any OS/400 program facility. The resulting output can be sent to the screen, to a printer, or to a file.

QM/400 brings an amazing level of SQL power to your system. Try it! If your shop is penny-pinching on SQL products or if you're just a simple old OS/400 minimalist like me, this might be just the ticket you need.

Thomas M. Stockwell is a senior technical editor for Midrange Computing. He can be reached by E-mail at This email address is being protected from spambots. You need JavaScript enabled to view it..

SQL Minimalism

Figure 1: CUSTSEARCH Command Prompt


SQL Minimalism

Figure 2: Query Management Search Results


Thomas Stockwell

Thomas M. Stockwell is an independent IT analyst and writer. He is the former Editor in Chief of MC Press Online and Midrange Computing magazine and has over 20 years of experience as a programmer, systems engineer, IT director, industry analyst, author, speaker, consultant, and editor.  

 

Tom works from his home in the Napa Valley in California. He can be reached at ITincendiary.com.

 

 

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: