SlideShare a Scribd company logo
1 of 10
Download to read offline
DB2 for i External Stored Procedure Example


Rational for i and Optim Studio Development Environment ............................................. 1
Load Process Flowchart (ACS_i5Load and ACSGenesis)................................................ 2
Run Stored Procedure from Optim and Rational for i packages (client)............................ 3
CPYFRMIMPF commands are submitted for batch processing on i ................................. 3
Module syb_i5_LoadFromDelimitedFile in service program ACSGENESIS ................... 4
DB2 External Stored Procedure Interface........................................................................... 6
DB2 External Stored Procedure Program ........................................................................... 7
Benefits of Stored Procedure Processing versus ODBC/JDBC statements........................ 8
  Two Types of Stored Procedures .................................................................................... 8
  Maintenance performed in one place.............................................................................. 8
Get Job Completion Status of Stored Procedure with a Select Statement.......................... 9
References......................................................................................................................... 10



Rational for i and Optim Studio Development Environment




     •     We would like to load many tables as fast as possible without locking our GUI for
           an extended timeframe.

                                                           Page 1 of 10
                                                                                                                1/3/2011 4:49:44 PM
DB2 for i External Stored Procedure Example


Load Process Flowchart (ACS_i5Load and ACSGenesis)




                              Page 2 of 10
                                                           1/3/2011 4:49:44 PM
DB2 for i External Stored Procedure Example


Run Stored Procedure from Optim and Rational for i packages (client)




CPYFRMIMPF commands are submitted for batch processing on i




                                Page 3 of 10
                                                              1/3/2011 4:49:44 PM
DB2 for i External Stored Procedure Example


Module syb_i5_LoadFromDelimitedFile in service program ACSGENESIS

   // * ==============================================================      *
   // * DB2 for i: Load from delimited files (syB) to ACS tables (db2)      *
   // * ==============================================================      *
p syb_i5_LoadFromDelimitedFile...
p                   b                    export
d syb_i5_LoadFromDelimitedFile...
d                   pi                n
d i5_Context                       32    const
d i5_Working                      128    const
d i5_Schema                       128    const
d i5_BegPos         s               5u 0 inz(0)
d i5_EndPos         s               5u 0 inz(0)
d i5_LibFil         s              20    inz(' ')
d i5_CpyFil         s            1024    inz(' ')
d exist             s               1    inz('0')
d existIndicator s                    n  based(sqlPointer)
d sqlPointer        s                 *  inz(%ADDR(exist))
d i5_LibNme         s              10    inz(' ')
  /Free
   Monitor;
   // * ==============================================================      *
   // * Check / Set import application context and working directories      *
   // * ==============================================================      *
    If isApplicationWorkingDirectoryValid( %trim(i5_Context):
                                               %trim(i5_Working) );
     i5_dirName = %trim(i5_Context) + '/' + %trim(i5_Working);
     If i5_Schema = 'QUSER';
      i5_LibNme = RtvCurLib( pgmStatus.jobName:
                                pgmStatus.userId:
                                pgmStatus.jobNumber );
     Else;
      i5_LibNme = pgmStatus.userId;
     EndIf;
     i5_dirPtr = OpenDir(%trim(i5_dirName));
     If (i5_dirPtr <>*NULL);
      DoU i5_dirEntPtr = *Null;
        i5_dirEntPtr = ReadDir( i5_dirPtr );
        Monitor;
        On-Error *All;
        EndMon;
        If i5_dirEntPtr = *Null;
         Leave;
        Else;
         i5_entryName = %str(%addr(i5_nameFull));
   // * ==============================================================      *
   // * Filter navigation entries (.) and select .data delimited files      *
   // * ==============================================================      *
         If %subst(i5_entryName: 1: 1) <> '.';
          i5_EndPos = %scan('.data':
                       %xlate(upper: lower: %trim(i5_entryName)): 1);
          If i5_EndPos <> 0;
           i5_BegPos = %scan('.': %xlate(upper: lower:
                 %subst(%trim(i5_entryName): 1: i5_EndPos - 1)): 1);


                                Page 4 of 10
                                                             1/3/2011 4:49:44 PM
DB2 for i External Stored Procedure Example

// * ============================================================== *
// * i5 internal system object name from DB2 schema catalog entries *
// * ============================================================== *
       i5_LibFil = syb_i5_getTableName(%trim(i5_LibNme):
                   %subst(%trim(i5_entryName)
                   :(i5_BegPos + 1)
                   :((i5_EndPos - i5_BegPos) - 1)));
       If i5_LibFil <> ' ';
// * ============================================================== *
// * Construct i5 CpyFrmImpF (Copy From Import File) command string *
// * ============================================================== *
        i5_CpyFil = 'CPYFRMIMPF FROMSTMF(' + ticMark +
                    %trim(i5_Context) + '/' +
                    %trim(i5_working) + '/' +
                    %trim(i5_entryName) + ticMark +
                    ') TOFILE(' + %subst(i5_LibFil: 1: 10) +
                    '/' + %subst(i5_LibFil: 11: 10) +
                    ') MBROPT(*REPLACE) RCDDLM(*CRLF' +
                    ') DTAFMT(*DLM) STRDLM(' + ticMark +
                    '"' + ticMark + ') RMVBLANK(*TRAILING' +
                    ') FLDDLM(' + ticMark + ',' + ticMark + ')';
// * ============================================================== *
// * Remove joblog messages, prepare for submit job status capture *
// * ============================================================== *
        RemoveMessage(rmv_PgmQue:
                      rmv_StkCnt:
                      rmv_MsgKey:
                      rmv_Remove:
                      error_Code);
// * ============================================================== *
// * Construct i5 SbmJob (Submit Job) command (CpyFrmImpF) queuing *
// * ============================================================== *
        i5_CmdStr = 'SBMJOB CMD(' + %trim(i5_CpyFil) +
                    ') INLLIBL(*CURRENT) JOBD(*USRPRF) JOBQ(*JOBD' +
                    ') JOB(' + %trim(%subst(i5_LibFil: 11: 10)) +
                    ') MSGQ(*LIBL/ACS_GEN500) HOLD(*YES) PRTTXT(' +
                     ticMark + %subst(%trim(i5_entryName)
                     :(i5_BegPos + 1)
                     :((i5_EndPos - i5_BegPos) - 1)) +
                     ticMark + ')';
        CallP(e) i5_RunCmd(%trim(i5_CmdStr): %len(%trim(i5_CmdStr)));
        If %Error;
         snd_Data = 'LOAD FAILURE: ' + %trim(i5_entryName) +
                    ' in ' + %trim(pgmStatus.mainProcName);
        Else;
         snd_Data = 'LOAD SUCCESS: ' + %trim(i5_entryName) +
                    ' in ' + %trim(pgmStatus.mainProcName);
// * ============================================================== *
// * Receive joblog messages prepare for submit job status capture *
// * ============================================================== *
         ReceiveMessage(MessageInfo :%size(MessageInfo)
                       :'RCVM0100' :'*' :0 :'*ANY' :MsgKey
                       :0 :'*OLD' :error_Code);
         If i5_ByteProv = 0;
          If messageInfo.common.QMHMI03 = 'CPC1221';
           i5_JobNme = %subst(messageInfo.errMsgDta: 1: 10);
           i5_UsrNme = %subst(messageInfo.errMsgDta: 11: 10);

                             Page 5 of 10
                                                          1/3/2011 4:49:44 PM
DB2 for i External Stored Procedure Example

               i5_JobNbr = %subst(messageInfo.errMsgDta: 21: 6);
               i5_UsrDta = %subst(%trim(i5_entryName)
                            :(i5_BegPos + 1)
                            :((i5_EndPos - i5_BegPos) - 1));
               If Not WrtLogSts('ACSLOAD_01': i5_TskCnt: i5_JobNme:
                                 i5_UsrNme: i5_JobNbr: i5_UsrDta);
                // Add additional error handling logic here ======>
               EndIf;
              EndIf;
             Else;
              // Add additional error handling logic here ======>
             EndIf;
            EndIf;
           EndIf;
         Else;
           snd_Data = 'LOAD WARNING: ' + %trim(i5_entryName) +
                       ' in ' + %trim(pgmStatus.mainProcName);
         EndIf;
         CallP(e) sndjobmsg(snd_Data);       // Job Log Message Entry
        EndIf;
       EndIf;
      EndDo;
      existIndicator = *ON;                  // Return SUCCESS code
     Else;
      i5_errNoPtr = errno();
      snd_Data = 'Open directory failed from ' + %trim(i5_dirName) +
                   ' in program ' + %trim(pgmStatus.mainProcName);
      CallP(e) sndjobmsg(snd_Data);          // Job Log Message Entry
     EndIf;
     closeDir(i5_dirPtr);
    EndIf;
   On-Error *All;
    snd_Data = 'Unexpected error in syb_i5_loadFromDelimitedFile()' +
                ' in program ' + %trim(pgmStatus.mainProcName);
    CallP(e) sndjobmsg(snd_Data);            // Job Log Message Entry
   EndMon;
   Return existIndicator;
  /End-Free
p syb_i5_LoadFromDelimitedFile...
p                    e


DB2 External Stored Procedure Interface

 Create Procedure i5ParallelDataLoader (
    IN Type     char( 10) )
    LANGUAGE RPGLE
    SPECIFIC ACS_I5LOAD
    NOT DETERMINISTIC
    External Name ACS_I5LOAD
    CALLED ON NULL INPUT
    PARAMETER STYLE DB2SQL;



                                Page 6 of 10
                                                             1/3/2011 4:49:44 PM
DB2 for i External Stored Procedure Example



DB2 External Stored Procedure Program

*    ================================================================== *
*    Program name...: ACS_i5LOAD                                        *
*    Purpose........: Load database tables from delimited files         *
*    Programmer.....: Thomas and Victoria Wolfe                         *
*    Create Date....: December 7, 2010                                  *
*                                                                       *
*    Maintenance Log:                                                   *
*                                                                       *
*     PGMR       SEARCH    DATE      DESC. OF CHANGE                    *
*                                                                       *
*    ================================================================== *

    /COPY qCPYSRC,ACS_H_PGM

    /COPY QPRTSRC,ACSGENESIS

d    i5_LoadType       s             10
d    i5_NullInd1       s              5i 0
d    sqlStateCode      s              5
d    functionName      s            517
d    specificName      s            128
d    errorMessage      s             70

d i5_ProdLoad          s             10      inz(' ')

d message              s             80      inz(*Blank)
d command              s           2048      inz(' ')

    * ================================================================== *
    * Common program status information data structure (SDS)             *
    * ================================================================== *

    /COPY QCPYSRC,ACS_SQLOPT

  * ================================================================== *
  * i5 DB2 Parallel Data Loader: Submit CpyFrmImpF command to batch    *
  * ================================================================== *
c       *entry        plist
c                     parm                    i5_LoadType
c                     parm                    i5_NullInd1
c                     parm                    sqlStateCode
c                     parm                    functionName
c                     parm                    specificName
c                     parm                    errorMessage
  /Free
    Monitor;                               // Global error handler
     sqlStateCode = '00000';
     If i5_NullInd1 = 0;
      i5_ProdLoad = i5_LoadType;
      Select;
        When %xlate(upper: lower: %trim(i5_ProdLoad)) = '*sybase';
         If syb_i5_LoadFromDelimitedFile(%trim(syB_Context):


                                     Page 7 of 10
                                                                  1/3/2011 4:49:44 PM
DB2 for i External Stored Procedure Example

                                           %trim(syB_dataDump):
                                           %trim(pgmStatus.UserId) );
      Else;
        sqlStateCode = '38999';
        errorMessage = 'Error occured in external stored ' +
                       'procedure. Contact support and report ' +
                       'this error message.';
      EndIf;
     Other;
      sqlStateCode = '38001';
      errorMessage = 'Invalid input load parameter ' +
                      %trim(i5_ProdLoad) + ' not processed.';
   EndSL;
  Else;
   sqlStateCode = '38001';
   errorMessage = 'Null input load parameter not' +
                   ' processed.';
  EndIf;
  On-Error *All;                         // Unmonitored Errors?
   Dump(a);
  EndMon;
  *inlr = *On;
  Return;
/End-Free


Benefits of Stored Procedure Processing versus ODBC/JDBC statements
  • Secure access to DB2 data.
  • Access non-DB2 data if necessary –VSAM, IMS, etc.
  • Code business logic once
       o Maintenance is simpler
  • Performance improvement?
       o Instead of multiple SQL statements being sent
  • Lots of languages to choose from
Two Types of Stored Procedures

  • SQL Stored Procedures
  • External Procedures
Maintenance performed in one place




                                 Page 8 of 10
                                                                1/3/2011 4:49:44 PM
DB2 for i External Stored Procedure Example



Get Job Completion Status of Stored Procedure with a Select Statement

Select PROCESS,
       PRCSEQ,
       Cast(Case
        When STATUS = 'N'
        Then 'Success'
        When STATUS = ‘A’
        Then ‘Failure’
        Else 'Running’
       End As Character(7)) As Status,
       STARTSTMP,
       ENDSTMP,
       RUNNAME,
       RUNUSER,
       RUNNUMBER,
       ERRORID,
       USRDATA
  From ACSJOBLOG
  Order by 4,1,2




                                Page 9 of 10
                                                             1/3/2011 4:49:44 PM
DB2 for i External Stored Procedure Example



References

Writing Stored Procedures in RPG MC Press Online 15 January 2008, Susan Gantner
http://www.mcpressonline.com/programming/rpg/writing-stored-procedures-in-rpg.html

Stored Procedures, Triggers, and User-Defined Functions on DB2 UDB for iSeries
http://www.redbooks.ibm.com/abstracts/sg246503.html

DB2 UDB for AS/400 Object Relational Support
http://www.redbooks.ibm.com/abstracts/sg245409.html

Cross-Platform DB2 Stored Procedures: Building and Debugging
http://www.redbooks.ibm.com/redbooks/pdfs/sg245485.pdf

DB2 9 for z/OS Stored Procedures: Through the Call and Beyond
http://www.redbooks.ibm.com/abstracts/sg247604.html

DB2 Java Stored Procedures: Learning by Example
http://www.redbooks.ibm.com/redbooks/pdfs/sg245945.pdf

Modernizing IBM eServer iSeries Application Data Access – A Roadmap Cornerstone
http://www.redbooks.ibm.com/abstracts/sg246393.html

Improving Stored Procedure Performance 18 December 2009, Kent Milligan, Jarek
Miszczyk, and Gene Cobb http://tinyurl.com/2ayskqo

The API Corner MC Press online: Automating Recovery (or Keeping the Help Desk Out
of the Loop) 21 July 2010 – October 2010, Bruce Vining http://tinyurl.com/32gkc9h




                                    Page 10 of 10
                                                                       1/3/2011 4:49:44 PM

More Related Content

What's hot

How Data Flow analysis works in a static code analyzer
How Data Flow analysis works in a static code analyzerHow Data Flow analysis works in a static code analyzer
How Data Flow analysis works in a static code analyzerAndrey Karpov
 
Dsa lab manual version 2
Dsa lab manual version 2Dsa lab manual version 2
Dsa lab manual version 2Dwight Sabio
 
Implementation of the ZigBee ZCL Reporting Configuration Features
Implementation of the ZigBee ZCL Reporting Configuration FeaturesImplementation of the ZigBee ZCL Reporting Configuration Features
Implementation of the ZigBee ZCL Reporting Configuration FeaturesSimen Li
 
Global Interpreter Lock: Episode I - Break the Seal
Global Interpreter Lock: Episode I - Break the SealGlobal Interpreter Lock: Episode I - Break the Seal
Global Interpreter Lock: Episode I - Break the SealTzung-Bi Shih
 
深入淺出C語言
深入淺出C語言深入淺出C語言
深入淺出C語言Simen Li
 
JVM code reading -- C2
JVM code reading -- C2JVM code reading -- C2
JVM code reading -- C2ytoshima
 
HKG15-207: Advanced Toolchain Usage Part 3
HKG15-207: Advanced Toolchain Usage Part 3HKG15-207: Advanced Toolchain Usage Part 3
HKG15-207: Advanced Toolchain Usage Part 3Linaro
 
[嵌入式系統] MCS-51 實驗 - 使用 IAR (3)
[嵌入式系統] MCS-51 實驗 - 使用 IAR (3)[嵌入式系統] MCS-51 實驗 - 使用 IAR (3)
[嵌入式系統] MCS-51 實驗 - 使用 IAR (3)Simen Li
 
Deterministic simulation testing
Deterministic simulation testingDeterministic simulation testing
Deterministic simulation testingFoundationDB
 
Debugging of (C)Python applications
Debugging of (C)Python applicationsDebugging of (C)Python applications
Debugging of (C)Python applicationsRoman Podoliaka
 
PHP Performance SfLive 2010
PHP Performance SfLive 2010PHP Performance SfLive 2010
PHP Performance SfLive 2010De Cock Xavier
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYMalikireddy Bramhananda Reddy
 
Windbg랑 친해지기
Windbg랑 친해지기Windbg랑 친해지기
Windbg랑 친해지기Ji Hun Kim
 
The Ring programming language version 1.2 book - Part 57 of 84
The Ring programming language version 1.2 book - Part 57 of 84The Ring programming language version 1.2 book - Part 57 of 84
The Ring programming language version 1.2 book - Part 57 of 84Mahmoud Samir Fayed
 
Debugging: Rules & Tools
Debugging: Rules & ToolsDebugging: Rules & Tools
Debugging: Rules & ToolsIan Barber
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++Dendi Riadi
 

What's hot (20)

How Data Flow analysis works in a static code analyzer
How Data Flow analysis works in a static code analyzerHow Data Flow analysis works in a static code analyzer
How Data Flow analysis works in a static code analyzer
 
Dsa lab manual version 2
Dsa lab manual version 2Dsa lab manual version 2
Dsa lab manual version 2
 
Implementation of the ZigBee ZCL Reporting Configuration Features
Implementation of the ZigBee ZCL Reporting Configuration FeaturesImplementation of the ZigBee ZCL Reporting Configuration Features
Implementation of the ZigBee ZCL Reporting Configuration Features
 
Global Interpreter Lock: Episode I - Break the Seal
Global Interpreter Lock: Episode I - Break the SealGlobal Interpreter Lock: Episode I - Break the Seal
Global Interpreter Lock: Episode I - Break the Seal
 
深入淺出C語言
深入淺出C語言深入淺出C語言
深入淺出C語言
 
JVM code reading -- C2
JVM code reading -- C2JVM code reading -- C2
JVM code reading -- C2
 
HKG15-207: Advanced Toolchain Usage Part 3
HKG15-207: Advanced Toolchain Usage Part 3HKG15-207: Advanced Toolchain Usage Part 3
HKG15-207: Advanced Toolchain Usage Part 3
 
Joel Falcou, Boost.SIMD
Joel Falcou, Boost.SIMDJoel Falcou, Boost.SIMD
Joel Falcou, Boost.SIMD
 
[嵌入式系統] MCS-51 實驗 - 使用 IAR (3)
[嵌入式系統] MCS-51 實驗 - 使用 IAR (3)[嵌入式系統] MCS-51 實驗 - 使用 IAR (3)
[嵌入式系統] MCS-51 實驗 - 使用 IAR (3)
 
Synthesis report
Synthesis reportSynthesis report
Synthesis report
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 
Deterministic simulation testing
Deterministic simulation testingDeterministic simulation testing
Deterministic simulation testing
 
Debugging of (C)Python applications
Debugging of (C)Python applicationsDebugging of (C)Python applications
Debugging of (C)Python applications
 
PHP Performance SfLive 2010
PHP Performance SfLive 2010PHP Performance SfLive 2010
PHP Performance SfLive 2010
 
C&cpu
C&cpuC&cpu
C&cpu
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
 
Windbg랑 친해지기
Windbg랑 친해지기Windbg랑 친해지기
Windbg랑 친해지기
 
The Ring programming language version 1.2 book - Part 57 of 84
The Ring programming language version 1.2 book - Part 57 of 84The Ring programming language version 1.2 book - Part 57 of 84
The Ring programming language version 1.2 book - Part 57 of 84
 
Debugging: Rules & Tools
Debugging: Rules & ToolsDebugging: Rules & Tools
Debugging: Rules & Tools
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++
 

Similar to DB2 for i External Stored Procedure

2.1 ### uVision Project, (C) Keil Software .docx
2.1   ### uVision Project, (C) Keil Software    .docx2.1   ### uVision Project, (C) Keil Software    .docx
2.1 ### uVision Project, (C) Keil Software .docxtarifarmarie
 
How to Design a Great API (using flask) [ploneconf2017]
How to Design a Great API (using flask) [ploneconf2017]How to Design a Great API (using flask) [ploneconf2017]
How to Design a Great API (using flask) [ploneconf2017]Devon Bernard
 
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bfinalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bChereCheek752
 
This is my code- #include -llvm-IR-LegacyPassManager-h- #include -llv.pdf
This is my code-  #include -llvm-IR-LegacyPassManager-h- #include -llv.pdfThis is my code-  #include -llvm-IR-LegacyPassManager-h- #include -llv.pdf
This is my code- #include -llvm-IR-LegacyPassManager-h- #include -llv.pdfEricvtJFraserr
 
Part II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationPart II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationWei-Ren Chen
 
Workshop 1: Good practices in JavaScript
Workshop 1: Good practices in JavaScriptWorkshop 1: Good practices in JavaScript
Workshop 1: Good practices in JavaScriptVisual Engineering
 
Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019julien pauli
 
Bristol 2009 q1_wright_steve
Bristol 2009 q1_wright_steveBristol 2009 q1_wright_steve
Bristol 2009 q1_wright_steveObsidian Software
 
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdfHow do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdffootstatus
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudAndrea Righi
 
[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack Firmware[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack FirmwareSimen Li
 
PHP CLI: A Cinderella Story
PHP CLI: A Cinderella StoryPHP CLI: A Cinderella Story
PHP CLI: A Cinderella StoryMike Lively
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2goMoriyoshi Koizumi
 
Unit 6
Unit 6Unit 6
Unit 6siddr
 
Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionIan Barber
 
Being functional in PHP (PHPDay Italy 2016)
Being functional in PHP (PHPDay Italy 2016)Being functional in PHP (PHPDay Italy 2016)
Being functional in PHP (PHPDay Italy 2016)David de Boer
 
리눅스 드라이버 실습 #3
리눅스 드라이버 실습 #3리눅스 드라이버 실습 #3
리눅스 드라이버 실습 #3Sangho Park
 
MySQL Audit using Percona audit plugin and ELK
MySQL Audit using Percona audit plugin and ELKMySQL Audit using Percona audit plugin and ELK
MySQL Audit using Percona audit plugin and ELKYoungHeon (Roy) Kim
 
Sydney Oracle Meetup - execution plans
Sydney Oracle Meetup - execution plansSydney Oracle Meetup - execution plans
Sydney Oracle Meetup - execution planspaulguerin
 
Lab8.classpathLab8.project Lab8 .docx
Lab8.classpathLab8.project  Lab8   .docxLab8.classpathLab8.project  Lab8   .docx
Lab8.classpathLab8.project Lab8 .docxDIPESH30
 

Similar to DB2 for i External Stored Procedure (20)

2.1 ### uVision Project, (C) Keil Software .docx
2.1   ### uVision Project, (C) Keil Software    .docx2.1   ### uVision Project, (C) Keil Software    .docx
2.1 ### uVision Project, (C) Keil Software .docx
 
How to Design a Great API (using flask) [ploneconf2017]
How to Design a Great API (using flask) [ploneconf2017]How to Design a Great API (using flask) [ploneconf2017]
How to Design a Great API (using flask) [ploneconf2017]
 
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bfinalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
 
This is my code- #include -llvm-IR-LegacyPassManager-h- #include -llv.pdf
This is my code-  #include -llvm-IR-LegacyPassManager-h- #include -llv.pdfThis is my code-  #include -llvm-IR-LegacyPassManager-h- #include -llv.pdf
This is my code- #include -llvm-IR-LegacyPassManager-h- #include -llv.pdf
 
Part II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationPart II: LLVM Intermediate Representation
Part II: LLVM Intermediate Representation
 
Workshop 1: Good practices in JavaScript
Workshop 1: Good practices in JavaScriptWorkshop 1: Good practices in JavaScript
Workshop 1: Good practices in JavaScript
 
Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019
 
Bristol 2009 q1_wright_steve
Bristol 2009 q1_wright_steveBristol 2009 q1_wright_steve
Bristol 2009 q1_wright_steve
 
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdfHow do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloud
 
[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack Firmware[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack Firmware
 
PHP CLI: A Cinderella Story
PHP CLI: A Cinderella StoryPHP CLI: A Cinderella Story
PHP CLI: A Cinderella Story
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
 
Unit 6
Unit 6Unit 6
Unit 6
 
Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 Version
 
Being functional in PHP (PHPDay Italy 2016)
Being functional in PHP (PHPDay Italy 2016)Being functional in PHP (PHPDay Italy 2016)
Being functional in PHP (PHPDay Italy 2016)
 
리눅스 드라이버 실습 #3
리눅스 드라이버 실습 #3리눅스 드라이버 실습 #3
리눅스 드라이버 실습 #3
 
MySQL Audit using Percona audit plugin and ELK
MySQL Audit using Percona audit plugin and ELKMySQL Audit using Percona audit plugin and ELK
MySQL Audit using Percona audit plugin and ELK
 
Sydney Oracle Meetup - execution plans
Sydney Oracle Meetup - execution plansSydney Oracle Meetup - execution plans
Sydney Oracle Meetup - execution plans
 
Lab8.classpathLab8.project Lab8 .docx
Lab8.classpathLab8.project  Lab8   .docxLab8.classpathLab8.project  Lab8   .docx
Lab8.classpathLab8.project Lab8 .docx
 

DB2 for i External Stored Procedure

  • 1. DB2 for i External Stored Procedure Example Rational for i and Optim Studio Development Environment ............................................. 1 Load Process Flowchart (ACS_i5Load and ACSGenesis)................................................ 2 Run Stored Procedure from Optim and Rational for i packages (client)............................ 3 CPYFRMIMPF commands are submitted for batch processing on i ................................. 3 Module syb_i5_LoadFromDelimitedFile in service program ACSGENESIS ................... 4 DB2 External Stored Procedure Interface........................................................................... 6 DB2 External Stored Procedure Program ........................................................................... 7 Benefits of Stored Procedure Processing versus ODBC/JDBC statements........................ 8 Two Types of Stored Procedures .................................................................................... 8 Maintenance performed in one place.............................................................................. 8 Get Job Completion Status of Stored Procedure with a Select Statement.......................... 9 References......................................................................................................................... 10 Rational for i and Optim Studio Development Environment • We would like to load many tables as fast as possible without locking our GUI for an extended timeframe. Page 1 of 10 1/3/2011 4:49:44 PM
  • 2. DB2 for i External Stored Procedure Example Load Process Flowchart (ACS_i5Load and ACSGenesis) Page 2 of 10 1/3/2011 4:49:44 PM
  • 3. DB2 for i External Stored Procedure Example Run Stored Procedure from Optim and Rational for i packages (client) CPYFRMIMPF commands are submitted for batch processing on i Page 3 of 10 1/3/2011 4:49:44 PM
  • 4. DB2 for i External Stored Procedure Example Module syb_i5_LoadFromDelimitedFile in service program ACSGENESIS // * ============================================================== * // * DB2 for i: Load from delimited files (syB) to ACS tables (db2) * // * ============================================================== * p syb_i5_LoadFromDelimitedFile... p b export d syb_i5_LoadFromDelimitedFile... d pi n d i5_Context 32 const d i5_Working 128 const d i5_Schema 128 const d i5_BegPos s 5u 0 inz(0) d i5_EndPos s 5u 0 inz(0) d i5_LibFil s 20 inz(' ') d i5_CpyFil s 1024 inz(' ') d exist s 1 inz('0') d existIndicator s n based(sqlPointer) d sqlPointer s * inz(%ADDR(exist)) d i5_LibNme s 10 inz(' ') /Free Monitor; // * ============================================================== * // * Check / Set import application context and working directories * // * ============================================================== * If isApplicationWorkingDirectoryValid( %trim(i5_Context): %trim(i5_Working) ); i5_dirName = %trim(i5_Context) + '/' + %trim(i5_Working); If i5_Schema = 'QUSER'; i5_LibNme = RtvCurLib( pgmStatus.jobName: pgmStatus.userId: pgmStatus.jobNumber ); Else; i5_LibNme = pgmStatus.userId; EndIf; i5_dirPtr = OpenDir(%trim(i5_dirName)); If (i5_dirPtr <>*NULL); DoU i5_dirEntPtr = *Null; i5_dirEntPtr = ReadDir( i5_dirPtr ); Monitor; On-Error *All; EndMon; If i5_dirEntPtr = *Null; Leave; Else; i5_entryName = %str(%addr(i5_nameFull)); // * ============================================================== * // * Filter navigation entries (.) and select .data delimited files * // * ============================================================== * If %subst(i5_entryName: 1: 1) <> '.'; i5_EndPos = %scan('.data': %xlate(upper: lower: %trim(i5_entryName)): 1); If i5_EndPos <> 0; i5_BegPos = %scan('.': %xlate(upper: lower: %subst(%trim(i5_entryName): 1: i5_EndPos - 1)): 1); Page 4 of 10 1/3/2011 4:49:44 PM
  • 5. DB2 for i External Stored Procedure Example // * ============================================================== * // * i5 internal system object name from DB2 schema catalog entries * // * ============================================================== * i5_LibFil = syb_i5_getTableName(%trim(i5_LibNme): %subst(%trim(i5_entryName) :(i5_BegPos + 1) :((i5_EndPos - i5_BegPos) - 1))); If i5_LibFil <> ' '; // * ============================================================== * // * Construct i5 CpyFrmImpF (Copy From Import File) command string * // * ============================================================== * i5_CpyFil = 'CPYFRMIMPF FROMSTMF(' + ticMark + %trim(i5_Context) + '/' + %trim(i5_working) + '/' + %trim(i5_entryName) + ticMark + ') TOFILE(' + %subst(i5_LibFil: 1: 10) + '/' + %subst(i5_LibFil: 11: 10) + ') MBROPT(*REPLACE) RCDDLM(*CRLF' + ') DTAFMT(*DLM) STRDLM(' + ticMark + '"' + ticMark + ') RMVBLANK(*TRAILING' + ') FLDDLM(' + ticMark + ',' + ticMark + ')'; // * ============================================================== * // * Remove joblog messages, prepare for submit job status capture * // * ============================================================== * RemoveMessage(rmv_PgmQue: rmv_StkCnt: rmv_MsgKey: rmv_Remove: error_Code); // * ============================================================== * // * Construct i5 SbmJob (Submit Job) command (CpyFrmImpF) queuing * // * ============================================================== * i5_CmdStr = 'SBMJOB CMD(' + %trim(i5_CpyFil) + ') INLLIBL(*CURRENT) JOBD(*USRPRF) JOBQ(*JOBD' + ') JOB(' + %trim(%subst(i5_LibFil: 11: 10)) + ') MSGQ(*LIBL/ACS_GEN500) HOLD(*YES) PRTTXT(' + ticMark + %subst(%trim(i5_entryName) :(i5_BegPos + 1) :((i5_EndPos - i5_BegPos) - 1)) + ticMark + ')'; CallP(e) i5_RunCmd(%trim(i5_CmdStr): %len(%trim(i5_CmdStr))); If %Error; snd_Data = 'LOAD FAILURE: ' + %trim(i5_entryName) + ' in ' + %trim(pgmStatus.mainProcName); Else; snd_Data = 'LOAD SUCCESS: ' + %trim(i5_entryName) + ' in ' + %trim(pgmStatus.mainProcName); // * ============================================================== * // * Receive joblog messages prepare for submit job status capture * // * ============================================================== * ReceiveMessage(MessageInfo :%size(MessageInfo) :'RCVM0100' :'*' :0 :'*ANY' :MsgKey :0 :'*OLD' :error_Code); If i5_ByteProv = 0; If messageInfo.common.QMHMI03 = 'CPC1221'; i5_JobNme = %subst(messageInfo.errMsgDta: 1: 10); i5_UsrNme = %subst(messageInfo.errMsgDta: 11: 10); Page 5 of 10 1/3/2011 4:49:44 PM
  • 6. DB2 for i External Stored Procedure Example i5_JobNbr = %subst(messageInfo.errMsgDta: 21: 6); i5_UsrDta = %subst(%trim(i5_entryName) :(i5_BegPos + 1) :((i5_EndPos - i5_BegPos) - 1)); If Not WrtLogSts('ACSLOAD_01': i5_TskCnt: i5_JobNme: i5_UsrNme: i5_JobNbr: i5_UsrDta); // Add additional error handling logic here ======> EndIf; EndIf; Else; // Add additional error handling logic here ======> EndIf; EndIf; EndIf; Else; snd_Data = 'LOAD WARNING: ' + %trim(i5_entryName) + ' in ' + %trim(pgmStatus.mainProcName); EndIf; CallP(e) sndjobmsg(snd_Data); // Job Log Message Entry EndIf; EndIf; EndDo; existIndicator = *ON; // Return SUCCESS code Else; i5_errNoPtr = errno(); snd_Data = 'Open directory failed from ' + %trim(i5_dirName) + ' in program ' + %trim(pgmStatus.mainProcName); CallP(e) sndjobmsg(snd_Data); // Job Log Message Entry EndIf; closeDir(i5_dirPtr); EndIf; On-Error *All; snd_Data = 'Unexpected error in syb_i5_loadFromDelimitedFile()' + ' in program ' + %trim(pgmStatus.mainProcName); CallP(e) sndjobmsg(snd_Data); // Job Log Message Entry EndMon; Return existIndicator; /End-Free p syb_i5_LoadFromDelimitedFile... p e DB2 External Stored Procedure Interface Create Procedure i5ParallelDataLoader ( IN Type char( 10) ) LANGUAGE RPGLE SPECIFIC ACS_I5LOAD NOT DETERMINISTIC External Name ACS_I5LOAD CALLED ON NULL INPUT PARAMETER STYLE DB2SQL; Page 6 of 10 1/3/2011 4:49:44 PM
  • 7. DB2 for i External Stored Procedure Example DB2 External Stored Procedure Program * ================================================================== * * Program name...: ACS_i5LOAD * * Purpose........: Load database tables from delimited files * * Programmer.....: Thomas and Victoria Wolfe * * Create Date....: December 7, 2010 * * * * Maintenance Log: * * * * PGMR SEARCH DATE DESC. OF CHANGE * * * * ================================================================== * /COPY qCPYSRC,ACS_H_PGM /COPY QPRTSRC,ACSGENESIS d i5_LoadType s 10 d i5_NullInd1 s 5i 0 d sqlStateCode s 5 d functionName s 517 d specificName s 128 d errorMessage s 70 d i5_ProdLoad s 10 inz(' ') d message s 80 inz(*Blank) d command s 2048 inz(' ') * ================================================================== * * Common program status information data structure (SDS) * * ================================================================== * /COPY QCPYSRC,ACS_SQLOPT * ================================================================== * * i5 DB2 Parallel Data Loader: Submit CpyFrmImpF command to batch * * ================================================================== * c *entry plist c parm i5_LoadType c parm i5_NullInd1 c parm sqlStateCode c parm functionName c parm specificName c parm errorMessage /Free Monitor; // Global error handler sqlStateCode = '00000'; If i5_NullInd1 = 0; i5_ProdLoad = i5_LoadType; Select; When %xlate(upper: lower: %trim(i5_ProdLoad)) = '*sybase'; If syb_i5_LoadFromDelimitedFile(%trim(syB_Context): Page 7 of 10 1/3/2011 4:49:44 PM
  • 8. DB2 for i External Stored Procedure Example %trim(syB_dataDump): %trim(pgmStatus.UserId) ); Else; sqlStateCode = '38999'; errorMessage = 'Error occured in external stored ' + 'procedure. Contact support and report ' + 'this error message.'; EndIf; Other; sqlStateCode = '38001'; errorMessage = 'Invalid input load parameter ' + %trim(i5_ProdLoad) + ' not processed.'; EndSL; Else; sqlStateCode = '38001'; errorMessage = 'Null input load parameter not' + ' processed.'; EndIf; On-Error *All; // Unmonitored Errors? Dump(a); EndMon; *inlr = *On; Return; /End-Free Benefits of Stored Procedure Processing versus ODBC/JDBC statements • Secure access to DB2 data. • Access non-DB2 data if necessary –VSAM, IMS, etc. • Code business logic once o Maintenance is simpler • Performance improvement? o Instead of multiple SQL statements being sent • Lots of languages to choose from Two Types of Stored Procedures • SQL Stored Procedures • External Procedures Maintenance performed in one place Page 8 of 10 1/3/2011 4:49:44 PM
  • 9. DB2 for i External Stored Procedure Example Get Job Completion Status of Stored Procedure with a Select Statement Select PROCESS, PRCSEQ, Cast(Case When STATUS = 'N' Then 'Success' When STATUS = ‘A’ Then ‘Failure’ Else 'Running’ End As Character(7)) As Status, STARTSTMP, ENDSTMP, RUNNAME, RUNUSER, RUNNUMBER, ERRORID, USRDATA From ACSJOBLOG Order by 4,1,2 Page 9 of 10 1/3/2011 4:49:44 PM
  • 10. DB2 for i External Stored Procedure Example References Writing Stored Procedures in RPG MC Press Online 15 January 2008, Susan Gantner http://www.mcpressonline.com/programming/rpg/writing-stored-procedures-in-rpg.html Stored Procedures, Triggers, and User-Defined Functions on DB2 UDB for iSeries http://www.redbooks.ibm.com/abstracts/sg246503.html DB2 UDB for AS/400 Object Relational Support http://www.redbooks.ibm.com/abstracts/sg245409.html Cross-Platform DB2 Stored Procedures: Building and Debugging http://www.redbooks.ibm.com/redbooks/pdfs/sg245485.pdf DB2 9 for z/OS Stored Procedures: Through the Call and Beyond http://www.redbooks.ibm.com/abstracts/sg247604.html DB2 Java Stored Procedures: Learning by Example http://www.redbooks.ibm.com/redbooks/pdfs/sg245945.pdf Modernizing IBM eServer iSeries Application Data Access – A Roadmap Cornerstone http://www.redbooks.ibm.com/abstracts/sg246393.html Improving Stored Procedure Performance 18 December 2009, Kent Milligan, Jarek Miszczyk, and Gene Cobb http://tinyurl.com/2ayskqo The API Corner MC Press online: Automating Recovery (or Keeping the Help Desk Out of the Loop) 21 July 2010 – October 2010, Bruce Vining http://tinyurl.com/32gkc9h Page 10 of 10 1/3/2011 4:49:44 PM