External Procedures, Triggers, and User-Defined Function ...

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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 5.4.1 Registering Java stored procedures with System i Navigator. . . . . . . . . . . . . . . 109 5.4.2 Using the Run SQL Scripts utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 5.4.3 Using the native interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 5.5 Calling Java stored procedures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 5.6 Using SQL NULL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 5.7 SQLJ procedures to manipulate JAR files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 5.7.1 SQLJ.INSTALL_JAR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 5.7.2 SQLJ.REMOVE_JAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 5.7.3 SQLJ.REPLACE_JAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 5.7.4 SQLJ.UPDATEJARINFO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 5.7.5 SQLJ.RECOVERJAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 5.8 Additional considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 5.8.1 Moving into production (save and restore) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 5.9 GetSuppliers example: Implementation with no result sets. . . . . . . . . . . . . . . . . . . . . 125 5.9.1 Stored procedure: GetSupplier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 5.9.2 Java client: ClientGetSupplier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 5.9.3 Java GUI client: ClientGetSupplierGUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132

iv External Procedures, Triggers, and User-Defined Functions on IBM DB2 for i

5.10 GetSupplierRS example: Implementation with result sets . . . . . . . . . . . . . . . . . . . . 132 5.10.1 GetSupplierRS stored procedure with the JAVA parameter style. . . . . . . . . . . 133 5.10.2 GetSupplierRS stored procedure with the DB2GENERAL parameter style . . . 135 5.10.3 Java clients: ClientGetSupplier and ClientGetSupplierGUI. . . . . . . . . . . . . . . . 138

5.11 Problem determination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 5.11.1 Debugging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 5.11.2 Tracing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

Chapter 6. Stored procedure error handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 6.1 Database error reporting strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

6.1.1 User-defined errors and warnings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 6.1.2 Consistent error handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 6.2 Error handling in SQL stored procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 6.2.1 Condition and handler declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 6.2.2 SIGNAL and RESIGNAL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 6.2.3 SQLCODE and SQLSTATE variables in the SQL procedure . . . . . . . . . . . . . . . 153 6.2.4 Returning values by using the RETURN statement . . . . . . . . . . . . . . . . . . . . . . 154 6.2.5 GET DIAGNOSTICS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 6.2.6 Error handling in nested compound statements . . . . . . . . . . . . . . . . . . . . . . . . . 156 6.2.7 Use nested compound statements for better performance . . . . . . . . . . . . . . . . . 164 6.3 Error handling in external stored procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 6.3.1 Checking the stored procedure completion status . . . . . . . . . . . . . . . . . . . . . . . 165 6.3.2 GENERAL and GENERAL WITH NULLS parameter styles . . . . . . . . . . . . . . . . 169 6.4 Error handling in Java stored procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 6.5 Retrieving user-defined errors in a client application . . . . . . . . . . . . . . . . . . . . . . . . . 173 6.5.1 Retrieving error conditions in a JDBC client . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 6.5.2 Retrieving error conditions from an ODBC or CLI client . . . . . . . . . . . . . . . . . . . 176 6.6 Transaction management in stored procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 6.6.1 Transaction management terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 6.6.2 Transactional behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 6.6.3 SQL statements for controlling transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 6.6.4 Transaction management in compound statements . . . . . . . . . . . . . . . . . . . . . . 184 6.7 External stored procedures and commitment control . . . . . . . . . . . . . . . . . . . . . . . . . 186 6.7.1 Activation group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 6.7.2 Savepoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 6.8 Several practical examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 6.8.1 SQL stored procedure example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 6.8.2 External stored procedure example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 6.8.3 Java stored procedure example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 6.8.4 C++ client code that uses ODBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 6.8.5 Java example client code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 6.8.6 Results for the example programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200

Chapter 7. Database triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 7.1 Trigger concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 7.2 Types of triggers in DB2 for i. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206

7.2.1 SQL triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 7.2.2 External triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 7.3 Enabling and disabling a trigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 7.4 Displaying and reviewing trigger information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 7.4.1 Using System i Navigator to view the properties of a trigger . . . . . . . . . . . . . . . 209 7.4.2 Displaying trigger information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 7.4.3 Printing trigger information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210

Contents v

7.5 System catalog tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 7.6 Authorization and adopted authorities on triggers. . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 7.7 Renaming and copying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215

Chapter 8. External triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 8.1 Defining a trigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218

8.1.1 ADDPFTRG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 8.1.2 Using System i Navigator to add an external trigger. . . . . . . . . . . . . . . . . . . . . . 222 8.2 Trigger program structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 8.2.1 Trigger buffer for RPG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 8.2.2 Trigger buffer for COBOL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 8.2.3 Trigger buffer for C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 8.2.4 Using the trigger buffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 8.3 Trigger feedback to application programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 8.3.1 Commitment control and triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 8.4 Designing trigger programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 8.4.1 Order Entry application scenario. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 8.4.2 Audit trail trigger example programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 8.4.3 Updating a trigger on the Order Header file program examples . . . . . . . . . . . . . 260 8.4.4 Softcoding the trigger buffer example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 8.4.5 Changing the record that fired a trigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 8.5 Applications and triggers: Design considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 8.6 Recommendations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300

Chapter 9. Triggers, referential integrity, and constraints. . . . . . . . . . . . . . . . . . . . . . 303 9.1 Transaction isolation and recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 9.2 Trigger journal entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 9.3 Triggers and referential integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 9.4 Comparing referential integrity and triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305

9.4.1 Using triggers to implement referential integrity rules . . . . . . . . . . . . . . . . . . . . . 305 9.5 Constraints and triggers: Ordering the actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306

9.5.1 Insert operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 9.5.2 Update operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 9.5.3 Delete operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 9.6 Triggers, referential integrity, and commitment control . . . . . . . . . . . . . . . . . . . . . . . . 310 9.6.1 When the application is not running commitment control . . . . . . . . . . . . . . . . . . 310 9.6.2 When the application runs under commitment control . . . . . . . . . . . . . . . . . . . . 310 9.7 Referential integrity, triggers, and journal entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311

Chapter 10. User-defined functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 10.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 10.2 Nature of user-defined functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315

10.2.1 User-defined scalar functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 10.2.2 User-defined table functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 10.3 Type of user-defined functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 10.3.1 Sourced UDFs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 10.3.2 SQL UDFs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 10.3.3 External UDFs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 10.4 Creating user-defined functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 10.4.1 CREATE FUNCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 10.4.2 Modifying a UDF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 10.4.3 Dropping a UDF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 10.5 Resolving a UDF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 10.5.1 UDF overloading and function signature. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325

vi External Procedures, Triggers, and User-Defined Functions on IBM DB2 for i

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download