UDTFs in RPG for the Win!
by Scott Klement

-----------------------------------------------------------------------------
UPLOAD INSTRUCTIONS:
-----------------------------------------------------------------------------

This article contains a handy FTP script to help you upload the source code 
from your PC to your IBM i system.  The script is called ftpsrc.bat.  
To run it, type the following from an MS-DOS prompt:

   ftpsrc.bat HOST LIB USER PASSWORD

Where:   HOST = the TCP/IP host name or IP address of your system.
          LIB = library to upload the source code to
         USER = your userid to log on to FTP with
     PASSWORD = your password to log on to FTP with

For example, if you unzipped this code into the C:\Downloads folder on your PC, you'd open a Command Prompt (MS-DOS prompt) and type:

   C:
   cd \Downloads
   ftpsrc.bat as400.example.com qgpl scottk bigboy

This would connect to as400.example.com and log on as userid scottk, password
bigboy.  It would then upload the source code to the QGPL library on that 
system.

Note: There's also an ftpsrc.sh shell script for FreeBSD users.  (Unfortunately
      I don't know if it's compatible with other Unix-like systems such as
      Linux.)

-----------------------------------------------------------------------------
MANUAL UPLOAD INSTRUCTIONS:
-----------------------------------------------------------------------------

If you are unable to use the FTP upload script, you can also upload the source
members using any other tool that you have available.  The members are 
expected to be uploaded as follows:

PC source file        IBM i source file     source member
--------------        -------------------   -------------
ARCASH.pf        ->   your-lib/QDDSSRC      ARCASH
CREATEPROC.sql   ->   your-lib/QSQLSRC      CREATEPROC
CUSTINV.rpgle    ->   your-lib/QRPGLESRC    CUSTINV
INVCSDT.lf       ->   your-lib/QDDSSRC      INVCSDT
INVDET.pf        ->   your-lib/QDDSSRC      INVDET
INVOICE.pf       ->   your-lib/QDDSSRC      INVOICE

-----------------------------------------------------------------------------
BUILD/COMPILE INSTRUCTIONS:
-----------------------------------------------------------------------------
First, create the data files:

   CRTPF ARCASH  SRCFILE(QDDSSRC)
   CRTPF INVOICE SRCFILE(QDDSSRC)
   CRTPF INVDET  SRCFILE(QDDSSRC)
   CRTLF INVCSDT SRCFILE(QDDSSRC)

You'll probably want to put some sample data in those files as well. 
(But, I'll leave that part up to you!)

Next, compile the RPG program:

   CRTRPGMOD CUSTINV SRCFILE(QRPGLESRC) DBGVIEW(*LIST)
   CRTSRVPGM CUSTINV EXPORT(*ALL)

Finally, install it as a function by calling the following SQL statement

Create Function CustInvoices( custno   Decimal(4, 0),
                              daysback Decimal(3, 0) )
       Returns Table
       (
          inv_no     char(5),
          inv_date   date,
          inv_wgt    decimal(9, 1),
          inv_amt    decimal(9, 2),
          pay_sts    char(1)
       )
       external name 'your-lib/CUSTINV(CUSTINVOICES)'
       program type sub
       language rpgle
       parameter style db2sql
       no sql
       not deterministic
       external action
       disallow parallel
