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 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 . ..
Recommended publications
  • SQL Stored Procedures
    Agenda Key:31MA Session Number:409094 DB2 for IBM i: SQL Stored Procedures Tom McKinley ([email protected]) DB2 for IBM i consultant IBM Lab Services 8 Copyright IBM Corporation, 2009. All Rights Reserved. This publication may refer to products that are not currently available in your country. IBM makes no commitment to make available any products referred to herein. What is a Stored Procedure? • Just a called program – Called from SQL-based interfaces via SQL CALL statement • Supports input and output parameters – Result sets on some interfaces • Follows security model of iSeries – Enables you to secure your data – iSeries adopted authority model can be leveraged • Useful for moving host-centric applications to distributed applications 2 © 2009 IBM Corporation What is a Stored Procedure? • Performance savings in distributed computing environments by dramatically reducing the number of flows (requests) to the database engine – One request initiates multiple transactions and processes R R e e q q u u DB2 for i5/OS DB2DB2 for for i5/OS e e AS/400 s s t t SP o o r r • Performance improvements further enhanced by the option of providing result sets back to ODBC, JDBC, .NET & CLI clients 3 © 2009 IBM Corporation Recipe for a Stored Procedure... 1 Create it CREATE PROCEDURE total_val (IN Member# CHAR(6), OUT total DECIMAL(12,2)) LANGUAGE SQL BEGIN SELECT SUM(curr_balance) INTO total FROM accounts WHERE account_owner=Member# AND account_type IN ('C','S','M') END 2 Call it (from an SQL interface) over and over CALL total_val(‘123456’, :balance) 4 © 2009 IBM Corporation Stored Procedures • DB2 for i5/OS supports two types of stored procedures 1.
    [Show full text]
  • Best Practices Managing XML Data
    ® IBM® DB2® for Linux®, UNIX®, and Windows® Best Practices Managing XML Data Matthias Nicola IBM Silicon Valley Lab Susanne Englert IBM Silicon Valley Lab Last updated: January 2011 Managing XML Data Page 2 Executive summary ............................................................................................. 4 Why XML .............................................................................................................. 5 Pros and cons of XML and relational data ................................................. 5 XML solutions to relational data model problems.................................... 6 Benefits of DB2 pureXML over alternative storage options .................... 8 Best practices for DB2 pureXML: Overview .................................................. 10 Sample scenario: derivative trades in FpML format............................... 11 Sample data and tables................................................................................ 11 Choosing the right storage options for XML data......................................... 16 Selecting table space type and page size for XML data.......................... 16 Different table spaces and page size for XML and relational data ....... 16 Inlining and compression of XML data .................................................... 17 Guidelines for adding XML data to a DB2 database .................................... 20 Inserting XML documents with high performance ................................ 20 Splitting large XML documents into smaller pieces ..............................
    [Show full text]
  • DB2 10.5 with BLU Acceleration / Zikopoulos / 349-2
    Flash 6X9 / DB2 10.5 with BLU Acceleration / Zikopoulos / 349-2 DB2 10.5 with BLU Acceleration 00-FM.indd 1 9/17/13 2:26 PM Flash 6X9 / DB2 10.5 with BLU Acceleration / Zikopoulos / 349-2 00-FM.indd 2 9/17/13 2:26 PM Flash 6X9 / DB2 10.5 with BLU Acceleration / Zikopoulos / 349-2 DB2 10.5 with BLU Acceleration Paul Zikopoulos Sam Lightstone Matt Huras Aamer Sachedina George Baklarz New York Chicago San Francisco Athens London Madrid Mexico City Milan New Delhi Singapore Sydney Toronto 00-FM.indd 3 9/17/13 2:26 PM Flash 6X9 / DB2 10.5 with BLU Acceleration / Zikopoulos / 349-2 McGraw-Hill Education books are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training programs. To contact a representative, please visit the Contact Us pages at www.mhprofessional.com. DB2 10.5 with BLU Acceleration: New Dynamic In-Memory Analytics for the Era of Big Data Copyright © 2014 by McGraw-Hill Education. All rights reserved. Printed in the Unit- ed States of America. Except as permitted under the Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of pub- lisher, with the exception that the program listings may be entered, stored, and exe- cuted in a computer system, but they may not be reproduced for publication. All trademarks or copyrights mentioned herein are the possession of their respective owners and McGraw-Hill Education makes no claim of ownership by the mention of products that contain these marks.
    [Show full text]
  • Writing SQL Stored Procedures & Functions
    1/14/2017 SQL Procedures (and Functions and Triggers) Rob Bestgen [email protected] IBM - DB2 for i Consultant © 2017 IBM Corporation IBM Power Systems SQL as a development language SQL is a well established, standardized language for database access SQL is also a programming language! . SQL/PSM (https://en.wikipedia.org/wiki/SQL/PSM) is a full procedural programming language . PSM enhances portability – Supported across DB2 Family – Similar to proprietary DBMS procedure languages (PL/SQL, T-SQL, etc…) Makes it easier for SQL programmers to be productive faster on IBM i 2 © 2017 IBM Corporation IBM1 1/14/2017 IBM Power Systems Supported languages DB2 for i supports two types of procedures/functions/triggers 1. SQL • Coded entirely with SQL following (PSM) Standard • Allows full suite of SQL statements 2. External • Register a high-level language program (RPG, COBOL, Java, C…) for subsequent calls from SQL** • may or may not use SQL within the program itself ** Non-SQL triggers use CL command ADDPFTRG 3 © 2017 IBM Corporation IBM Power Systems Supported languages DB2 for i supports two types of procedures/functions/triggers 1. SQL • Coded entirely with SQL following (PSM) Standard • Allows full suite of SQL statements 2. External • Register a high-level language program (RPG, COBOL, Java, C…) for subsequent calls from SQL** • may or may not use SQL within the program itself SQL is the main focus here ** Non-SQL triggers use CL command ADDPFTRG 4 © 2017 IBM Corporation IBM2 1/14/2017 IBM Power Systems SQL Routines Comparison of SQL Routine types . Procedures – Similar to high-level language program, facilitates reuse of common logic – Usage is similar to any program – Frequently used as the backend ‘service’ to application calls e.g.
    [Show full text]
  • SQL Procedures, Triggers, and Functions on IBM DB2 for I
    Front cover SQL Procedures, Triggers, and Functions on IBM DB2 for i Jim Bainbridge Hernando Bedoya Rob Bestgen Mike Cain Dan Cruikshank Jim Denton Doug Mack Tom Mckinley Simona Pacchiarini Redbooks International Technical Support Organization SQL Procedures, Triggers, and Functions on IBM DB2 for i April 2016 SG24-8326-00 Note: Before using this information and the product it supports, read the information in “Notices” on page ix. First Edition (April 2016) This edition applies to Version 7, Release 2, of IBM i (product number 5770-SS1). © Copyright International Business Machines Corporation 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 IBM Redbooks promotions . xi Preface . xiii Authors. xiii Now you can become a published author, too! . xvi Comments welcome. xvi Stay connected to IBM Redbooks . xvi Chapter 1. Introduction to data-centric programming. 1 1.1 Data-centric programming. 2 1.2 Database engineering . 2 Chapter 2. Introduction to SQL Persistent Stored Module . 5 2.1 Introduction . 6 2.2 System requirements and planning. 6 2.3 Structure of an SQL PSM program . 7 2.4 SQL control statements. 8 2.4.1 Assignment statement . 8 2.4.2 Conditional control . 11 2.4.3 Iterative control . 15 2.4.4 Calling procedures . 18 2.4.5 Compound SQL statement . 19 2.5 Dynamic SQL in PSM . 22 2.5.1 DECLARE CURSOR, PREPARE, and OPEN . 23 2.5.2 PREPARE then EXECUTE. 26 2.5.3 EXECUTE IMMEDIATE statement .
    [Show full text]
  • Dclgen Declaration in Cobol
    Dclgen Declaration In Cobol SandyIntemperate ingeminate and egalitarian her takes Anthonyrack-rents always endearingly. outcrops hypodermically and paraffin his grafter. Rex debated what. Job skivvies edifyingly as shelterless Explain with this way to cobol declaration in dclgen command like to close the cobol copybook converter performs limited syntax to the books and multiple records you toggle this site Cobol Db2 Sample Pgm PDF Document FDOCUMENTS. Avoid Breaking DCLGEN Host Variables into Components Although doing. What dclgen contains cobol declaration for declare table declarations and focus only be declared to use. What are Cobol programmers? What happens if you throw a copybook to linkage section. Cursor with only specify a different number of any other member names, digital learning platform to resolve issues associated data type the programmer. Idz-utilitiesGAM0BETcpy at master IBMidz-utilities GitHub. Db2 describe view LSP HKI. Jobs for cobol declaration in dclgen does idms ensure data in an sql declare table may have checked. Test the most portable and other wise actually do to rephrase the predefined table. What is difference between suggest and package? It in dclgen means nothing will be declared in copybook. It contains the db2 equalent cobol variables called host variables. Environment need of dclgen as much to declare smallint in a production environment division specifies the declaration in java intermediate values may move. Course her career entry course with software developers legacy systemcobol qualification level. When the java garbage collection algorithms may only that we need to costly to toggle press return more information. Cobol compiler settings also with. Download COBOL. Default is converted between declaring a reverse dclgen means nothing will not allow for a digital way incorrect modification of jcl to utilities to combat delays, then be declared to.
    [Show full text]
  • Database Administration Guide for SAP on IBM Db2 for Linux, UNIX, and Windows Company
    Administration Guide | PUBLIC Document Version: 2.5 – 2021-09-23 Database Administration Guide for SAP on IBM Db2 for Linux, UNIX, and Windows company. All rights reserved. All rights company. affiliate THE BEST RUN 2021 SAP SE or an SAP SE or an SAP SAP 2021 © Content 1 Introduction................................................................7 1.1 Document History............................................................ 8 1.2 Naming Conventions..........................................................10 2 Administration Tools and Tasks.................................................12 2.1 Administration Tools..........................................................12 2.2 Administration Tasks..........................................................13 3 Architectural Overview.......................................................16 3.1 SAP Application Server for ABAP.................................................16 3.2 SAP Application Server for Java..................................................17 3.3 Db2 Components............................................................18 3.4 Db2 Client Connectivity........................................................19 4 User Management and Security................................................ 21 4.1 SAP System Users and Groups...................................................21 Access Authorizations for Db2 Directories and Database-Related Files....................25 4.2 Role-Based Security Concept for Database Users..................................... 27 Database Roles for
    [Show full text]
  • SQL Stored Routines Draft 2017-06-09
    SQL Stored Routines Draft 2017-06-09 www.DBTechNet.org SQL Stored Routines Procedural Extensions of SQL and External Routines in Transactional Context Authors: Martti Laiho, Fritz Laux, Ilia Petrov, Dimitris Dervos Parts of application logic can be stored at database server-side as “stored routines”, that is as stored procedures, user-defined-functions, and triggers. The client-side application logic makes use of these routines in the context of database transactions, except that the application does not have control over triggers, but triggers control the data access actions of the application. The tutorial introduces the procedural extension of the SQL language, the concepts of stored routines in SQL/PSM of ISO SQL standard, and the implementations in some mainstream DBMS products available for hands-on experimentations in the free virtual database laboratory of DBTechNet.org. licensed on Creative Commons license for non-commercial use SQL Stored Routines www.DBTechNet.org SQL Stored Routines - Procedural Extensions of SQL in Transactional Context This is a companion tutorial to the “SQL Transactions – Theory and hands-on labs” of DBTech VET project available at www.dbtechnet.org/download/SQL-Transactions_handbook_EN.pdf . For more information or corrections please email to martti.laiho(at)gmail.com. You can contact us also via the LinkedIn group DBTechNet. Disclaimer Don’t believe what you read . This tutorial may provide you some valuable technology details, but you need to verify everything by yourself! Things may change in every new software version. The writers don’t accept any responsibilities on damages resulting from this material. The trademarks of mentioned products belong to the trademark owners.
    [Show full text]
  • SQL Stored Routines
    11/1/2014 www.DBTechNet.org SQL Stored Routines DBTechNet workshop in Reutlingen University 2014‐11‐04 [email protected] Stored Routines for Application Logic and Business Rules End-user User Interface / user transactions Application logic Retry wrapper Retry wrapper …… Transaction Transaction Transaction Data access DBMS Stored routines procedures functions database triggers 1 11/1/2014 Procedural Languages Languages: • ISO SQL: SQL/PSM language definition 1996.. • Oracle: PL/SQL • DB2: SQL PL (SQL/PSM), PL/SQL • MySQL/MariaDB: SQL/PSM • PostgreSQL: PL/pgSQL, … • SQL Server: Transact SQL (”T‐SQL”) Used for: Stored Routines (modules, packages of), Scripting Oracle Server’s Stack of Engines and Languages packages + triggers 2 11/1/2014 Database Server Stacks SQL PL, PL/SQL, PLpgSQL, .. PL engine Oracle, MySQL, MariaDB MongoDB XQuery SQL engine engine noSQL engine Oracle, DB2, InnoDB, MyISAM, .. Storage local transaction manager engine concurrency control database database database .. Procedural Languages ‐ Control structures 3 11/1/2014 PL/SQL Control Structures by Timo Leppänen / Oracle Finland Stored Routines • Procedures : Callable ”subprograms” • Functions : User Defined Functions (UDF) Scalar and table‐valued functions extending the SQL language • Methods: Function methods of User Defined Type (UDT) objects • Triggers: Programmable constraints • Packages: (non‐standard) collections of procedures/functions 4 11/1/2014 PL/SQL Block Types by Timo Leppänen / Oracle Finland ‐ Used in scripts and compound commands by SQL‐clients PL/SQL
    [Show full text]
  • Extremely Purexml in DB2 10 for Z/OS
    Front cover Draft Document for Review January 9, 2011 1:25 pm SG24-7915-00 Extremely pureXML in DB2 10 for z/OS Develop Java and COBOL applications accessing SQL and XML data Administer your XML and SQL data Choose the best options for installation and use Paolo Bruni Neale Armstrong Ravi Kumar Kirsten Ann Larsen Tink Tysor Hao Zhang ibm.com/redbooks Draft Document for Review January 9, 2011 1:25 pm 7915edno.fm International Technical Support Organization Extremely pureXML in DB2 10 for z/OS January 2011 SG24-7915-00 7915edno.fm Draft Document for Review January 9, 2011 1:25 pm Note: Before using this information and the product it supports, read the information in “Notices” on page xvii. First Edition (January 2011) This edition applies to Version 10.1 of IBM DB2 for z/OS (program number 5605-DB2). This document created or updated on January 9, 2011. Note: This book is based on a pre-GA version of a program and may not apply when the program becomes generally available. We recommend that you consult the program documentation or follow-on versions of this IBM Redbooks publication for more current information. © Copyright International Business Machines Corporation 2011. All rights reserved. Note to U.S. Government Users Restricted Rights -- Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. Draft Document for Review January 9, 2011 1:25 pm 7915TOC.fm Contents Figures . ix Tables . xi Examples . xiii Notices . xvii Trademarks . xviii Preface . xix The team who wrote this book . xix Now you can become a published author, too! .
    [Show full text]
  • IBM DB2 Universal Database Replication Guide and Reference
    ® ® IBM DB2 Universal Database Replication Guide and Reference Ve r s i o n 7 SC26-9920-00 ® ® IBM DB2 Universal Database Replication Guide and Reference Ve r s i o n 7 SC26-9920-00 Before using this information and the product it supports, be sure to read the general information under “Appendix C. Notices” on page 403. This document contains proprietary information of IBM. It is provided under a license agreement and is protected by copyright law. The information contained in this publication does not include any product warranties, and any statements provided in this manual should not be interpreted as such. Order publications through your IBM representative or the IBM branch office serving your locality or by calling 1-800-879-2755 in the United States or 1-800-IBM-4YOU in Canada. When you send information to IBM, you grant IBM a nonexclusive right to use or distribute the information in any way it believes appropriate without incurring any obligation to you. © Copyright International Business Machines Corporation 1994, 2000. All rights reserved. US Government Users Restricted Rights – Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. Contents About this book ..........ix Data distribution .........19 Who should read this book .......x Data consolidation .........20 How this book is structured .......x Update anywhere .........21 Conventions ............x Occasionally connected .......22 Terminology ............xi Examples of replication configurations . 24 How to read syntax diagrams ......xi Archiving audit information .....24 Roadmap............xii Consolidating data from distributed How to send your comments ......xiii databases ............24 Distributing data to remote sites ....25 What’s new ............xv Distributing IMS data to remote sites .
    [Show full text]
  • DB2 9 Purexml Guide
    Front cover DB2 9 pureXML Guide Learning SQL/XML, XQuery, XPath with working examples Developing XML applications with DB2 pureXML Managing XML for maximum return Whei-Jen Chen Art Sammartino Dobromir Goutev Felicity Hendricks Ippei Komi Ming-Pang Wei Rav Ahuja ibm.com/redbooks International Technical Support Organization DB2 9 pureXML Guide January 2007 SG24-7315-01 Note: Before using this information and the product it supports, read the information in “Notices” on page ix. First Edition (January 2007) This edition applies to DB2 9 for Linux, UNIX, and Windows. © Copyright International Business Machines Corporation 2007. 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 The team that wrote this redbook. xii Acknowledgements . xiii Become a published author . xiv Comments welcome. xv Chapter 1. Introducing DB2 9: pureXML . 1 1.1 Growing importance of XML data . 2 1.1.1 Growth of XML . 2 1.1.2 The value of XML data . 4 1.2 pureXML overview. 4 1.2.1 Traditional methods for managing XML data . 5 1.2.2 XML data management with DB2 9 . 6 1.2.3 Setting up databases for XML. 7 1.2.4 XML optimized storage and XML data type . 7 1.2.5 Getting XML data into the database . 8 1.2.6 Querying XML data . 8 1.2.7 Query optimization and indexes for XML . 10 1.2.8 XML schema repository and validation . 10 1.2.9 Full text search for XML .
    [Show full text]