Examples: RPG - Host code for ODBC stored procedures

In this example, the program, SPROC2, is called from the client as a stored procedure via ODBC. It returns data to the client from the PARTS database file.

RPG/400® (non-ILE) example:

      * THIS EXAMPLE IS WRITTEN IN RPG/400 (NON-ILE)
      *
      *  DEFINES PART AS AN INTEGER (BINARY 4.0)
      *
     I#OPTDS      DS
     I                                    B   1   40#OPT
     I#PRTDS      DS
     I                                    B   1   40#PART
     C           *ENTRY    PLIST
     C                     PARM           #OPTDS
     C                     PARM           #PRTDS
      *  COPY PART NUMBER TO RPG NATIVE VARIABLE WITH SAME
      *  ATTRIBUTES OF FIELD IN PARTS MASTER FILE (PACKED DECIMAL 5,0)
     C                     Z-ADD#PART     PART    50
     C           #OPT      CASEQ1         ONEREC
     C           #OPT      CASEQ2         ALLREC
     C                     ENDCS
     C                     SETON                     LR
     C                     RETRN
      *
      ****************************
     C           ONEREC    BEGSR
      ****************************
      * PROCESS REQUEST FOR A SINGLE RECORD.
     C/EXEC SQL DECLARE C1 CURSOR FOR
     C+   SELECT
     C+   PARTNO,
     C+   PARTDS,
     C+   PARTQY,
     C+   PARTPR,
     C+   PARTDT
     C+
     C+   FROM PARTS               -- FROM PART MASTER FILE
     C+
     C+   WHERE PARTNO  = :PART
     C+
     C+
     C+  FOR FETCH ONLY            -- READ ONLY CURSOR
     C/END-EXEC
     C*
     C/EXEC SQL
     C+  OPEN C1
     C/END-EXEC
     C*
     C/EXEC SQL
     C+  SET RESULT SETS CURSOR C1
     C/END-EXEC
     C                     ENDSR
      ****************************
     C           ALLREC    BEGSR
      ****************************
      * PROCESS REQUEST TO RETURN ALL RECORDS
     C/EXEC SQL DECLARE C2 CURSOR FOR
     C+   SELECT
     C+   PARTNO,
     C+   PARTDS,
     C+   PARTQY,
     C+   PARTPR,
     C+   PARTDT
     C+
     C+   FROM PARTS       -- FROM PART MASTER FILE
     C+
     C+
     C+  ORDER BY PARTNO   -- SORT BY PARTNO
     C+
     C+  FOR FETCH ONLY    -- READ ONLY CURSOR
     C/END-EXEC
     C*
     C/EXEC SQL
     C+  OPEN C2
     C/END-EXEC
     C*
     C/EXEC SQL
     C+  SET RESULT SETS CURSOR C2
     C/END-EXEC
     C                     ENDSR

ILE-RPG example:

      * This example is written in ILE-RPG
      *
      * Define option and part as integer
     D#opt             s             10i 0
     D#part            s             10i 0
      * Define part as packed 5/0
     Dpart             s              5p 0

     C     *entry        plist
     C                   parm                    #opt
     C     part          parm                    #part

     C     #opt          caseq     1             onerec
     C     #opt          caseq     2             allrec
     C                   endcs

     C                   eval      *inlr = *on
     C                   return
      *
      ****************************
     C     onerec        begsr
      ****************************
      * Process request for a single record.
     C/EXEC SQL DECLARE C1 CURSOR FOR
     C+   SELECT
     C+   PARTNO,
     C+   PARTDS,
     C+   PARTQY,
     C+   PARTPR,
     C+   PARTDT
     C+
     C+   FROM PARTS               -- FROM PART MASTER FILE
     C+
     C+   WHERE PARTNO  = :PART
     C+
     C+
     C+  FOR FETCH ONLY            -- READ ONLY CURSOR
     C/END-EXEC
     C*
     C/EXEC SQL
     C+  OPEN C1
     C/END-EXEC
     C*
     C/EXEC SQL
     C+  SET RESULT SETS CURSOR C1
     C/END-EXEC
     C                   endsr
      ****************************
     C     allrec        begsr
      ****************************
      * Process request to return all records
     C/EXEC SQL DECLARE C2 CURSOR FOR
     C+   SELECT
     C+   PARTNO,
     C+   PARTDS,
     C+   PARTQY,
     C+   PARTPR,
     C+   PARTDT
     C+
     C+   FROM PARTS       -- FROM PART MASTER FILE
     C+
     C+
     C+  ORDER BY PARTNO   -- SORT BY PARTNO
     C+
     C+  FOR FETCH ONLY    -- READ ONLY CURSOR
     C/END-EXEC
     C*
     C/EXEC SQL
     C+  OPEN C2
     C/END-EXEC
     C*
     C/EXEC SQL
     C+  SET RESULT SETS CURSOR C2
     C/END-EXEC
     C                   endsr