Stored Procedures, Triggers, and User-Defined Functions on DB2 Universal Database for Iseries
Total Page:16
File Type:pdf, Size:1020Kb
Stored Procedures, Triggers, and User-Defined Functions on DB2 Universal Database for iSeries Develop robust DB2 Universal Database for iSeries applications Discover the details of SQL stored procedures and SQL triggers Learn the secrets of user-defined functions Hernando Bedoya Fredy Cruz Daniel Lema Satid Singkorapoom ibm.com/redbooks International Technical Support Organization Stored Procedures, Triggers, and User-Defined Functions on DB2 Universal Database for iSeries October 2006 SG24-6503-02 Take Note! Before using this information and the product it supports, be sure to read the general information in “Notices” on page xi. Third Edition (October 2006) 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, 2004, 2006. All rights reserved. Note to U.S Government Users - Documentation related to restricted rights - Use, duplication or disclosure is subject to restrictions set forth in GSA ADP Schedule Contract with IBM Corp. Contents Notices . xi Trademarks . xii Preface . xiii The team that wrote this redbook. xiv Become a published author . xvi Comments welcome. xvi Part 1. Background . 1 Chapter 1. Introducing DB2 Universal Database for iSeries . 3 1.1 An integrated relational database . 4 1.2 DB2 Universal Database for iSeries: An overview . 4 1.2.1 DB2 Universal Database for iSeries basics . 5 1.2.2 Stored procedures, triggers, and user-defined functions . 6 1.3 DB2 Universal Database for iSeries sample schema . 7 Chapter 2. Stored procedures, triggers, and user-defined functions: Order entry application. 11 2.1 Order Entry application overview . 12 2.2 Order Entry database overview. 13 2.3 Stored procedures and triggers in the Order Entry database . 17 2.3.1 Stored procedures . 18 2.3.2 Triggers. 18 2.3.3 User-defined functions . 18 Chapter 3. Introduction to the SQL Persistent Stored Module in DB2 Universal Database for iSeries . 19 3.1 Introduction . 20 3.2 System requirements and planning. 20 3.3 Structure of a SQL PSM program . 21 3.4 SQL control statements. 21 3.4.1 Assignment statement . 21 3.4.2 Conditional control . 22 3.4.3 Iterative control . 24 3.4.4 Calling procedures . 29 3.5 Compound SQL statement . 29 3.5.1 Nested compound statements . 30 3.5.2 Variable declaration . 30 3.5.3 Using cursors . 31 3.6 Using scrollable cursors in SQL PSM . 33 3.7 Dynamic SQL in SQL PSM . 34 3.7.1 PREPARE statement . 34 3.7.2 EXECUTE statement . 35 3.7.3 EXECUTE IMMEDIATE statement . 35 3.7.4 Cursors based on dynamic SQL . 35 3.8 Moving into production (save and restore) . 35 3.8.1 Restore processing for V4R5 and prior releases . 35 3.8.2 Restore processing for V5 . 36 iii 3.9 Adopted authorities in SQL PSM . 36 3.9.1 Authorities and adopted authorities in dynamic SQL . 38 3.10 Testing and debugging . 38 3.10.1 Graphical debugger . 38 3.10.2 The ILE source debugger . 46 3.10.3 Preparing the SQL procedure for debugging . 47 3.10.4 Testing the SQL PSM . 49 3.10.5 Testing the SQL PSM in a distributed environment . 52 3.11 Reverse engineering and Generate SQL . 54 3.11.1 Generate SQL. 55 Part 2. Stored procedures . 61 Chapter 4. Stored procedures . 63 4.1 Introduction . 64 4.2 Stored procedure types. 67 4.2.1 SQL stored procedures. 67 4.2.2 External stored procedure. 67 4.3 Registering stored procedures . 68 4.3.1 CREATE PROCEDURE . 68 4.3.2 DECLARE PROCEDURE . 70 4.4 System catalog tables . 74 4.4.1 SYSROUTINES catalog . 74 4.4.2 SYSPARMS catalog . 75 4.5 Procedure signature and procedure overloading . 75 4.6 Deleting or replacing a stored procedure . 76 4.6.1 Using a command line to drop a procedure . 76 4.6.2 Dropping overloaded procedures . 77 4.7 Authorization and adopted authority . 78 4.8 Returning result sets from stored procedures . 79 Chapter 5. SQL stored procedures . ..