![Page 1: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/1.jpg)
Presentation © Copyright 2015, Enskill.com
RPG HANDS-ON LABUsing SQL, Procedures and Service programs
Jim BuckPhone 262-705-2832Email –[email protected] - @j_buck51
![Page 2: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/2.jpg)
5250 & SEU – Doesn’t work anymore!
SEU doesn’t support the latest version of RPG.
Well I guess, you could turnoff Syntax Checking!
My students have a short introduction… in case of emergencies!
![Page 3: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/3.jpg)
Rational Developer for i – 9.5.1.2
This is the FUTURE!
*** Disclaimer ***
All of the code that I show in my presentations is supported using RDi!
![Page 4: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/4.jpg)
Model View Controller (MVC)
![Page 5: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/5.jpg)
Modular Programming
• Developing code in small, independent units offers several advantages• Reusability
• Fewer errors
• Easier to test
• Changes are unlikely to cause unwanted side effects
• Easier team development
• Callp (Call a Prototyped Procedure or Program) operation• Passes control to separate *PGM object
• When called program returns, caller resumes
![Page 6: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/6.jpg)
Prototyping the Call Interface
• Variable’s scope is limited to program in which it is defined
• Callp operation passes parameters to communicate values between caller and called program• Parameters are values that one program can pass to another• Called program can then accept parameters and use those values to control
its processing
• Prototype definition defines call interface• Name of program to call• Number of parameters, data attributes, order
• Compiler uses prototype to call program correctly and to ensure caller passes correct parameters
![Page 7: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/7.jpg)
Prototyping the Call Interface
• Prototype definition has two parts• Prototype header
• Parameter descriptions
• Dcl-pr (Declare Prototype) instruction signals beginning of prototyped call interface• Coded in Declarations section
• End-pr instruction ends prototype
Dcl-pr Updcust Extpgm('AR002');Company Char(5);Customer Zoned(7:0);
End-pr;
![Page 8: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/8.jpg)
Prototyping the Call Interface
• Dcl-pr must name prototype• Callp will use this name
• Extpgm keyword names actual *PGM object to call• Can be quoted literal, named
constant or variable• Name must match called
program’s actual name• Case sensitive• Need not name program if
prototype name is same as program name
Dcl-pr Updcust Extpgm('AR002');Company Char(5);Customer Zoned(7:0);
End-pr;
Dcl-pr AR002 Extpgm;Company Char(5);Customer Zoned(7:0);
End-pr;
![Page 9: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/9.jpg)
Prototyping the Call Interface
• Subsequent lines describe parameters• Similar to data structure subfields• May list up to 255 parameters
• Parameters need not be named• Variable must be declared
elsewhere• May use *N placeholder instead• If name matches RPG reserved
word, Dcl-parm (Declare Parameter) instruction is required• Optional otherwise
Dcl-pr Updcust Extpgm('AR002');*N Char(5);*N Zoned(7:0);
End-pr;
Dcl-pr Updcust Extpgm('AR002');Dcl-parm Company Char(5);Dcl-parm Customer Zoned(7:0);
End-pr;
Dcl-pr Updcust Extpgm('AR002');Company Char(5);Customer Zoned(7:0);
End-pr;
![Page 10: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/10.jpg)
Callp (Call Prototyped Procedure or Program)
• Callp (Call Prototyped Procedure or Program) operation invokes associated *PGM object, then passes control to it• Actually coding Callp is optional
• Parameters in parentheses immediately follow prototype name• If no parameters, code empty
parentheses instead
Dcl-pr Updcust Extpgm('AR002');*N Char(5);*N Zoned(7:0);
End-pr;…Callp Updcust(Company:Custnbr);
Updcust(Company:Custnbr);
Dcl-pr Sleep Extpgm End-pr;…Sleep();
![Page 11: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/11.jpg)
Procedure Interface – Called Program
• Called RPG program should also use prototype to describe list of parameters that it is to receive from caller• Main procedure prototype
• Prototypes in each program must match each other• Same number of parameters, data attributes
• Can Should use /COPY to keep them in sync!
• Names need not be same
• Called program must include procedure interface definition• In addition to prototype
• Defines variable names to hold received parameter values
![Page 12: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/12.jpg)
Procedure Interface
• Dcl-pi (Declare Procedure Interface) instruction defines main procedure interface
• Similar to prototype definition
• Must appear after main procedure prototype
• Two parts
• Procedure interface header
• Parameter definitions to hold received parameters
Dcl-pr AR002 Extpgm;*N Char(5);*N Zoned(7:0);
End-pr;
Dcl-pi AR002;Company Char(5);Customer Zoned(7:0);
End-pi;
![Page 13: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/13.jpg)
Procedure Interface
• Main procedure interface must be named same as main procedure prototype
• May prefer to use consistent name for all programs• For example, Main
Dcl-pr AR002 Extpgm;*N Char(9);*N Zoned(7:0);
End-pr;
Dcl-pi AR002;Company Char(5);Customer Zoned(7:0);
End-pi;
Dcl-pr Main Extpgm('AR002');*N Char(9);*N Zoned(7:0);
End-pr;
Dcl-pi Main;Company Char(5);Customer Zoned(7:0);
End-pi;
![Page 14: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/14.jpg)
Procedure Interface
• Callp always requires that caller include prototype
• If no parameters, the called program doesn’t need either the prototype or procedure interface• NOTE!
• Recommended for consistency
• To promote future enhancements
![Page 15: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/15.jpg)
Passing Parameters by Reference
• Default parameter passing method
• RPG passes parameter by passing address of memory location represented by variable• Called program uses that storage address to retrieve and process the
parameter value
• Both programs share same storage• If called program changes parameter value,
• Caller recognizes change when it regains control
![Page 16: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/16.jpg)
Passing Parameters by Reference
// CallerDcl-pr Nextpgm Extpgm;
*N Ind;End-pr;
Dcl-s Okay Ind Inz(*Off);…
// Okay = *Off before callNextpgm(Okay);
// Okay = *On after call…
// NextpgmDcl-pr Nextpgm Extpgm;
*N Ind;End-pr;
Dcl-pi Nextpgm;Flag *Ind;
End-pi;…Flag = *On; // Change value of Flag*Inlr = *On;Return;
![Page 17: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/17.jpg)
Fitting the Pieces
• Caller includes two items• Prototype definition to describe parameters to pass
• Callp operation to execute call
• Called program contains three items• Main procedure prototype definition to describe parameters to receive
• Main procedure interface to accept parameters and place them into variables
• Return operation to return control to caller
• Generally, will first set *Inlr *On
![Page 18: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/18.jpg)
Introduction to Procedures• Modern RPG Program is comprised four sections
• Control options• Provides default options for program
• Declarations• Identifies and defines files, variables, and other data items
• Main procedure• Processes, calculations, and procedures
• Subprocedures• Declarations and processes for optional distinct program functions that
main procedure or other subprocedures can execute once or many times
• A Procedure is self-contained, identifiable collection of RPG statements within *PGM object• Performs specific task and returns to caller• Not system object
![Page 19: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/19.jpg)
Role of Procedures in a Program
• Like a subroutine, but with more capabilities• Subprocedure can be created independently from program
• Code in separate source member and compile into separate module, then bind it when you create program
• Subprocedure enables flexible variable scoping• Global declarations make all variables equally accessible by entire program
• Only option without subprocedures• Local variables are recognized only within procedure in which they are
defined• Data values are communicated between procedures by passing parameters
• Subprocedure supports return value• User-defined function
• Subprocedures can be recursive• Can call itself
![Page 20: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/20.jpg)
Coding a Procedure
• Dcl-proc (Declare Procedure) instruction begins procedure• End-proc instruction ends it
• Subprocedure code can include• Procedure interface
• Declarations
• Processing statements (calculations)
// ---------------------------------------------// Procedure Celsius =
Converts Fahrenheit to Celsius// ---------------------------------------------
Dcl-proc Celsius;
// ------------------------- Procedure interfaceDcl-pi *N Zoned(5:2);
Fahrenheit Zoned(5:2);End-pi;
// ----------------------------- Local variablesDcl-s Temperature Zoned(5:2);
Eval(H) Temperature = (5/9) * (Fahrenheit - 32);
Return Temperature;
End-proc Celsius;
![Page 21: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/21.jpg)
Coding a Procedure
• Need not name subprocedure interface• Use *N placeholder instead
• Dcl-proc instruction includes data attributes of return value• Can be value of any supported
data type
• Can also be data structure (using Likeds) or array (using Dim)
// ---------------------------------------------// Procedure Celsius =
Converts Fahrenheit to Celsius// ---------------------------------------------
Dcl-proc Celsius;
// ------------------------- Procedure interfaceDcl-pi *N Zoned(5:2);
Fahrenheit Zoned(5:2);End-pi;
// ----------------------------- Local variablesDcl-s Temperature Zoned(5:2);
Eval(H) Temperature = (5/9) * (Fahrenheit - 32);
Return Temperature;
End-proc Celsius;
![Page 22: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/22.jpg)
Coding a Procedure
• Declarations within subprocedure have local scope• Including variables in procedure
interface
• Restricted to subprocedure
• No other procedure will recognize local variables
• Return operation includes value to return to caller• May be variable, literal, or
expression
// ---------------------------------------------// Procedure Celsius =
Converts Fahrenheit to Celsius// ---------------------------------------------
Dcl-proc Celsius;
// ------------------------- Procedure interfaceDcl-pi *N Zoned(5:2);
Fahrenheit Zoned(5:2);End-pi;
// ----------------------------- Local variablesDcl-s Temperature Zoned(5:2);
Eval(H) Temperature = (5/9) * (Fahrenheit - 32);
Return Temperature;
End-proc Celsius;
![Page 23: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/23.jpg)
Coding a Procedure
• Subprocedure prototype is coded in global section of source
• Prototype names procedure• If procedure name is different
from prototype name, use Extprockeyword
• Prototype includes return value
// --------------------- Global Area of PgmDcl-pr Celsius Zoned(5:2);
*N Zoned(5:2);End-pr;
// -----------------------------------------// Procedure Celsius = // Converts Fahrenheit to Celsius// -----------------------------------------
Dcl-proc Celsius;
// --------------------- Procedure interfaceDcl-pi *N Zoned(5:2);
Fahrenheit Zoned(5:2);End-pi;
// ------------------------- Local variablesDcl-s Temperature Zoned(5:2);
Eval(H) Temperature = (5/9) * (Fahrenheit - 32);
Return Temperature;
End-proc Celsius;
![Page 24: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/24.jpg)
Coding a Procedure
• Locally scoped variables are allocated in automatic storage• Reinitialized each time procedure executes
• Do not retain value between iterations
• Static keyword uses static storage instead• Retains value
Dcl-s Counter Uns(5) Static;
![Page 25: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/25.jpg)
RDi and creating PR and PI’s
• RDi has some great features to help you get started with Prototypes and Interfaces.
• Most of the problems are caused by a “Piece(s)” missing.
![Page 26: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/26.jpg)
Creating PR and PI’s
![Page 27: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/27.jpg)
Executing a Procedure
• Callp operation calls procedure without return value• Or if program ignores return value
• Function-like call uses return value
Callp Updcust(Company:Custnbr);
Metrictemp = Celsius(Englishtemp);
If Celsius(Englishtemp) > 100;…Endif;
![Page 28: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/28.jpg)
Cycle Main Programs
• Most traditional RPG programs are cycle main programs
• Cycle main program has main procedure implicitly specified• Main procedure is not named
• Location in the program code designates main procedure
• Includes main source section (the main procedure), followed by zero or more subprocedure sections• Main procedure is everything before first Dcl-proc instruction
• May include local subprocedures coded following main procedure
• Compiler automatically includes RPG cycle to provide program initialization, termination, file input and output (I/O)
![Page 29: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/29.jpg)
Sample Cycle Main Program
// Control Options ================================*Ctl-Opt Option(*NoDebugIO) DftActGrp(*No);
Dcl-pr CallTemp ExtPgm;*N char(5);
End-pr;
Dcl-pi CallTemp;CharTemp char(5);
End-pi;
// ----------------------------------------- PrototypesDcl-pr Celsius Zoned(5:2);
*N Zoned(5:2);End-pr;
![Page 30: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/30.jpg)
// ------------------------------ Global variablesDcl-s Message Char(52);Dcl-s Metrictemp Zoned(5:2);Dcl-s State Varchar(8);Dcl-s EnglishTemp Zoned(5:2);// ------------------------------- Main procedure
EnglishTemp = %Dec(CharTemp:5:2);Metrictemp = Celsius(Englishtemp);
Select;When Metrictemp < 0;
State = 'solid';When Metrictemp = 0;
State = 'freezing';When Metrictemp = 100;
State = 'boiling';When Metrictemp > 100;
State = 'gaseous';Other;
State = 'liquid';Endsl;
![Page 31: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/31.jpg)
Message = 'At ' + %Char(Englishtemp) + ' degrees (' +%Char(Metrictemp) + ' Celsius), water is ' +
State + '.';Dsply Message '*REQUESTER';*Inlr = *On;Return;
// ----------------------------------------------------// Procedure Celsius Converts Fahrenheit to Celsius// ----------------------------------------------------Dcl-proc Celsius;
// ------------------------------ Procedure interfaceDcl-pi *N Zoned(5:2);
Fahrenheit Zoned(5:2);End-pi;
// ---------------------------------- Local variablesDcl-s Temperature Zoned(5:2);
Eval(H) Temperature = (5/9) * (Fahrenheit - 32);Return Temperature;
End-proc Celsius;
![Page 32: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/32.jpg)
Linear Main Programs
• Linear main program explicitly names main procedure• Ctl-opt instruction uses Main keyword to name main procedure
• Global section in linear main program does not contain any executable code• Before first Dcl-proc• Can include global declarations
• Compiler will not embed RPG cycle into linear main program
• Program implicitly initializes variables, locks data areas, and opens files when program starts
• Program does not use *Inlr to trigger automatic shutdown• Resources are not cleaned up or closed when program ends, unless program explicitly does
so
• Program is recursive
![Page 33: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/33.jpg)
Sample Linear Main ProgramCtl-Opt Option(*NoDebugIO) DftActGrp(*No);Ctl-opt Main(Driver);
// Control Options ==================================*Dcl-pr Driver ExtPgm('CALLTEMP1');
*N char(5);End-pr;
// ----------------------------------------- PrototypesDcl-pr Celsius Zoned(5:2);
*N Zoned(5:2);End-pr;// ----------------------------------------------------// Main procedure//Dcl-proc Driver;------------------------------ Procedure interfaceDcl-pi *N;
CharTemp char(5);End-pi;
![Page 34: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/34.jpg)
// ---------------------------------- Local variablesDcl-s Message Char(52);Dcl-s Metrictemp Zoned(5:2);Dcl-s State Varchar(8);Dcl-s EnglishTemp Zoned(5:2);// ------------------------------------- Main procedure
EnglishTemp = %Dec(charTemp:5:2);Metrictemp = Celsius(Englishtemp);
Select;When Metrictemp < 0;
State = 'solid';When Metrictemp = 0;
State = 'freezing';When Metrictemp = 100;
State = 'boiling';When Metrictemp > 100;
State = 'gaseous';Other;
State = 'liquid';Endsl;
![Page 35: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/35.jpg)
Message = 'At ' + %Char(Englishtemp) + ' degrees (' +%Char(Metrictemp) + ' Celsius), water is ' +
State + '.';Dsply Message '*REQUESTER';
Return;End-proc Driver;
// ---------------------------------------------------// Procedure Celsius = Converts Fahrenheit to Celsius// ---------------------------------------------------Dcl-proc Celsius;// ------------------------------ Procedure interfaceDcl-pi *N Zoned(5:2);
Fahrenheit Zoned(5:2);End-pi;
// ---------------------------------- Local variablesDcl-s Temperature Zoned(5:2);Eval(H) Temperature = (5/9) * (Fahrenheit - 32);Return Temperature;
End-proc Celsius;
![Page 36: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/36.jpg)
Nomain Modules
• Nomain modules consists of program segments without no main procedure• Source consists only of global declarations and subprocedures
• Can combine with other modules to create program
• One module in program must have main procedure
• Includes Ctl-opt instruction with Nomain keyword
• Enhance code reusability• Can help enforce business rules and practices by centralizing application functions
• Eliminate redundant code
• Improve maintainability, reliability
![Page 37: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/37.jpg)
Nomain Modules
• Dcl-proc “Export” keyword allows procedure to be called from outside module• Other modules in program can execute procedure, even though they don’t
contain the code for the procedure
• Without Export, procedure can only be executed from within module
• Most procedures in Nomain modules include Export keyword
![Page 38: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/38.jpg)
CELSIUS - Nomain Module
Ctl-opt Nomain;// Copy Block – Replaces including the Prototype
/Copy RPGTRAIN/QRPGLESRC,CALLTMPCPY
Dcl-proc Celsius Export;
// ------------------------------ Procedure interfaceDcl-pi *N Zoned(5:2);
Fahrenheit Zoned(5:2);End-pi;
// ---------------------------------- Local variablesDcl-s Temperature Zoned(5:2);
Eval(H) Temperature = (5/9) * (Fahrenheit - 32);Return Temperature;
End-proc Celsius;
![Page 39: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/39.jpg)
Creating Modular Programs
• CRTRPGMOD (Create RPG Module) command compiles source member, creates *MODULE object• Module contains compiled, executable code
• Cannot run module by itself
• Module is interim building block for eventual program object
![Page 40: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/40.jpg)
CALLTEMP2 - Linear Main Program// Control Options ===================================*Ctl-opt Main(Driver) Option(*NoDebugIO);
// Copy Block/Copy RPGTRAIN/QRPGLESRC,CALLTMPCPY// ----------------------------------------------------// Main procedure// ----------------------------------------------------Dcl-proc Driver;
// ------------------------------ Procedure interfaceDcl-pi *N;
CharTemp char(5);End-pi;// ----------------------------------- Global variablesDcl-s Message Char(52);Dcl-s Metrictemp Zoned(5:2);Dcl-s State Varchar(8);Dcl-s EnglishTemp Zoned(5:2);
![Page 41: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/41.jpg)
// ------------------------------------- Main procedureEnglishTemp = %Dec(CharTemp:5:2);Metrictemp = Celsius(Englishtemp);
Select;When Metrictemp < 0;
State = 'solid';When Metrictemp = 0;
State = 'freezing';When Metrictemp = 100;
State = 'boiling';When Metrictemp > 100;
State = 'gaseous';Other;
State = 'liquid';Endsl;
Message = 'At ' + %Char(Englishtemp) + ' degrees (' +%Char(Metrictemp) + ' Celsius), water is ' +
State + '.';Dsply Message '*REQUESTER';Return;
End-proc Driver;
![Page 42: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/42.jpg)
Creating CALLTEMP2 - Module
• CRTRPGMOD (Create RPG Module) command compiles source member, creates *MODULE object• Module contains compiled,
executable code
• Cannot run module by itself
• Module is interim building block for eventual program object
![Page 43: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/43.jpg)
Creating Modular Programs
• CRTRPGMOD (Create RPG Module) command compiles source member, creates *MODULE object• Module contains compiled, executable code
• Cannot run module by itself
• Module is interim building block for eventual program object
• CRTPGM (Create Program) command binds module to *PGM object• Bind-by-copy
• Program is runnable, using CALL command
![Page 44: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/44.jpg)
Creating Modular Programs
• CRTPGM command can combine one or more modules during binding• Modules may have been written using any ILE language
![Page 45: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/45.jpg)
Creating the Executable Program
![Page 46: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/46.jpg)
Maintaining Modular Programs• Program modification requires compile and binding steps
• UPDPGM (Update Program) command performs abbreviated binding step• Lists only module(s) to replace in original program
• Unchanged modules are unaffected
![Page 47: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/47.jpg)
Introduction to Service Programs
• Service program (*SRVPGM) is code toolbox that many programs can use• Binder need not physically copy subprocedure code into each client program• Bind-by-reference
• Service program does not have main procedure• Any subprocedure can be entry point into service program• Multiple entry point program
• Single entry point ILE program—or another service program—can invoke any exported procedure in service program• Only one copy of actual subprocedure code exists• Many other programs (clients) share
• Combine some performance advantages of bind-by-copy static binding with modularity and flexibility benefits of dynamic program call
![Page 48: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/48.jpg)
Compiling and Binding Service Programs
• Service program procedures have no unique coding requirements• Use Nomain modules• Source can have global declarations section
• Items declared in global section, before any subprocedures, are available to all procedures within module
• Includes prototype for each procedure
• Service program procedures usually use “Export” keyword to ensure availability to client programs• May hide procedure inside module by omitting “Export” keyword
• Compile source code with CRTRPGMOD command• Creates *Module object
![Page 49: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/49.jpg)
Compiling and Binding Service Programs
• We are using the “Celsius” Module that we created previously to create this service program.
• CRTSRVPGM (Create Service Program) command binds multiple entry point service program
• The actual CL command is shown in the window at right!
![Page 50: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/50.jpg)
Compiling and Binding Service Programs
• MODULE parameter lists modules to copy into *Srvpgm object• No entry module
• EXPORT allows you to reference a source member with a list of procedures to export. I have selected *ALL
• BNDSRVPGM parameter lists other service programs to bind by reference to this service program• If procedures refer to other procedures in other service programs
• BNDDIR parameter supports binding directories• Allows binder to find necessary modules or service programs not explicitly
listed with the MODUE or BNDSRVPGM parameters
• End result of successful binding step is *Srvpgm object
![Page 51: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/51.jpg)
Deploying Service Programs in an Application
• Single entry point program – or another service program – can invoke any service program procedure• Caller does not call service
program itself
• Caller calls procedure instead
Callp Updcust(Company:Custnbr);
Metrictemp = Celsius(Englishtemp);
If Celsius(Englishtemp) > 100; …Endif;
![Page 52: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/52.jpg)
Deploying Service Programs in an Application
• CRTPGM command decides whether to use bind-by-copy or bind-by-reference (service program)• CRTSRVPGM command also uses same logic
• Modules listed in MODULE parameter are bound by copy
• Service programs listed in BNDSRVPGM parameter are bound by reference
CRTPGM PGM(THISPGM) +MODULE(THISPGM) +ENTMOD(THISPGM) +BNDSRVPGM(DATSRVPGM) +BNDDIR(MYBNDDIR)
Bind-by-copy
Bind-by-reference
![Page 53: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/53.jpg)
Using RDi to create a Program
• PGM - program name will be “CELSIUSPGM”
• MODULE - “CALLTEMP2” Driver module, we created in a previous step
• BNDSVRPGM - “MYSRVPGM” the service program we created previously
• ACTGRP - *CALLER it will run in the same Activation group as the program that called “CELSIUSPGM”
• We now have a functional (runnable) program
![Page 54: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/54.jpg)
Maintaining Service Programs
• Modification to service program procedure requires compile-then-bind process to apply changes
• UPDSRVPGM (Update Service Program) command abbreviates binding step• List only changed module(s)
![Page 55: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/55.jpg)
Hands-on Lab
• Create the Display Screen• First record (INPUTDSP) allows the user to input a zoned(5:2)
• Second record (OUTPUTDSP) displays the results of the conversion
• Copy CALLTEMP2 to CALLTEMP3• Remove logic to accept a parameter
• Add logic to:• Open the display file
• Display INPUTDSP record
• Call CELSIUS Service Program to do conversion
• Display OUTPUTDSP record
• Loop until user presses F3
![Page 56: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/56.jpg)
Application Overview
Compile Steps• Create Display File CALLTEMP3D
• Compile Module CALLTEMP3
• Compile Module CELSIUS
• Create Service PGM CELSIUS
• Create Program CALLTEMP3
![Page 57: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/57.jpg)
CALLTEMP3D – Display File
![Page 58: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/58.jpg)
CALLTEMP3D – Display File Source Code
![Page 59: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/59.jpg)
CALLTEMP3 – Driver Program Source Code
![Page 60: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/60.jpg)
CALLTEMP3 – Driver Program Source Code
![Page 61: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/61.jpg)
Hands-on Lab
Refer to printed lab Document!
![Page 62: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/62.jpg)
Subfile Database ApplicationCopy Members to your Library
PRG172DSQL – Driver ProgramPRG172D - Display ScreenCUSTSRVPGM – SQL Service ProgramGETSQLDIAG – SQL Diagnostics PgmCALLTEMP3C - COPYBOOK
![Page 63: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/63.jpg)
Subfile Database Application
![Page 64: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/64.jpg)
Subfile Application Example
![Page 65: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/65.jpg)
Subfile Application – Putting it Together
• Comprised of three programs + copybook• PRG175DSQL – Main Driver Program
• Runs the 5250 screens• Handles the CREATE, READ, UPDATE and DELETE Logic
• CUSTSRVPGM – Service program that handles SQL I/O• SQL INSERT, UPDATE, SELECT and DELETE Code• Returns data Structures (Customer and SQL Status)
• GETSQLDIAG – Service program that:• Processes the GET DIAGNOSTICS command• Puts the results into a data structure• Returns this data structure to the calling program
![Page 66: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/66.jpg)
PRG175DSQL – Driver program
![Page 67: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/67.jpg)
Dynamic SQL Example
// Purpose: Used for SQL Into Statement
Dcl-ds CustomerDS Ext ExtName('CUSTOMER') Qualified Dim(9999)End-ds;
// Data Structure for SQL ResultsDcl-Ds UtilDSSQL inz;
MessageId Char(10);MessageId1 VarChar(7);MessageId2 VarChar(7);MessageLength int(5);MessageText varchar(32740);ReturnedSQLCode int(5);ReturnedSQLState char(5);RowsCount int(10);
End-Ds;
• The driver program uses an example of Dynamic SQL
![Page 68: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/68.jpg)
// ====================================================================// main loop// ====================================================================
ClearFields(); // Clear out the fieldsOpen DisplayScreen;Dow not exit;
Clear FooterDs.message;Clear CustSflDS.Opt;LoadSFL(); // Do SQL processing and load the SubfileDisplaySFL(); // Display the subfile and handle any processingIf CustCtlDS.SearchFld <> *Blanks;
SearchTerm = '%' + %Trim(CustCtlDS.SearchFld) + '%' ;EndIf;
enddo;Close DisplayScreen;*inlr = *on;return;
Main Loop
![Page 69: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/69.jpg)
// ==================================================================
// load Subfile with Records// ==================================================================
Dcl-Proc LoadSFL;ClearSFL(); // Clear out the subfileBuildSQLStmt(); // Build the SQL StatementSelectSuccess =
GetCUSTOMERRecs_DynSelect(CustomerDS:Statement:SearchTerm:UtilDSSQL);
If SelectSuccess;
for rrn = 1 to RowsCount;EVAL-CORR CustSflDS = CustomerDS(rrn);CustSflDS.nzip = %Dec(CustomerDS(rrn).czip:9:0);write DisplayScreen.CustSfl CustSflDS;reset CustSflDS;
Endfor;Else;
HandleSQLMessages();Endif;
End-Proc;
Building Dynamic SQL Statements – LoadSFL
![Page 70: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/70.jpg)
// ==================================================================
// Build Select Statement// ==================================================================Dcl-Proc BuildSQLStmt;
statement = 'SELECT * FROM CUSTOMER ';
If CustCtlDS.SearchFld <> *Blanks; // Search Field has something in it?Statement += 'WHERE ' ;Statement += 'UPPER(CFNAME) LIKE ? ';Statement += 'OR ';Statement += 'UPPER(CLNAME) LIKE ? ';Statement += 'OR ';
Building Dynamic SQL Statement – cont.
• Program must first prepare SQL statement from program data values
• Can build text string using standard RPG character processing techniques
![Page 71: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/71.jpg)
Statement += 'UPPER(CSTREET) LIKE ? ';Statement += 'OR ';Statement += 'UPPER(CCITY) LIKE ? ';Statement += 'OR ';Statement += 'UPPER(CSTATE) LIKE ? ';Statement += 'OR ';Statement += 'UPPER(CZIP) LIKE ? ';Statement += 'OR ';Statement += 'UPPER(CEMAIL) LIKE ? ';
EndIf;Statement += ' ORDER BY CLNAME, CFNAME';
Statement += ' FOR FETCH ONLY';End-Proc;
Building Dynamic SQL Statements – cont.
![Page 72: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/72.jpg)
Prepare / Declare / Open / Fetch Statements
• Prepare SQL statement - validates text string and translates it into executable SQL statement• Names prepared SQL statement
• Declare Cursor SQL Statement – names a cursor and associates it with a SQL statement
• Open SQL Statement – opens the cursor and process the Select statement embedded in the cursor making the table rows available
• Fetch SQL Statement – retrieves one of more rows from a result set in to host variables of data structures
![Page 73: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/73.jpg)
CUSTSRVPGM – SQL Database I/O
![Page 74: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/74.jpg)
CUSTSRVPGM Procedure
// Service program’s called procedure to return record set
Dcl-Proc GetCUSTOMERRecs_DynSelect Export;
Dcl-pi GetCUSTOMERRecs_DynSelect Ind;CUSTOMERDataDS LIKEDS(CUSTOMER_IODataDS) Dim(9999);Statement varchar(4096);SearchTerm VarChar(100);WrkUtilDS LikeDS(UtilDSSQL);
End-pi;Dcl-s SelectSuccess ind inz(*off);
Exec SQL prepare CustSelect from :statement;GetDiagnostics(WrkUtilDS);
Exec SQL declare CustNameCUR Scroll Cursor for CustSelect;GetDiagnostics(WrkUtilDS);
....
Procedure returns a recordset set to the caller for Subfile.
![Page 75: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/75.jpg)
CUSTSRVPGM Procedure
....
If SearchTerm <> *Blanks;
Exec SQL open CustNameCUR using:searchTerm, :searchTerm, :searchTerm, :searchTerm, :searchTerm, :searchTerm, :searchTerm;
else;Exec SQL open CustNameCUR using :statement;
EndIf;GetDiagnostics(WrkUtilDS);
....
Procedure returns a recordset set to the caller for Subfile.
![Page 76: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/76.jpg)
...Exec SQL fetch CustNameCUR for 9999 rows into :CUSTOMERDataDS;GetDiagnostics(WrkUtilDS);
If ReturnedSQLCode = 000;SelectSuccess = *on;
Else;SelectSuccess = *off;
EndIf;exec sql close CustNameCUR;
return SelectSuccess;End-Proc;
CUSTSRVPGM Procedure
Procedure returns a recordset set to the caller for Subfile.
![Page 77: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/77.jpg)
Cursor Adds, Updates and Deletes
Procedure to do ADD a record in service program
//**************************************************** // * Adds New DB2 Data For CUSTOMER//****************************************************Dcl-Proc WriteCUSTOMER_Data Export;
Dcl-Pi *N IND;CUSTOMERDataDS LIKEDS(CUSTOMER_IODataDS);WrkCustNbr Zoned(6:0);WrkUtilDS LikeDS(UtilDSSQL);
End-Pi ;
Dcl-s InsertSuccess Ind inz(*off);...
![Page 78: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/78.jpg)
Cursor Adds, Updates and Deletes
Procedure to do ADD a record in service program
...EXEC SQL
INSERT INTO CUSTOMER(CUSTNO, CFNAME, CLNAME, CSTREET, CCITY, CSTATE, CZIP, CPHONE, CALPHONE, CEMAIL, ORDDAT, BALDUE)VALUES(:CUSTOMERDataDS);
GetDiagnostics(WrkUtilDS);
If ReturnedSQLCode = 000;InsertSuccess = *on;COMMIT;
Else;InsertSuccess = *off;
EndIf;
Return InsertSuccess;
End-Proc;
![Page 79: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/79.jpg)
Cursor Adds, Updates and Deletes
Procedure to do DELETE in service program
//***************************************************
// * Delete DB2 Data For CUSTOMER//****************************************************
Dcl-Proc DeleteCUSTOMER_Data Export;
Dcl-Pi *N IND;CUSTOMERDataDS LIKEDS(CUSTOMER_IODataDS);WrkCustNbr Zoned(6:0);WrkUtilDS LikeDS(UtilDSSQL);
End-Pi ;
Dcl-s DeleteError Ind inz(*off);
...
![Page 80: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/80.jpg)
Cursor Adds, Updates and Deletes
Procedure to do DELETE in service program
...EXEC SQL
Delete from CUSTOMERwhere CUSTNO = :WrkCustNbr;
GetDiagnostics(UtilDSSQL);
If ReturnedSQLCode = 000;DeleteError = *off;COMMIT;
Else;DeleteError = *on;
EndIf;
return DeleteError;
End-Proc;
![Page 81: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/81.jpg)
Cursor Adds, Updates and Deletes
Procedure to do UPDATE in service program
//****************************************************// * Updates DB2 Data For CUSTOMER//****************************************************
Dcl-Proc UpDateCUSTOMER_Data Export;Dcl-Pi *N IND;
CUSTOMERDataDS LIKEDS(CUSTOMER_IODataDS);WrkCustNbr Zoned(6:0);WrkUtilDS LikeDS(UtilDSSQL);
End-Pi ;Dcl-s UpdateSuccess Ind inz(*off);
...
![Page 82: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/82.jpg)
Cursor Adds, Updates and Deletes
Procedure to do UPDATE in service program
...EXEC SQL UPDATE CUSTOMER
SET ROW = :CUSTOMERDataDSWHERE CUSTNO = :WrkCustNbr;
GetDiagnostics(WrkUtilDS);
If ReturnedSQLCode = 000;UpdateSuccess = *on;COMMIT;
else;UpdateSuccess = *off;
EndIf;
Return UpdateSuccess;
End-Proc ;
![Page 83: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/83.jpg)
GETSQLDIAG – Check SQL I/O Results
![Page 84: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/84.jpg)
Using GET DIAGNOSTICS Statement
Start using GET DIAGNOSTICS
• Returns additional information on the last SQL statement
• Superset of SQLCA
• Over 100 values can be returned
• SQLCA is limited in size (136 Byte)• Many of the return values are truncated
![Page 85: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/85.jpg)
Using GET DIAGNOSTICS Statement
Ctl-opt nomain Option(*NoDebugIO:*SrcStmt:*NoUnRef);
//-----------------------------------------------------//// Service Program: GETSQLLDIAG //// Procedure: Getdiagnostics //// Service program to capture and return the results //// of an SQL Statement ////-----------------------------------------------------//Dcl-Pr GetDiagnostics;*n LikeDS(UtilDSSQL);
End-Pr ;
Sample Service Program
![Page 86: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/86.jpg)
Using GET DIAGNOSTICS Statement
Dcl-Ds UtilDSSQL Qualified inz;MessageId Char(10);MessageId1 VarChar(7);MessageId2 VarChar(7);MessageLength int(5);MessageText varchar(32740);ReturnedSQLCode int(5);ReturnedSQLState char(5);RowsCount int(10);RowsReturned Zoned(31:0); ResultSetNoOfRows Zoned(31:0);
End-Ds;
Data Structure
Returns information on the last SQL statement
![Page 87: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/87.jpg)
Using GET DIAGNOSTICS Statement
DB2_MESSAGE_ID - Message ID corresponding to the MESSAGE_TEXT.DB2_MESSAGE_ID1 - CPF escape message that originally caused this error or an empty string is returned.
DB2_MESSAGE_ID2 - CPD diagnostic message that originally caused this error or empty string is returned.
MESSAGE_LENGTH - Length of the message text of the error, warning, or successful completion.MESSAGE_TEXT - - Message text of the error, warning, or successful completion. If the SQLCODE is 0 an empty string is returned.
DB2_RETURNED_SQLCODE - Contains the SQLCODE.RETURNED_SQLSTATE - Contains the SQLSTATE.DB2_SQLERRD3 - Value of SQLERRD(3) from the SQLCA.
![Page 88: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/88.jpg)
Using GET DIAGNOSTICS Statement
Dcl-proc GetDiagnostics Export;
Dcl-Pi *N;DiagUtilDS LikeDS(UtilDSSQL);
End-Pi ;
Clear DiagUtilDS;
Exec sql GET DIAGNOSTICS CONDITION 1:DiagUtilDS.MessageId = DB2_MESSAGE_ID,:DiagUtilDS.MessageId1 = DB2_MESSAGE_ID1,:DiagUtilDS.MessageId2 = DB2_MESSAGE_ID2,:DiagUtilDS.MessageLength = MESSAGE_LENGTH,:DiagUtilDS.MessageText = MESSAGE_TEXT,:DiagUtilDS.ReturnedSqlCode = DB2_RETURNED_SQLCODE,:DiagUtilDS.ReturnedSQLState = RETURNED_SQLSTATE,:DiagUtilDS.RowsCount = DB2_SQLERRD3;Return ;
End-Proc;
![Page 89: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/89.jpg)
Using GET DIAGNOSTICS Statement
Dcl-proc GetDiagnostics Export;
Dcl-Pi *N;DiagUtilDS LikeDS(UtilDSSQL);
End-Pi ;
Clear DiagUtilDS;
Exec sql GET DIAGNOSTICS CONDITION 1:DiagUtilDS.MessageId = DB2_MESSAGE_ID,:DiagUtilDS.MessageId1 = DB2_MESSAGE_ID1,:DiagUtilDS.MessageId2 = DB2_MESSAGE_ID2,:DiagUtilDS.MessageLength = MESSAGE_LENGTH,:DiagUtilDS.MessageText = MESSAGE_TEXT,:DiagUtilDS.ReturnedSqlCode = DB2_RETURNED_SQLCODE,:DiagUtilDS.ReturnedSQLState = RETURNED_SQLSTATE,:DiagUtilDS.RowsCount = DB2_SQLERRD3;Return ;
End-Proc;
![Page 90: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/90.jpg)
PRG172DCPY – Prototype Copybook
![Page 91: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/91.jpg)
Using RDi to Debug SQL Statements
• Most problems when using SQL in RPG are caused by:
• Statement being incorrectly built:• Especially with Dynamic SQL
• Run SQL Scripts– No longer just in iNav for Windows• Included in Access Client Solutions – Cool!
• Added to Rational Developer for I – Very Cool!
![Page 92: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/92.jpg)
Run SQL Scripts in RDi 9.5.1.2
![Page 93: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/93.jpg)
Debugging a Static SQL statement
![Page 94: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/94.jpg)
Debugging a Dynamic SQL statement
![Page 95: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/95.jpg)
Creating SQLRPGLE Programs
• Enter source code into source file member• Member type SQLRPGLE
• CRTSQLRPGI (Create SQL ILE RPG Object) CL command compiles source and creates program• Or module or service program
• CRTSQLRPGI uses precompiler to translate SQL portions of source code before actually compiling it• Converts SQL statements to equivalent RPG code to calls database functions
• Creates temporary source member
• Compiles (and binds) temporary source member, creating resulting object
![Page 96: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/96.jpg)
![Page 97: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/97.jpg)
Creating SQLRPGLE Modules
• Enter source code into source file member• Member type SQLRPGLE
• CRTSQLRPGI (Create SQL ILE RPG Object) CL command compiles source and creates Module
CRTSQLRPGI OBJ(RPGILEXAMP/PRG172DSQL) SRCFILE(RPGILEXAMP/QRPGSQLSRC) SRCMBR(PRG172DSQL) OBJTYPE(*MODULE) OPTION(*EVENTF) REPLACE(*YES) DBGVIEW(*SOURCE)
• When using RDi Select “Compile (Prompt)” and change OBJTYPE(*MODULE) as shown on the next slide.
![Page 98: RPG HANDS-ON LABgomitec.com/wordpress/wp-content/uploads/2017/06/... · Prototyping the Call Interface •Subsequent lines describe parameters •Similar to data structure subfields](https://reader033.vdocuments.net/reader033/viewer/2022050214/5f5fe59dc581bc25c65d085f/html5/thumbnails/98.jpg)
Creating SQLRPGLE Modules