TechTip: Some Uses for REXX

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

REXX has been around on the iSeries since, well, before my time. But rarely have I seen it utilized. However, I've found some simple uses for it that I'd like to share.

Use REXX as "Source" for Non-Source Objects

Often, iSeries applications consist of objects that have no source to define them. Instead, they're created with CL commands. Such objects may include data areas, data queues, and the like. And now, with ILE, service programs and binding directories also fall into this category.

It would be nice to see the CL commands that created these objects easily, without having to wade through the output of DSPwhatever commands. You could put those commands in CL programs, but then you'd have additional objects. This is why REXX is ideal for storing Create (CRTxxx) commands for your non-source objects and even for running the Create commands, without the need of a command line and prompting.

When you create these "source" members, you can and probably should set the source member type to reflect the object you are creating. If you do, remember that PDM Option 16 and RSE Run Procedure won't work, but you can easily place STRREXPRC in your own PDM option or RSE User Action.

Here's an example for a data area:

/* Create Data Area CSK0940A */                            
                                                       
"crtdtaara dtaara(FILES/CSK0940A) type(*char) len(1) value('0') ",
    "text('Calculations in Process')"        

Here's an example for a binding directory:

/* Binding Directory for UI00110R */           
                                               
bnddir='UI00110RB';                            
                                               
"crtbnddir" bnddir;                            
                                               
"addbnddire" bnddir "((UI00110R01 *module))";  
"addbnddire" bnddir "((UI00110R02 *module))";  
"addbnddire" bnddir "((UI00110R03 *module))";  
"addbnddire" bnddir "((UI00110R04 *module))";  
"addbnddire" bnddir "((UI00110R05 *module))";  
"addbnddire" bnddir "((UI00110R06 *module))";     

Use REXX for One-Time and Fix-It Programs

The nice thing about using REXX for one-timer solutions is that, as I said before, there is no program object. Also, you can easily mix CL and SQL in the same REXX procedure (something that cannot easily be done in CL).

Here's an example:

/* Move HHAINS errors to separate member in HHAAUDT */    
                                           
"cpyf HHAAUDT HHAAUDT tombr(HHAINS) mbropt(*add) ",       
"increl((*if TFERROR *eq 'HHAINS'))"          
                                  
address execsql execsql , 
"delete from HHAAUDT ",
"where TFERROR = 'HHAINS' with none"  
                                                   
"rgzpfm HHAAUDT keyfile(HHAAUDTL01 HHAAUDTL01)"   


And here's another:

/* Add Triggers */
    
"addlible TRIGTRACK *last"       
T = '*AFTER';                                            
P = 'HHM000';           
E = '*INSERT';                                            
"TRGTADD FILE(HHMCARE) TRGTIME("T") TRGEVENT("E") PGM("P")"; 
"TRGTADD FILE(HHMCFR)  TRGTIME("T") TRGEVENT("E") PGM("P")"; 
"TRGTADD FILE(HHMEWAL) TRGTIME("T") TRGEVENT("E") PGM("P")"; 
"TRGTADD FILE(HHMEWA)  TRGTIME("T") TRGEVENT("E") PGM("P")"; 
"TRGTADD FILE(HHMLODR) TRGTIME("T") TRGEVENT("E") PGM("P")"; 
                                                  
E = '*UPDATE';                     
"TRGTADD FILE(HHMCARE) TRGTIME("T") TRGEVENT("E") PGM("P") ", 

"TRGUPDCND(*CHANGE)"; 
"TRGTADD FILE(HHMCFR)  TRGTIME("T") TRGEVENT("E") PGM("P") ", 

"TRGUPDCND(*CHANGE)";
"TRGTADD FILE(HHMLOT)  TRGTIME("T") TRGEVENT("E") PGM("P") ", 

"TRGUPDCND(*CHANGE)"; 
"TRGTADD FILE(HHMSCD)  TRGTIME("T") TRGEVENT("E") PGM("P") ", 

"TRGUPDCND(*CHANGE)"; 
"TRGTADD FILE(HHMSTOR) TRGTIME("T") TRGEVENT("E") PGM("P") ", 

"TRGUPDCND(*CHANGE)"; 

Use REXX for Oft-Used Commands, Especially if They Are Complex

I once worked with an ODBC product that involved incredibly complex OS/400 commands. The commands were ugly. Even when these commands were put into CL programs, the commands were still ugly and hard to maintain. Using REXX allowed me to code these commands in a readable, maintainable format. Also, I did not have to recompile a CL program whenever a change was necessary.

Here's an example, using that ODBC product:

/* G5S801SP */
/********************************************************************/
/* G5S801SP - SHOWCASE - CREATE STORED PROCEDURE INTERFACE TO       */
/*            G5S801 - SALES ORDER QUERY.                           */
/* This routine used for query stored procedures.                   */
/*                                                                  */
/* Why this program?                                                */
/* - It is necessary, after writing a stored procedure program, to  */
/*   create the interface between the PC client and the stored      */
/*   procedure (a server application).  This is done using the      */
/*   Showcase CRTSP command.  The interface defines the name of     */
/*   the stored procedure known to the PC client app (the name of   */
/*   the stored procedure does not have to be the same as the SP    */
/*   program object name on the AS/400), all the parameters the PC  */
/*   must pass to the stored procedure, and the structure of the    */
/*   parameters received by the SP program.                         */
/*                                                                  */
/* - To create a stored procedure, the CRTSP command is used.       */
/*   Prompting becomes necessary especially when defining a large   */
/*   number of parameters.  Each parameter has several options.     */
/*   Working in a prompted command, especially a command as         */
/*   involved as CRTSP, can be very cumbersome.  It becomes even    */
/*   more complex in this next scenario:                            */
/*                                                                  */
/* - Maintaining an already existing stored procedure is done by    */
/*   the CHGSP command.  Working with this command in prompt mode   */
/*   is even more complex.  For example, a stored procedure program */
/*   may take a parameter as an array, each element containing a    */
/*   number of fields (G5S602, for example).  In defining the       */
/*   stored procedure, each field of each element must be defined   */
/*   as a separate parameter.  Imagine the time needed in CHGSP     */
/*   to add a field to each element.                                */
/*                                                                  */
/* - This program provides a way to store source CRTSP commands     */
/*   in such a way to create and maintain stored procedures in      */
/*   source, rather than directly through the commands.  It is      */
/*   easier and less time consuming to edit CRTSP commands in a     */
/*   source member.                                                 */
/*                                                                  */
/* Why REXX? REXX, a powerful string processing language was chosen:*/
/* - Its free format allows even easier editing of the CRTSP        */
/*   keywords than a CL source would.  This editing is facilitated  */
/*   also by REXX string operations.                                */
/* - REXX is interpreted.  Once you edit a CRTSP source, you can    */
/*   run the command through REXX without having to compile it      */
/*   first.  There are also no program objects.                     */
/* - Code can be more generic.                                      */
/* - REXX variables are variable length, meaning no wasted space    */
/*   in CRTSP source.                                               */
/*                                                                  */
/********************************************************************/
/* MODIFICATIONS:                                                   */
/* Date      Who   Project/Description.                             */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
/* 03/22/96  MDE   New REXX                                         */
/********************************************************************/

spname  = 'G5S801'       /* Name of stored procedure (called by PC) */
splib   = 'DWMICS'       /* Library of SP program                   */
pgmname = spname         /* Name of SP program                      */
pgmtype = 'COBOL'        /* Language type of SP program             */
rsltset = '*STATIC'      /* Result set type                         */

/* For each parameter in PARMLIST, the values are:                   */
/*                                                                   */
/* Field Name                                                        */
/* Field Type      - *INPUT, *OUTPUT, *INOUT, *CONSTANT              */
/* Data Type       - SQL format                                      */
/* Length of Field                                                   */
/* Decimal positns - *N for nonnumeric fields                        */
/* Value Required  - *INPUT, *INOUT only - Enter *YES or *NO         */
/* Null Capable    - Enter *YES or *NO                               */
/* Default/Constant                                                  */
/* Remarks                                                           */
/* Field Sequence  - to define number of parameters and their        */
/*                   structure to the stored procedure program       */

parmlist = ,
'(IPNUMROWS    *INPUT  *NUMERIC  4  0 *YES *NO *N *N 1)' ,
'(IPNUMLIBS    *INPUT  *NUMERIC  1  0 *YES *NO *N *N 1)' ,
'(IPLIB1       *INPUT  *CHAR    10 *N *NO  *NO *N *N 1)' ,
'(IPLIB2       *INPUT  *CHAR    10 *N *NO  *NO *N *N 1)' ,
'(IPLIB3       *INPUT  *CHAR    10 *N *NO  *NO *N *N 1)' ,
'(IPLIB4       *INPUT  *CHAR    10 *N *NO  *NO *N *N 1)' ,
'(IPLIB5       *INPUT  *CHAR    10 *N *NO  *NO *N *N 1)' ,
''

/* For each parameter in RSLTLIST, the values are:                   */
/*                                                                   */
/* Field Name                                                        */
/* Data Type       - SQL format                                      */
/* Length of Field                                                   */
/* Decimal positns - *N for nonnumeric fields                        */
/* Null Capable    - Enter *YES or *NO                               */
/* Remarks                                                           */

rsltlist = ,
'(OHDRS      *NUMERIC        8   0     *NO   *NO)' ,
'(OHCCMP     *CHAR           1  *N     *NO   *NO)' ,
'(OHCDIV     *NUMERIC        2   0     *NO   *NO)' ,
'(OHCORG     *NUMERIC        3   0     *NO   *NO)' ,
'(OHCWHS     *NUMERIC        3   0     *NO   *NO)' ,
'(OHNSO      *NUMERIC        6   0     *NO   *NO)' ,
'(OHCAC9     *CHAR           7  *N     *NO   *NO)' ,
'(OHCDES     *CHAR           3  *N     *NO   *NO)' ,
'(OLNSQU     *NUMERIC        3   0     *NO   *NO)' ,
'(OLNSQD     *NUMERIC        2   0     *NO   *NO)' ,
'(OLNSQL     *NUMERIC        2   0     *NO   *NO)' ,
'(OLQPLB     *NUMERIC        5   0     *NO   *NO)' ,
'(OHTSHP     *CHAR          30  *N     *NO   *NO)' ,
'(OHTSA1     *CHAR          30  *N     *NO   *NO)' ,
'(OHTSA2     *CHAR          30  *N     *NO   *NO)' ,
'(OHTSA3     *CHAR          30  *N     *NO   *NO)' ,
'(OHTSA4     *CHAR          30  *N     *NO   *NO)' ,
'(OHTSCI     *CHAR          28  *N     *NO   *NO)' ,
'(OHCSST     *CHAR           2  *N     *NO   *NO)' ,
'(OHCSZP     *CHAR          10  *N     *NO   *NO)' ,
'(OHTSHV     *CHAR          20  *N     *NO   *NO)' ,
'(OHCSCA     *CHAR           4  *N     *NO   *NO)' ,
'(OHNCO      *CHAR          25  *N     *NO   *NO)' ,
'(OHNPLN     *NUMERIC        1   0     *NO   *NO)' ,
'(OLCPRD     *NUMERIC        8   0     *NO   *NO)' ,
'(OLTPR      *CHAR          60  *N     *NO   *NO)' ,
'(OLCCCP     *NUMERIC        1   0     *NO   *NO)' ,
'(OLCCCD     *NUMERIC        2   0     *NO   *NO)' ,
'(OLCDC      *CHAR           1  *N     *NO   *NO)' ,
'(OLCLOT     *CHAR          15  *N     *NO   *NO)' ,
'(OLQORD     *NUMERIC        9   0     *NO   *NO)' ,
'(OLCUMO     *CHAR           1  *N     *NO   *NO)' ,
'(OLQWSH     *NUMERIC        9   2     *NO   *NO)' ,
'(CRTCCD     *CHAR          30  *N     *NO   *NO)' ,
'(BMFCLC     *CHAR           1  *N     *NO   *NO)' ,
''

/* Add SCSERVER to library list - variable RC is return code */
'ADDLIBLE LIB(SCSERVER)'
if rc <> 0 then nop

/* Delete current version of stored procedure interface */
'DLTSP SPNAME('spname') SPLIB('splib')'
if rc <> 0 then nop

/* Create the stored procedure */
'CRTSP SPNAME('spname') SPLIB('splib')' ,
      'PGMNAME('pgmname') PGMTYPE('pgmtype')' ,
      'PARMLIST('parmlist')' ,
      'RSLTSET('rsltset') RSLTLIST('rsltlist')'

if rc <> 0 then
    say 'Error in Stored Procedure creation =>' rc

What You Need to Know

You do not need to be an expert in REXX to use it for these functions. By looking at the examples above, you can see how simple it can be. You only need to know these basics:
Begin a REXX procedure with a comment. Comments in REXX are like those in CL; they're embedded between /* and */.

  • There is no need to declare variables in REXX. Simply assign a value to them.
  • You can enclose the text of your command or SQL statement in single or double quotes. I suggest double quotes, in case something in the text contains single quotes.
  • To put the contents of a variable into the command or SQL text, close the text with the quote, put in the variable, and use the quote to open any remaining text. If you do not want a space between the text and the variable contents, abut the text and the variable. If you do want the space, put a space between the text and the variable.
  • If you need to continue a command or SQL statement to the next line, type a comma at the end of the previous line. Put a space before the comma if you need a space at that point of the command or SQL statement.
  • Precede an SQL statement with address execsql execsql. Be aware of your commit level. If the file is not journaled, set the SQL commit option to *NONE, or use WITH NONE on your DML statements.
  • Statements in a simple REXX procedure may end with a semicolon. It is optional.

With all the other technologies available on the iSeries, REXX still has some uses. Avail yourself of REXX and make your life easier.

Doug Eckersley is the iSeries programmer with a premier homebuilder in Columbus. He has been programming on the iSeries for 10 years and has been in the business for 15. He is certified by IBM.

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$