SQL Procedures, Triggers, and Functions on IBM DB2 for i

Book description

Structured Query Language (SQL) procedures, triggers, and functions, which are also known as user-defined functions (UDFs), are the key database features for developing robust and distributed applications. IBM® DB2® for i supported these features for many years, and they are enhanced in IBM i versions 6.1, 7.1, and 7.2. DB2 for i refers to the IBM DB2 family member and relational database management system that is integrated within the IBM Power operating system that is known as IBM i.

This IBM Redbooks® publication includes several of the announced features for SQL procedures, triggers, and functions in IBM i versions 6.1, 7.1, and 7.2. This book includes suggestions, guidelines, and practical examples to develop DB2 for i SQL procedures, triggers, and functions effectively.

This book covers the following topics:

  • Introduction to the SQL/Persistent Stored Modules (PSM) language, which is used in SQL procedures, triggers, and functions

  • SQL procedures

  • SQL triggers

  • SQL functions


  • This book is for IBM i database engineers and data-centric developers who strive to provide flexible, extensible, agile, and scalable database solutions that meet business requirements in a timely manner.

    Before you read this book, you need to know about relational database technology and the application development environment on the IBM Power Systems™ with the IBM i operating system.

    Table of contents

    1. Front cover
    2. Notices
      1. Trademarks
    3. IBM Redbooks promotions
    4. Preface
      1. Authors
      2. Now you can become a published author, too!
      3. Comments welcome
      4. Stay connected to IBM Redbooks
    5. Chapter 1. Introduction to data-centric programming
      1. 1.1 Data-centric programming
      2. 1.2 Database engineering
    6. Chapter 2. Introduction to SQL Persistent Stored Module
      1. 2.1 Introduction
      2. 2.2 System requirements and planning
      3. 2.3 Structure of an SQL PSM program
      4. 2.4 SQL control statements
        1. 2.4.1 Assignment statement
        2. 2.4.2 Conditional control
        3. 2.4.3 Iterative control
        4. 2.4.4 Calling procedures
        5. 2.4.5 Compound SQL statement
      5. 2.5 Dynamic SQL in PSM
        1. 2.5.1 DECLARE CURSOR, PREPARE, and OPEN
        2. 2.5.2 PREPARE then EXECUTE
        3. 2.5.3 EXECUTE IMMEDIATE statement
      6. 2.6 Error handling
        1. 2.6.1 The basic database error indicators
        2. 2.6.2 Conditions and handlers
        3. 2.6.3 GET DIAGNOSTICS
        4. 2.6.4 SIGNAL and RESIGNAL
        5. 2.6.5 RETURN statement
        6. 2.6.6 Direct SQLSTATE and SQLCODE usage
        7. 2.6.7 Error handling in nested compound statements
      7. 2.7 Transaction management in procedures
        1. 2.7.1 Transaction management example
    7. Chapter 3. SQL fundamentals
      1. 3.1 SQL concepts
        1. 3.1.1 Schemas and libraries
        2. 3.1.2 Unqualified object names
        3. 3.1.3 SQL PATH
        4. 3.1.4 Global variables
      2. 3.2 Common information for SQL routines and triggers
        1. 3.2.1 Routine and trigger creation process
        2. 3.2.2 IBM i names for generated SQL objects
        3. 3.2.3 CREATE OR REPLACE
        4. 3.2.4 Shared attributes
      3. 3.3 DB2 sample database
      4. 3.4 Transactions
        1. 3.4.1 Transaction terminology
        2. 3.4.2 Transaction management
        3. 3.4.3 Transaction management in compound statements
      5. 3.5 DB2 for i catalog views
    8. Chapter 4. Procedures
      1. 4.1 Introduction to procedures
      2. 4.2 Structure of a procedure
      3. 4.3 Creating a procedure
        1. 4.3.1 CREATE PROCEDURE syntax
      4. 4.4 System catalog tables for procedures
        1. 4.4.1 SYSROUTINES catalog
        2. 4.4.2 SYSPARMS catalog
      5. 4.5 Procedure signature and procedure overloading
      6. 4.6 Calling a procedure
        1. 4.6.1 CALL statement syntax
      7. 4.7 Producing and consuming result sets
        1. 4.7.1 Creating result sets in an SQL procedure
        2. 4.7.2 Retrieving result sets in the caller
      8. 4.8 Handling errors
        1. 4.8.1 Basic database error indicators
        2. 4.8.2 Handling errors within procedures
        3. 4.8.3 Tailoring error messages
      9. 4.9 Summary
    9. Chapter 5. Triggers
      1. 5.1 Trigger concepts
      2. 5.2 Trigger types
        1. 5.2.1 SQL triggers
        2. 5.2.2 External triggers
      3. 5.3 Introduction to triggers
      4. 5.4 Defining triggers
      5. 5.5 Trigger examples
        1. 5.5.1 Simple trigger examples
        2. 5.5.2 Use of correlation names for column values
        3. 5.5.3 Multiple event triggers
        4. 5.5.4 Changing row values in a BEFORE trigger
        5. 5.5.5 Calling a procedure from a trigger
        6. 5.5.6 Using transition tables
        7. 5.5.7 Signaling an error
        8. 5.5.8 Self-referencing triggers
        9. 5.5.9 DB2ROW versus DB2SQL triggers
        10. 5.5.10 INSTEAD OF triggers
      6. 5.6 Additional trigger considerations
        1. 5.6.1 Trigger limits
        2. 5.6.2 Qualifying references
        3. 5.6.3 Trigger program attributes
        4. 5.6.4 Adding columns to tables
        5. 5.6.5 Dropping or revoking privileges on tables
        6. 5.6.6 Renaming or moving a table
        7. 5.6.7 Transaction isolation
        8. 5.6.8 Datetime considerations
        9. 5.6.9 Triggers and traditional record-level access
        10. 5.6.10 Multiple triggers on the same table
      7. 5.7 Trigger-related catalogs
    10. Chapter 6. Functions
      1. 6.1 Introduction
      2. 6.2 Nature of user-defined functions
        1. 6.2.1 User-defined scalar functions
        2. 6.2.2 User-defined table functions
      3. 6.3 Types of user-defined functions
        1. 6.3.1 Sourced UDFs
        2. 6.3.2 SQL UDFs
      4. 6.4 Structure of an SQL UDF
      5. 6.5 CREATE FUNCTION syntax for SQL scalar and table functions
        1. 6.5.1 Modifying or dropping a UDF
      6. 6.6 Resolving a UDF
        1. 6.6.1 UDF overloading and function signature
        2. 6.6.2 Parameter matching and promotion
        3. 6.6.3 Function path and the function selection algorithm
      7. 6.7 System catalog tables and views
        1. 6.7.1 SYSFUNCS catalog
        2. 6.7.2 SYSPARMS catalog
      8. 6.8 UDF examples
        1. 6.8.1 Simple scalar UDF
        2. 6.8.2 More complex SQL statement UDF
      9. 6.9 UDF inlining
        1. 6.9.1 Examples of INLINE and NON INLINE UDFs
      10. 6.10 UDTF examples
        1. 6.10.1 Single SQL statement UDTF
        2. 6.10.2 More complex SQL statement UDTFs
        3. 6.10.3 External action UDTF
        4. 6.10.4 UDTF for ranking
      11. 6.11 Pipelined table functions
        1. 6.11.1 PIPE syntax
        2. 6.11.2 Pipelined function examples
      12. 6.12 Coding considerations: UDF preferred practices
      13. 6.13 SQL control statements
      14. 6.14 Handling errors in SQL UDFs
    11. Chapter 7. Development and deployment
      1. 7.1 Tools for developing SQL routines and triggers
        1. 7.1.1 System i Navigator
        2. 7.1.2 IBM Data Studio
        3. 7.1.3 IBM i Access Client Solutions
        4. 7.1.4 Comparison
        5. 7.1.5 DB2 Express-C
      2. 7.2 Debug SQL routines and triggers
        1. 7.2.1 Debug SQL routines and triggers by using IBM Data Studio
        2. 7.2.2 Debug by using IBM Run SQL Scripts
      3. 7.3 Reverse engineering of SQL routines and triggers
        1. 7.3.1 System i Navigator
        2. 7.3.2 GENERATE_SQL
      4. 7.4 Ownership and authorities of SQL routines and triggers
        1. 7.4.1 Ownership
        2. 7.4.2 Authorities
      5. 7.5 Deployment of SQL routines and triggers
        1. 7.5.1 Deploying procedures and user-defined functions
        2. 7.5.2 Deploying triggers
    12. Chapter 8. Creating flexible and reusable procedures
      1. 8.1 Introduction to reusable SQL procedures
      2. 8.2 A modular approach to SQL procedure development
        1. 8.2.1 Global variables as default parameters
        2. 8.2.2 Simplified SQL descriptor usage
        3. 8.2.3 Result set consumption
        4. 8.2.4 Extended indicators and more
      3. 8.3 Summary
    13. Chapter 9. IBM i and IBM DB2 for i services
      1. 9.1 Health Center procedures
      2. 9.2 Utility procedures
        1. 9.2.1 QSYS2.EXTRACT_STATEMENTS
      3. 9.3 Plan cache procedures
        1. 9.3.1 QSYS2.DUMP_PLAN_CACHE_topN
      4. 9.4 DB Application Services
      5. 9.5 Performance Services
      6. 9.6 PTF Services
        1. 9.6.1 GROUP_PTF_CURRENCY
      7. 9.7 Security Services
        1. 9.7.1 QSYS2.SQL_CHECK_AUTHORITY()
      8. 9.8 Message Handling Services
        1. 9.8.1 QSYS2.JOBLOG_INFO
      9. 9.9 Librarian Services
        1. 9.9.1 QSYS2.OBJECT_STATISTICS()
      10. 9.10 Work Management Services
        1. 9.10.1 QSYS2.ACTIVE_JOB_INFO()
        2. 9.10.2 QSYS2.OBJECT_LOCK_INFO
        3. 9.10.3 QSYS2.SYSTEM_STATUS_INFO
      11. 9.11 Java Services
        1. 9.11.1 QSYS2.SET_JVM()
      12. 9.12 IBM i Application Services
        1. 9.12.1 QSYS2.QCMDEXC()
    14. Appendix A. Allocating, describing, and manipulating descriptors
      1. Introduction to SQL descriptors
      2. Allocating SQL descriptors
      3. DEALLOCATE SQL DESCRIPTOR
      4. Describing SQL descriptors
      5. Manipulating data to and from descriptors
    15. Appendix B. Additional material
      1. Locating the web material
      2. Using the web material
    16. Related publications
      1. IBM Redbooks
      2. Online resources
      3. Help from IBM
    17. Back cover

    Product information

    • Title: SQL Procedures, Triggers, and Functions on IBM DB2 for i
    • Author(s): Jim Bainbridge, Hernando Bedoya, Rob Bestgen, Mike Cain, Dan Cruikshank, Jim Denton, Doug Mack, Tom Mckinley, Simona Pacchiarini
    • Release date: April 2016
    • Publisher(s): IBM Redbooks
    • ISBN: 9780738441641