External Procedures, Triggers, and User-Defined Function on DB2 for I
Total Page:16
File Type:pdf, Size:1020Kb
Front cover External Procedures, Triggers, and User-Defined Functions on IBM DB2 for i Hernando Bedoya Fredy Cruz Daniel Lema Satid Singkorapoom Redbooks International Technical Support Organization External Procedures, Triggers, and User-Defined Functions on IBM DB2 for i April 2016 SG24-6503-03 Note: Before using this information and the product it supports, read the information in “Notices” on page ix. Fourth Edition (April 2016) This edition applies to V5R1, V5R2, and V5R3 of IBM OS/400 and V5R4 of IBM i5/OS, Program Number 5722-SS1. © Copyright International Business Machines Corporation 2001, 2016. All rights reserved. Note to U.S. Government Users Restricted Rights -- Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. Contents Notices . ix Trademarks . .x Preface . xi Authors. xii Now you can become a published author, too! . xiv Comments welcome. xiv Stay connected to IBM Redbooks . .xv Summary of changes. xvii April 2016, Fourth Edition. xvii IBM Redbooks promotions . xix Chapter 1. Introducing IBM DB2 for i . 1 1.1 An integrated relational database . 2 1.2 DB2 for i overview . 2 1.2.1 DB2 for i basics. 3 1.2.2 Stored procedures, triggers, and user-defined functions . 4 1.3 DB2 for i sample schema . 5 Chapter 2. Stored procedures, triggers, and user-defined functions for an Order Entry application. 9 2.1 Order Entry application overview . 10 2.2 Order Entry database overview. 11 2.3 Stored procedures and triggers in the Order Entry database . 16 2.3.1 Stored procedures . 16 2.3.2 Triggers. 17 2.3.3 User-defined functions . 17 Chapter 3. Stored procedures . 19 3.1 Introduction . 20 3.2 Stored procedure types. 23 3.2.1 SQL stored procedures. 24 3.2.2 External stored procedure. 24 3.3 Registering stored procedures . 25 3.3.1 CREATE PROCEDURE . 25 3.3.2 DECLARE PROCEDURE . 27 3.4 System catalog tables . 31 3.4.1 SYSROUTINES catalog . 32 3.4.2 SYSPARMS catalog . 32 3.5 Procedure signature and procedure overloading . 33 3.6 Deleting or replacing stored procedures . 33 3.6.1 Using a command line to drop a procedure . 34 3.6.2 Dropping overloaded procedures . 35 3.7 Authorization and adopted authority . 35 3.8 Returning result sets from stored procedures . 36 Chapter 4. External stored procedures . 39 4.1 Registering external stored procedures . 40 Contents iii 4.1.1 Registering an external procedure with System i Navigator . 40 4.2 Parameter styles in external stored procedures . 45 4.2.1 SQL parameter style . 46 4.2.2 DB2SQL parameter style . 47 4.2.3 GENERAL WITH NULLS parameter style . 47 4.2.4 GENERAL parameter style . 47 4.3 Coding external stored procedures . 48 4.3.1 Coding for SQL parameter style . 48 4.3.2 Coding the DB2SQL parameter style . 54 4.3.3 Coding the GENERAL WITH NULLS parameter style . 57 4.4 Returning result sets from external procedures . 60 4.4.1 Coding external stored procedures that return cursor result sets . 61 4.4.2 Coding external stored procedures that return array result sets. 67 4.5 CLI client program that calls a procedure that returns multiple result sets . 68 4.6 Moving into production (save and restore) . 73 4.7 The Order Entry application: Stored procedure examples . 74 4.7.1 Calling a stored procedure . 75 4.7.2 Sample stored procedure: SQL RPG version . 81 4.8 External stored procedure that uses a service program . 83 4.9 RPG IV example for an external stored procedure . 86 4.9.1 External stored procedure that writes to a data queue . 87 4.9.2 External stored procedure that reads from a data queue . 88 4.9.3 Calling external stored procedures from the Run SQL Scripts utility . 89 Chapter 5. Java stored procedures . 91 5.1 Prerequisites . 93 5.2 Coding DB2 for i Java stored procedures . 93 5.2.1 Parameter styles . 93 5.2.2 Data type compatibility . 96 5.2.3 Database connection in a Java stored procedure. 96 5.2.4 Returning result sets in Java stored procedures. 97 5.3 Coding examples . 99 5.3.1 Compilation of Java code . 104 5.3.2 Where to place Java classes . 106 5.3.3 Creating Java programs . 107 5.4 Registering Java stored procedures . ..