29
Fri, Nov
0 New Articles

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

$

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: