Lesson 182 of 288
SQL on IBM iMini Project: SQLRPGLE Customer Lookup
The Customer Inquiry Program rebuilt using embedded SQL instead of native CHAIN, as a direct side-by-side comparison.
What We Are Building
The same customer inquiry task as the Customer Inquiry Program mini
project, rebuilt using embedded SQL instead of native CHAIN, as a
direct, side-by-side comparison between the two approaches.
Business Scenario
Imagine the same customer service representative's inquiry need, but a development team that has decided new lookup programs should use embedded SQL going forward. This project shows exactly what changes, and what stays the same, when moving from native file I/O to SQLRPGLE for this kind of task.
Concepts Used
SELECT INTOand host variables, from the SELECT INTO for Reading One Row and Host Variables in SQLRPGLE lessons.SQLCODE, from the SQLCODE and SQLSTATE Basics in SQLRPGLE and Handling No Row Found in SQLRPGLE lessons.- The same display file concepts as the Customer Inquiry Program mini project.
Files and Programs Involved
CUSTMAST, queried here through embedded SQL rather than nativedcl-f/CHAIN.CUSTDSPF, the same display file built in the Customer Inquiry Program mini project, reused without changes.CUSTINQS, a new RPGLE program, the SQLRPGLE counterpart toCUSTINQR.
Step-by-Step Build Outline
- Declare
CUSTDSPFwithdcl-f, exactly as before;CUSTMASTdoes not need its owndcl-f, since embedded SQL accesses it directly. - Declare host variables for the customer number, name, and balance, covered in the Host Variables in SQLRPGLE lesson.
- Loop calling
exfmt INQFMT, reading the customer number entered. - Use an embedded
SELECT INTOstatement to retrieve the name and balance for that customer number. - Check
SQLCODE:0means found, anything else means not found, setting the same error message indicator asCUSTINQR.
Example Code
dcl-f CUSTDSPF workstn;
dcl-s hCustNbr packed(6:0);
dcl-s hCustName char(30);
dcl-s hCustBal packed(9:2);
dcl-s exitRequested ind;
dow not exitRequested;
exfmt INQFMT;
if *in03;
exitRequested = *on;
else;
hCustNbr = CUSTNBR;
exec sql
select CUSTNAME, CUSTBAL into :hCustName, :hCustBal
from CUSTMAST
where CUSTNBR = :hCustNbr;
if sqlcode = 0;
CUSTNAME = hCustName;
CUSTBAL = hCustBal;
*in50 = *off;
else;
*in50 = *on;
endif;
endif;
enddo;
*inlr = *on;
Notice the overall shape, loop, exfmt, check-then-branch, is identical
to CUSTINQR from the Customer Inquiry Program mini project. What
differs is entirely inside the lookup step itself: a SELECT INTO
statement and SQLCODE check replace CHAIN and %FOUND, exactly the
correspondence covered in the Native RPGLE File I/O vs Embedded SQL
lesson.
How to Test It
Use the exact same three test cases as the Customer Inquiry Program mini
project: a customer number that exists, one that does not, and pressing
F3 immediately. The screen's behavior should look identical to CUSTINQR
from the outside, even though the lookup mechanism underneath is
completely different.
Common Mistakes
- Forgetting the colon prefix on host variables inside the
EXEC SQLblock, covered in the Host Variables in SQLRPGLE lesson. - Checking
sqlcode <> 0generically instead of specifically distinguishing100from a genuine error, covered in the Handling No Row Found in SQLRPGLE lesson, though for this simple lookup a basicsqlcode = 0check is sufficient. - Forgetting to copy the retrieved host variable values back into
CUSTDSPF's own output fields,CUSTNAMEandCUSTBAL, before the nextexfmtdisplays them.
Debugging Checklist
If the screen shows blank or stale fields for a customer known to exist,
follow the SELECT INTO for Reading One Row lesson's guidance: use EVAL
on sqlcode right after the SELECT INTO statement to confirm it
actually came back 0, and separately confirm hCustName and
hCustBal were actually copied into CUSTNAME and CUSTBAL afterward.
Possible Extensions
A natural extension is rewriting the Simple Order List Subfile mini
project's loading loop using a cursor and FETCH instead of
SETLL/READE, following the same native-versus-SQL comparison
demonstrated here.
Quick Recap
CUSTINQSrebuilds the Customer Inquiry Program mini project's exact behavior usingSELECT INTOandSQLCODEinstead ofCHAINand%FOUND.- The overall program shape stays the same; only the lookup mechanism changes.
- The same three test cases apply, and the screen should behave identically from a user's point of view.
Try Asking the AI Tutor
Use the AI Tutor to practice extending or troubleshooting this project. For example, try asking:
- "What would this program look like if it needed to distinguish SQLCODE = 100 from a genuine database error, rather than treating any nonzero value the same way?"
- "Could CUSTINQR and CUSTINQS both exist in the same application, calling the same CUSTDSPF display file?"