Database Db2 Universal Database For Iseries SQL Programming: System I
Database Db2 Universal Database For Iseries SQL Programming: System I
Database Db2 Universal Database For Iseries SQL Programming: System I
System i
Note Before using this information and the product it supports, read the information in Notices, on page 303.
Seventh Edition (February 2006) This edition applies to version 5, release 4, modification 0 of IBM i5/OS (product number 5722-SS1) and to all subsequent releases and modifications until otherwise indicated in new editions. This version does not run on all reduced instruction set computer (RISC) models nor does it run on CISC models. Copyright International Business Machines Corporation 1998, 2006. All rights reserved. US Government Users Restricted Rights Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
Contents
SQL programming . . . . . . . . . . 1
Whats new for V5R4 . . . . . . . . . . Printable PDF . . . . . . . . . . . . . Introduction to DB2 UDB for iSeries Structured Query Language . . . . . . . . . . . . SQL concepts . . . . . . . . . . . . SQL relational database and system terminology . . . . . . . . . . . SQL and system naming conventions . . . Types of SQL statements . . . . . . . SQL communication area . . . . . . . SQL diagnostics area. . . . . . . . . SQL objects . . . . . . . . . . . . . Schemas . . . . . . . . . . . . . Journals and journal receivers . . . . . Catalogs . . . . . . . . . . . . . Tables, rows, and columns . . . . . . . Aliases . . . . . . . . . . . . . Views . . . . . . . . . . . . . . Indexes . . . . . . . . . . . . . Constraints . . . . . . . . . . . . Triggers . . . . . . . . . . . . Stored procedures . . . . . . . . . Sequences . . . . . . . . . . . . User-defined functions. . . . . . . . User-defined types . . . . . . . . . SQL packages. . . . . . . . . . . Application program objects . . . . . . . User source file member . . . . . . . Output source file member . . . . . . Program . . . . . . . . . . . . SQL package . . . . . . . . . . . Module . . . . . . . . . . . . . Service program . . . . . . . . . . Data definition language . . . . . . . . . Creating a schema . . . . . . . . . . Creating a table . . . . . . . . . . . Adding and removing constraints . . . . Referential integrity and tables . . . . . Adding and removing referential constraints . . . . . . . . . . . Example: Adding referential constraints . Example: Removing constraints . . . . . Check pending . . . . . . . . . . Creating a table using LIKE . . . . . . . Creating a table using AS . . . . . . . . Creating and altering a materialized query table Declaring a global temporary table . . . . Creating and altering an identity column . . Using ROWID . . . . . . . . . . . Creating and using sequences . . . . . . Comparison of identity columns and sequences . . . . . . . . . . . . Creating descriptive labels using the LABEL ON statement . . . . . . . . . . . . . Describing an SQL object using COMMENT ON
Copyright IBM Corp. 1998, 2006
. 1 . 2 . 2 . 3 . 4 . 5 . 5 . 7 . 7 . 7 . 8 . 8 . 8 . 8 . 9 . 9 . 9 . 9 . 10 . 10 . 10 . 10 . 11 . 11 . 11 . 13 . 13 . 13 . 13 . 14 . 14 . 14 . 14 . 15 . 15 . 16 . . . . . . . . . . 16 17 18 18 19 19 20 21 21 22 22
. 24 . 25 26
Changing a table definition . . . . . . . Adding a column . . . . . . . . . Changing a column . . . . . . . . . Allowable conversions of data types . . . Deleting a column . . . . . . . . . Order of operations for the ALTER TABLE statement . . . . . . . . . . . . Creating and using ALIAS names . . . . . Creating and using views. . . . . . . . WITH CHECK OPTION on a view . . . WITH CASCADED CHECK OPTION . WITH LOCAL CHECK OPTION . . . Example: Cascaded check option . . . Creating indexes . . . . . . . . . . . Catalogs in database design . . . . . . . Getting catalog information about a table . Getting catalog information about a column Dropping a database object . . . . . . . Data manipulation language . . . . . . . . Retrieving data using the SELECT statement . Basic SELECT statement . . . . . . . Specifying a search condition using the WHERE clause . . . . . . . . . . Expressions in the WHERE clause . . . Comparison operators . . . . . . . NOT keyword . . . . . . . . . GROUP BY clause . . . . . . . . . HAVING clause . . . . . . . . . . ORDER BY clause . . . . . . . . . Static SELECT statements . . . . . . . Handling null values . . . . . . . . Special registers in SQL statements . . . Casting data types . . . . . . . . . Date, time, and timestamp data types . . Specifying current date and time values . Date/time arithmetic . . . . . . . Handling duplicate rows . . . . . . . Defining complex search conditions . . . Special considerations for LIKE . . . . Multiple search conditions within a WHERE clause . . . . . . . . . Using OLAP specifications . . . . . . Joining data from more than one table . . Inner join . . . . . . . . . . . Left outer join . . . . . . . . . Right outer join . . . . . . . . . Exception join . . . . . . . . . Cross join . . . . . . . . . . . Simulating a full outer join . . . . . Multiple join types in one statement . . Using table expressions . . . . . . . Using recursive queries . . . . . . . Using the UNION keyword to combine subselects . . . . . . . . . . . . Specifying the UNION ALL keyword . . Using the EXCEPT keyword . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
26 26 27 27 28 28 29 29 31 31 32 33 34 34 35 35 35 36 36 36 37 38 39 40 40 42 43 44 45 46 48 48 48 49 49 50 51 52 53 55 56 57 58 58 59 59 60 60 62
. 68 . 71 . 73
iii
Using the INTERSECT keyword . . . . . 75 Data retrieval errors . . . . . . . . . 77 Inserting rows using the INSERT statement . . . 78 Inserting rows using the VALUES clause . . 80 Inserting rows using a select-statement . . . 80 Inserting multiple rows using the blocked INSERT statement . . . . . . . . . . 81 Inserting data into tables with referential constraints . . . . . . . . . . . . . 81 Inserting values into an identity column . . . 82 Changing data in a table using the UPDATE statement . . . . . . . . . . . . . . 83 Updating a table using a scalar-subselect . . 84 Updating a table with rows from another table 85 Updating tables with referential constraints . . 85 Examples: UPDATE rules . . . . . . . 86 Updating an identity column . . . . . . 86 Updating data as it is retrieved from a table 86 Removing rows from a table using the DELETE statement . . . . . . . . . . . . . . 88 Removing rows from tables with referential constraints . . . . . . . . . . . . . 89 Example: DELETE rules . . . . . . . 90 Using subqueries . . . . . . . . . . . 91 Subqueries in SELECT statements . . . . . 92 Subqueries and search conditions . . . . 93 Usage notes on subqueries . . . . . . 93 Including subqueries in the WHERE or HAVING clause . . . . . . . . . . 93 Correlated subqueries . . . . . . . . . 95 Correlated names and references . . . . 95 Example: Correlated subquery in a WHERE clause . . . . . . . . . . . . . 95 Example: Correlated subquery in a HAVING clause . . . . . . . . . . 97 Example: Correlated subquery in a select-list . . . . . . . . . . . . 97 Example: Correlated subquery in an UPDATE statement . . . . . . . . . 98 Example: Correlated subquery in a DELETE statement . . . . . . . . . 99 Sort sequences and normalization in SQL . . . . 99 Sort sequence used with ORDER BY and row selection . . . . . . . . . . . . . . 100 Sort sequence and ORDER BY . . . . . . 100 Sort sequence and row selection . . . . . 102 Sort sequence and views . . . . . . . . 102 Sort sequence and the CREATE INDEX statement . . . . . . . . . . . . . . 103 Sort sequence and constraints . . . . . . . 103 ICU sort sequence . . . . . . . . . . . 103 Normalization . . . . . . . . . . . . 104 Data protection . . . . . . . . . . . . . 105 Security for SQL objects . . . . . . . . . 105 Authorization ID . . . . . . . . . . 106 Views . . . . . . . . . . . . . . 106 Auditing . . . . . . . . . . . . . 106 Data integrity . . . . . . . . . . . . 107 Concurrency . . . . . . . . . . . . 107 Journaling . . . . . . . . . . . . 108 Commitment control . . . . . . . . . 109
Savepoints . . . . . . . . . . . . Atomic operations . . . . . . . . . . Constraints . . . . . . . . . . . . Adding and using check constraints . . . Save and restore functions . . . . . . . Damage tolerance . . . . . . . . . . Index recovery . . . . . . . . . . . Catalog integrity . . . . . . . . . . User auxiliary storage pool . . . . . . . Independent auxiliary storage pool . . . . Routines . . . . . . . . . . . . . . . Stored procedures . . . . . . . . . . . Defining an external procedure . . . . . Defining an SQL procedure . . . . . . . Calling a stored procedure . . . . . . . Using the CALL statement where procedure definition exists . . . . . . Using the embedded CALL statement where no procedure definition exists . . Using the embedded CALL statement with an SQLDA . . . . . . . . . Using the dynamic CALL statement where no CREATE PROCEDURE exists . . . . Examples: CALL statements . . . . . Returning result sets from stored procedures Example 1: Calling a stored procedure that returns a single result set . . . . . Example 2: Calling a stored procedure that returns a result set from a nested procedure . . . . . . . . . . . Parameter passing conventions for stored procedures and user-defined functions . . . Indicator variables and stored procedures Returning a completion status to the calling program . . . . . . . . . . . . . Using user-defined functions . . . . . . . UDF concepts . . . . . . . . . . . Writing UDFs as SQL functions . . . . . Example: SQL scalar UDFs . . . . . . Example: SQL table UDFs . . . . . . Writing UDFs as external functions . . . . Registering UDFs . . . . . . . . . Passing arguments from DB2 to external functions . . . . . . . . . . . . Table function considerations . . . . . Error processing for UDFs . . . . . . Threads considerations . . . . . . . Parallel processing. . . . . . . . . Fenced or unfenced considerations . . . Save and restore considerations . . . . Examples: UDF code . . . . . . . . . Example: Square of a number UDF . . . Example: Counter . . . . . . . . . Example: Weather table function . . . . Using UDFs in SQL statements . . . . . Using parameter markers or the NULL values as function arguments . . . . . Using qualified function references . . . Using unqualified function references . . Summary of function references . . . . Triggers . . . . . . . . . . . . . .
112 114 115 116 116 117 117 118 118 119 119 119 120 121 125 126 126 128 129 129 136 136
137 143 147 149 149 150 152 152 152 153 153 156 162 162 163 164 164 164 165 165 166 167 173 173 174 174 175 177
iv
SQL triggers . . . . . . . . . . . . BEFORE SQL triggers . . . . . . . AFTER SQL triggers . . . . . . . . INSTEAD OF SQL triggers . . . . . . Handlers in SQL triggers . . . . . . SQL trigger transition tables . . . . . External triggers . . . . . . . . . . Example: External triggers . . . . . . Debugging an SQL routine . . . . . . . . Improving performance of procedures and functions . . . . . . . . . . . . . . Improving implementation of procedures and functions . . . . . . . . . . . . . Redesigning routines for performance . . . Processing special data types . . . . . . . . Large objects . . . . . . . . . . . . Large object data types . . . . . . . . Large object locators . . . . . . . . . Example: Using a locator to work with a CLOB value . . . . . . . . . . . . Example: LOBLOC.SQC in C . . . . . Example: LOBLOC.SQB in COBOL . . . Indicator variables and LOB locators . . . LOB file reference variables . . . . . . Example: Extracting CLOB data to a file . . Example: LOBFILE.SQC in C . . . . . Example: LOBFILE.SQB in COBOL . . . Example: Inserting data into a CLOB column Displaying the layout of LOB columns . . . Journal entry layout of LOB columns . . . User-defined distinct types . . . . . . . . Defining a UDT . . . . . . . . . . Example: Money . . . . . . . . . Example: Resum . . . . . . . . . Defining tables with UDTs . . . . . . . Example: Sales . . . . . . . . . . Example: Application forms . . . . . Manipulating UDTs . . . . . . . . . Examples: Using UDTs . . . . . . . . Example: Comparisons between UDTs and constants . . . . . . . . . . . . Example: Casting between UDTs . . . . Example: Comparisons involving UDTs Example: Sourced UDFs involving UDTs Example: Assignments involving UDTs Example: Assignments in dynamic SQL Example: Assignments involving different UDTs . . . . . . . . . . . . . Example: Using UDTs in UNION . . . . Examples: Using UDTs, UDFs, and LOBs . . . Example: Defining the UDT and UDFs . . . Example: Using the LOB function to populate the database . . . . . . . . Example: Using UDFs to query instances of UDTs . . . . . . . . . . . . . . Example: Using LOB locators to manipulate UDT instances . . . . . . . . . . . Using DataLinks . . . . . . . . . . . Linking control levels in DataLinks . . . . NO LINK CONTROL . . . . . . .
177 178 179 180 182 183 184 184 188 188 188 190 191 191 192 192 193 193 195 197 197 198 198 199 200 201 201 202 202 203 203 203 203 204 204 204 204 205 206 206 207 207 207 208 209 209 210 210 211 211 212 212
| |
FILE LINK CONTROL with FS permissions . . . . . . . . . . . FILE LINK CONTROL with DB permissions . . . . . . . . . . . Working with DataLinks . . . . . . . Using SQL in different environments . . . . . Using a cursor . . . . . . . . . . . . Types of cursors . . . . . . . . . . Examples: Using a cursor . . . . . . . Step 1: Defining the cursor . . . . . . Step 2: Opening the cursor . . . . . . Step 3: Specifying what to do when the end of data is reached . . . . . . . Step 4: Retrieving a row using a cursor Step 5a: Updating the current row . . . Step 5b: Deleting the current row . . . . Step 6: Closing the cursor . . . . . . Using the multiple-row FETCH statement Multiple-row FETCH using a host structure array . . . . . . . . . . Multiple-row FETCH using a row storage area . . . . . . . . . . . . . Unit of work and open cursors . . . . . Dynamic SQL applications . . . . . . . . Designing and running a dynamic SQL application . . . . . . . . . . . . CCSID of dynamic SQL statements . . . . Processing non-SELECT statements . . . . Using the PREPARE and EXECUTE statements . . . . . . . . . . . Processing SELECT statements and using a descriptor . . . . . . . . . . . . Fixed-list SELECT statements . . . . . Varying-list SELECT statements . . . . SQL descriptor areas . . . . . . . . SQLDA format . . . . . . . . . . Example: A SELECT statement for allocating storage for SQLDA . . . . . Example: A SELECT statement using an allocated SQL descriptor . . . . . . . Parameter markers . . . . . . . . Using dynamic SQL through client interfaces Accessing data with Java . . . . . . . Accessing data with Domino . . . . . . Accessing data with ODBC . . . . . . . Accessing data with i5/OS PASE . . . . . Accessing data with iSeries Access for Windows OLE DB Provider . . . . . . Accessing data with Net.Data . . . . . . Accessing data through a Linux partition . . Accessing data using Distributed Relational Database Architecture (DRDA) . . . . . Using interactive SQL . . . . . . . . . Starting interactive SQL . . . . . . . . Using the statement entry function . . . . Prompting . . . . . . . . . . . . Syntax checking . . . . . . . . . Statement processing mode . . . . . . Subqueries . . . . . . . . . . . CREATE TABLE prompting . . . . . Entering DBCS data . . . . . . . .
Contents
212 212 213 215 215 215 216 218 219 220 220 221 221 222 222 223 224 226 227 228 228 228 228 229 229 230 231 232 234 238 241 242 242 243 243 243 243 243 243 244 244 245 246 246 247 248 248 248 248
Using the list selection function . . . . . Example: Using the list selection function Session services description . . . . . . Exiting interactive SQL . . . . . . . . Using an existing SQL session . . . . . . Recovering an SQL session . . . . . . . Accessing remote databases with interactive SQL . . . . . . . . . . . . . . Using the SQL statement processor . . . . . Execution of statements after errors occur Commitment control in the SQL statement processor . . . . . . . . . . . . . Source member listing for the SQL statement processor . . . . . . . . . . . . . Distributed relational database function and SQL DB2 UDB for iSeries distributed relational database support . . . . . . . . . . . DB2 UDB for iSeries distributed relational database example program . . . . . . . . SQL package support . . . . . . . . . . Valid SQL statements in an SQL package . . Considerations for creating an SQL package CRTSQLPKG authorization . . . . . . Creating a package on a database other than DB2 UDB for iSeries . . . . . . Target release (TGTRLS) parameter . . . SQL statement size . . . . . . . . Statements that do not require a package Package object type . . . . . . . . ILE programs and service programs . . . Package creation connection . . . . . Unit of work . . . . . . . . . . Creating packages locally . . . . . . Labels . . . . . . . . . . . . . Consistency token . . . . . . . . . SQL and recursion . . . . . . . . . CCSID considerations for SQL . . . . . . . Connection management and activation groups Source code for PGM1 . . . . . . . . Source code for PGM2 . . . . . . . . Source code for PGM3 . . . . . . . . Multiple connections to the same relational database . . . . . . . . . . . . . Implicit connection management for the default activation group . . . . . . . . Implicit connection management for nondefault activation groups . . . . . . Distributed support . . . . . . . . . . Determining the connection type . . . . . Connect and commitment control restrictions
249 249 251 252 253 253 253 255 256 256 257 258 259 260 261 261 262 262 262 263 263 263 263 263 264 264 264 264 264 265 265 265 266 266 267 269 270 271 271 272 274
Determining the connection status . . . Distributed unit of work connection considerations . . . . . . . . . . Ending connections . . . . . . . . Distributed unit of work. . . . . . . . Managing distributed unit of work connections . . . . . . . . . . . Checking the connection status . . . . Cursors and prepared statements . . . . Application requester driver programs . . . Problem handling . . . . . . . . . . DRDA stored procedure considerations . . . Reference . . . . . . . . . . . . . . DB2 Universal Database for iSeries sample tables . . . . . . . . . . . . . . Department table (DEPARTMENT) . . . DEPARTMENT . . . . . . . . . Employee table (EMPLOYEE) . . . . . EMPLOYEE . . . . . . . . . . Employee photo table (EMP_PHOTO) . . EMP_PHOTO . . . . . . . . . Employee resum table (EMP_RESUME) . EMP_RESUME . . . . . . . . . Employee to project activity table (EMPPROJACT) . . . . . . . . . EMPPROJACT . . . . . . . . . Project table (PROJECT) . . . . . . . PROJECT . . . . . . . . . . . Project activity table (PROJACT) . . . . PROJACT . . . . . . . . . . Activity table (ACT) . . . . . . . . ACT . . . . . . . . . . . . Class schedule table (CL_SCHED) . . . CL_SCHED . . . . . . . . . . In-tray table (IN_TRAY) . . . . . . . IN_TRAY . . . . . . . . . . . Organization table (ORG) . . . . . . ORG . . . . . . . . . . . . Staff table (STAFF) . . . . . . . . STAFF . . . . . . . . . . . . Sales table (SALES) . . . . . . . . SALES . . . . . . . . . . . . DB2 UDB for iSeries CL command descriptions
. 275 . 276 . 276 . 277 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 279 279 280 280 281 281 281 282 283 283 284 285 286 286 287 287 288 290 290 291 292 294 294 295 295 295 296 296 297 297 298 299 299 300
vi
SQL programming
DB2 Universal Database for iSeries provides a wide range of support for Structured Query Language (SQL). The examples of SQL statements shown in this topic collection are based on the sample tables and assume that the following statements are true: v They are shown in the interactive SQL environment or they are written in ILE C or in COBOL. EXEC SQL and END-EXEC are used to delimit an SQL statement in a COBOL program. v Each SQL example is shown on several lines, with each clause of the statement on a separate line. v SQL keywords are highlighted. v Table names provided in the sample tables use the schema CORPDATA. Table names that are not found in the Sample Tables should use schemas you create. v Calculated columns are enclosed in parentheses, (), and brackets, []. v The SQL naming convention is used. v The APOST and APOSTSQL precompiler options are assumed although they are not the default options in COBOL. Character string literals within SQL and host language statements are delimited by single-quotation marks (). v A sort sequence of *HEX is used, unless otherwise noted. Whenever the examples vary from these assumptions, it is stated. Because this topic collection is for the application programmer, most of the examples are shown as if they were written in an application program. However, many examples can be slightly changed and run interactively by using interactive SQL. The syntax of an SQL statement, when using interactive SQL, differs slightly from the format of the same statement when it is embedded in a program. Note: By using the code examples, you agree to the terms of the Code license and disclaimer information on page 300. Related concepts Embedded SQL programming Related reference DB2 Universal Database for iSeries sample tables on page 281 These sample tables are referred to and used in the SQL programming and the SQL reference topic collections. SQL reference
| | | |
| v Information about allocated SQL descriptors was added in the Parameter markers on page 241, SQL | descriptor areas on page 231, Varying-list SELECT statements on page 230 and Processing SELECT statements and using a descriptor on page 229 topics. | | v More special registers were added in Special registers in SQL statements on page 46. | v Changes were made to Types of SQL statements on page 5. | v More statements that can be used with the SQL statement processor were added in Using the SQL statement processor on page 255. |
To find other information about whats new or changed this release, see the Memo to users.
Printable PDF
Use this to view and print a PDF of this information. To view or download the PDF version of this document, select SQL programming (about 3554KB).
Related reference Distributed relational database function and SQL on page 258 A distributed relational database consists of a set of SQL objects that are spread across interconnected computer systems.
SQL concepts
DB2 UDB for iSeries SQL consists of several main parts, such as SQL runtime support, precompilers, and interactive SQL. v SQL runtime support SQL run time parses SQL statements and runs any SQL statements. This support is part of the i5/OS licensed program, which allows applications that contain SQL statements to be run on systems where the IBM DB2 Query Manager and SQL Development Kit for iSeries licensed program is not installed. v SQL precompilers SQL precompilers support precompiling embedded SQL statements in host languages. The following languages are supported: ILE C ILE C++ ILE COBOL COBOL PL/I RPG III (part of RPG) ILE RPG The SQL host language precompilers prepare an application program containing SQL statements. The host language compilers then compile the precompiled host source programs. For more information about precompiling, see Preparing and running a program with SQL statements in the Embedded SQL programming information. The precompiler support is part of the DB2 Query Manager and SQL Development Kit for iSeries licensed program. SQL interactive interface SQL interactive interface allows you to create and run SQL statements. For more information about interactive SQL, see Using interactive SQL on page 244. Interactive SQL is part of the DB2 Query Manager and SQL Development Kit for iSeries licensed program. Run SQL Scripts The Run SQL Scripts window in iSeries Navigator allows you to create, edit, run, and troubleshoot scripts of SQL statements. Run SQL Scripts is a part of iSeries Navigator. Run SQL Statements (RUNSQLSTM) CL command The RUNSQLSTM command allows you to run a series of SQL statements that are stored in a source file. For more information about the RUNSQLSTM command, see Using the SQL statement processor on page 255. DB2 Query Manager for iSeries DB2 Query Manager for iSeries provides a prompt-driven interactive interface that allows you to create data, add data, maintain data, and run reports on the databases. Query Manager is part of the DB2 Query Manager and SQL Development Kit for iSeries licensed program. For more information, see
. Query Manager Use v SQL REXX interface The SQL REXX interface allows you to run SQL statements in a REXX procedure. For more information about using SQL statements in REXX procedures, see Coding SQL statements in REXX applications in the Embedded SQL Programming information. v SQL call level interface
SQL programming
DB2 UDB for iSeries supports the SQL call level interface. This allows users of any of the ILE languages to access SQL functions directly through bound calls to a service program that is provided by the system. Using the SQL call level interface, you can perform all the SQL functions without the need to precompile. This is a standard set of procedure calls to prepare SQL statements, run SQL statements, fetch rows of data, and even perform advanced functions, such as accessing the catalogs and binding program variables to output columns. For a complete description of all the available functions and their syntax, see SQL call level interface in the Database section of the i5/OS Information Center. Process Extended Dynamic SQL (QSQPRCED) API This application programming interface (API) provides an extended dynamic SQL capability. You can prepare SQL statements into an SQL package and run them by using this API. Statements that are prepared into a package by this API persist until the package or statement is explicitly dropped. For more information about the QSQPRCED API, see Process Extended Dynamic SQL (QSQPRCED) API. For general information about APIs, see Application programming interfaces. Syntax Check SQL Statement (QSQCHKS) API This API syntax checks SQL statements. For more information about the QSQCHKS API, see Syntax Check SQL Statement (QSQCHKS) API. For general information about APIs, see Application programming interfaces. DB2 Multisystem This feature of the operating system allows your data to be distributed across multiple systems. For more information, see DB2 Multisystem. DB2 Symmetric Multiprocessing This feature of the operating system provides the query optimizer with additional methods for retrieving data that include parallel processing. Symmetric multiprocessing (SMP) is a form of parallelism achieved on a single system where multiple processors (CPU and I/O processors) that share memory and disk resource work simultaneously toward achieving a single end result. This parallel processing means that the database manager can have more than one (or all) of the system processors working on a single query simultaneously. For more information, see Controlling parallel processing for queries in the Database Performance and Query Optimization topic collection.
Physical file. A set of records. Record. A set of fields. Field. One or more characters of related information of one data type. Logical file. A subset of fields and records of one or more physical files. SQL package. An object type that is used to run SQL statements. User Profile
Related concepts
SQL programming
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
SQL schema statements ALTER SEQUENCE ALTER TABLE COMMENT ON CREATE ALIAS CREATE DISTINCT TYPE CREATE FUNCTION CREATE INDEX CREATE PROCEDURE CREATE SCHEMA CREATE SEQUENCE CREATE TABLE CREATE TRIGGER CREATE VIEW DROP ALIAS DROP DISTINCT TYPE DROP FUNCTION DROP INDEX DROP PACKAGE DROP PROCEDURE DROP SEQUENCE DROP SCHEMA DROP TABLE DROP TRIGGER DROP VIEW GRANT DISTINCT TYPE GRANT FUNCTION GRANT PACKAGE GRANT PROCEDURE GRANT SEQUENCE GRANT TABLE LABEL ON RENAME REVOKE DISTINCT TYPE REVOKE FUNCTION REVOKE PACKAGE REVOKE PROCEDURE REVOKE SEQUENCE REVOKE TABLE SQL data change statements DELETE INSERT UPDATE
SQL data statements CLOSE DECLARE CURSOR DELETE FETCH FREE LOCATOR HOLD LOCATOR INSERT LOCK TABLE OPEN REFRESH TABLE SELECT INTO SET variable UPDATE VALUES INTO
SQL connection statements CONNECT DISCONNECT RELEASE SET CONNECTION SQL session statements DECLARE GLOBAL TEMPORARY TABLE SET CURRENT DEGREE SET ENCRYPTION PASSWORD SET PATH SET SCHEMA SET SESSION AUTHORIZATION
SQL transaction statements COMMIT RELEASE SAVEPOINT ROLLBACK SAVEPOINT SET TRANSACTION
| | | | | | | | | | | | | | | | | |
Dynamic SQL statements ALLOCATE DESCRIPTOR DEALLOCATE DESCRIPTOR DESCRIBE DESCRIBE INPUT DESCRIBE TABLE EXECUTE EXECUTE IMMEDIATE GET DESCRIPTOR PREPARE SET DESCRIPTOR
Embedded SQL host language statements BEGIN DECLARE SECTION DECLARE PROCEDURE DECLARE STATEMENT DECLARE VARIABLE END DECLARE SECTION GET DIAGNOSTICS INCLUDE SET OPTION SET RESULT SETS SIGNAL WHENEVER
SQL statements can operate on objects that are created by SQL as well as externally described physical files and single-format logical files. They do not refer to the interactive data definition utility (IDDU) dictionary definition for program-described files. Program-described files appear as a table with only a single column. Related concepts Data definition language on page 14 Data definition language (DDL) describes the portion of SQL that creates, alters, and deletes database objects. These database objects include schemas, tables, views, sequences, catalogs, indexes, and aliases. Data manipulation language on page 36 Data manipulation language (DML) describes the portion of SQL that manipulates or controls data. Related reference SQL reference
SQL objects
SQL objects are schemas, journals, catalogs, tables, aliases, views, indexes, constraints, triggers, sequences, stored procedures, user-defined functions, user-defined types, and SQL packages. SQL creates and maintains these objects as system objects.
SQL programming
Schemas
A schema provides a logical grouping of SQL objects. A schema consists of a library, a journal, a journal receiver, a catalog, and, optionally, a data dictionary. Tables, views, and system objects (such as programs) can be created, moved, or restored into any system library. All system files can be created or moved into an SQL schema if the SQL schema does not contain a data dictionary. If the SQL schema contains a data dictionary then: v Source physical files or nonsource physical files with one member can be created, moved, or restored into an SQL schema. v Logical files cannot be placed in an SQL schema because they cannot be described in the data dictionary. You can create and own many schemas. The term collection can be used synonymously with schema.
Catalogs
An SQL catalog is a collection of tables and views that describe tables, views, indexes, packages, procedures, functions, files, sequences, triggers, and constraints. This information is contained in a set of cross-reference tables in libraries QSYS and QSYS2. In each SQL schema there is a set of views built over the catalog tables that contains information about the tables, views, indexes, packages, files, and constraints in the schema. A catalog is automatically created when you create a schema. You cannot drop or explicitly change the catalog. Related reference Catalog
Creating and altering a materialized query table on page 20 A materialized query table is a table whose definition is based on the result of a query, and whose data is in the form of precomputed results that are taken from the table or tables on which the materialized query table definition is based.
Aliases
An alias is an alternate name for a table or view. You can use an alias to refer to a table or view in those cases where an existing table or view can be referred to. Additionally, aliases can be used to join table members. Related reference Alias
Views
A view appears like a table to an application program. However, a view contains no data and only logically represents one or more tables over which it is created. A view can contain all the columns and rows of the given tables or a subset of them. The columns can be arranged differently in a view than they are in the tables from which they are taken. A view in SQL is a special form of a nonkeyed logical file. Related reference Views
Indexes
An SQL index is a subset of the data in the columns of a table that are logically arranged in either ascending or descending order. Each index contains a separate arrangement. These arrangements are used for ordering (ORDER BY clause), grouping (GROUP BY clause), and joining. An SQL index is a keyed logical file. The index is used by the system for faster data retrieval. Creating an index is optional. You can create any number of indexes. You can create or drop an index at any time. The index is automatically maintained by the system. However, because the indexes are maintained by the system, a large number of indexes can adversely affect the performance of the applications that change the table. Related concepts Creating an index strategy
Constraints
A constraint is a rule enforced by the database manager to limit the values that can be inserted, deleted, or updated in a table. DB2 UDB for iSeries supports the following constraints: v Unique constraints A unique constraint is the rule that the values of the key are valid only if they are unique. You can create a unique constraint using the CREATE TABLE or ALTER TABLE statement. Although the CREATE INDEX statement can create a unique index that also guarantees uniqueness, such an index is not a constraint. Unique constraints are enforced during the execution of INSERT and UPDATE statements. A PRIMARY KEY constraint is a form of the UNIQUE constraint. The difference is that a PRIMARY KEY cannot contain any nullable columns. v Referential constraints A referential constraint is the rule that the values of the foreign key are valid only if one of the following conditions is met:
SQL programming
They appear as values of a parent key. Some component of the foreign key is null. Referential constraints are enforced during the execution of INSERT, UPDATE, and DELETE statements. v Check constraints A check constraint is the rule that limits the values allowed in a column or group of columns. You can create a check constraint using the CREATE TABLE or ALTER TABLE statement. Check constraints are enforced during the execution of INSERT and UPDATE statements. To satisfy the constraint, each row of data inserted or updated in the table must make the specified condition either TRUE or unknown (because of a null value). Related reference Constraints on page 115 DB2 UDB for iSeries supports unique, referential, and check constraints.
Triggers
A trigger is a set of actions that runs automatically whenever a specified event occurs to a specified table or view. An event can be an insert, an update, a delete, or a read operation. A trigger can run either before or after the event. DB2 UDB for iSeries supports SQL insert, update, and delete triggers and external triggers. Related tasks Triggering automatic events in your database
Stored procedures
A stored procedure is a program that can be called with the SQL CALL statement. DB2 UDB for iSeries supports external stored procedures and SQL procedures. An external stored procedure can be any system program, service program, or REXX procedure. It cannot be a System/36 program or procedure. An SQL procedure is defined entirely in SQL and can contain SQL statements, including SQL control statements. Related concepts Stored procedures on page 119 A procedure (often called a stored procedure) is a program that can be called to perform operations. A procedure can include both host language statements and SQL statements. Procedures in SQL provide the same benefits as procedures in a host language.
Sequences
A sequence is a data area object that provides a quick and easy way of generating unique numbers. You can use a sequence to replace an identity column or a user-generated numeric column. A sequence has uses similar to these alternatives. Related reference Creating and using sequences on page 22 Sequences are similar to identity columns in that they both generate unique values. However, sequences are objects that are independent of any tables. You can use sequences to generate values quickly and easily.
User-defined functions
A user-defined function is a program that can be called like any built-in functions. DB2 UDB for iSeries supports external functions, SQL functions, and sourced functions. An external function can be any system ILE program or service program. An SQL function is defined entirely in SQL
10
and can contain SQL statements, including SQL control statements. A sourced function is built over any built-in or any existing user-defined function. You can create a scalar function or a table function as either an SQL or an external function. Related concepts Using user-defined functions on page 149 In writing SQL applications, you can implement some actions or operations as a user-defined function (UDF) or as a subroutine in your application. Although it might appear easier to implement new operations as subroutines, you might want to consider the advantages of using a UDF instead.
User-defined types
A user-defined type is a distinct data type that you can define independently of the data types that are provided by the database management system. Distinct data types map on a one-to-one basis to existing database types. Related concepts User-defined distinct types on page 202 A user-defined distinct type (UDT) is a mechanism to extend DB2 capabilities beyond the built-in data types that are available.
SQL packages
An SQL package is an object that contains the control structure produced when the SQL statements in an application program are bound to a remote relational database management system (DBMS). The DBMS uses the control structure to process SQL statements encountered while running the application program. SQL packages are created when a relational database name (RDB parameter) is specified on a Create SQL (CRTSQLxxx) command and a program object is created. Packages can also be created with the Create SQL Package (CRTSQLPKG) command. Note: The xxx in this command refers to the host language indicators: CI for ILE C, CPPI for ILE C++, CBL for COBOL, CBLI for ILE COBOL, PLI for PL/I, RPG for RPG/400, and RPGI for ILE RPG. SQL packages can also be created with the Process Extended Dynamic SQL (QSQPRCED) API. The SQL packages mentioned within this topic collection refer exclusively to distributed program SQL packages. The QSQPRCED API uses SQL packages to provide extended dynamic SQL support. Related reference Distributed relational database function and SQL on page 258 A distributed relational database consists of a set of SQL objects that are spread across interconnected computer systems. Process Extended Dynamic SQL (QSQPRCED) API
11
With a nondistributed non-ILE DB2 UDB for iSeries program, you must manage only the original source and the resulting program. The following figure shows the objects involved and the steps that happen during the precompile and compile processes for a nondistributed non-ILE DB2 UDB for iSeries program:
With a nondistributed ILE DB2 UDB for iSeries program, you might need to manage the original source, the modules, and the resulting program or service program. The following figure shows the objects involved and the steps that happen during the precompile and compile processes for a nondistributed ILE DB2 UDB for iSeries program when OBJTYPE(*PGM) is specified on the precompile command:
With a distributed non-ILE DB2 UDB for iSeries program, you must manage the original source, the resulting program, and the resulting package. The following figure shows the objects and the steps that occur during the precompile and compile processes for a distributed non-ILE DB2 UDB for iSeries program:
With a distributed ILE DB2 UDB for iSeries program, you must manage the original source, module objects, the resulting program or service program, and the resulting packages. An SQL package can be created for each distributed module in a distributed ILE program or service program. The following figure shows the objects and the steps that occur during the precompile and compile processes for a distributed ILE DB2 UDB for iSeries program:
12
Note: The access plans associated with the DB2 UDB for iSeries distributed program object are not created until the program is run locally. Related tasks Preparing and running a program with SQL statements
Program
A program is an object that is created as a result of the compilation process for non-ILE compilations or as a result of the bind process for ILE compilations. An access plan is a set of internal structures and information that tells SQL how to run an embedded SQL statement most effectively. It is created only when the program has been successfully created. Access plans are not created during program creation for SQL statements if the statements: v Refer to a table or view that cannot be found v Refer to a table or view to which you are not authorized The access plans for such statements are created when the program is run. If, at that time, the table or view still cannot be found or you are still not authorized, a negative SQLCODE is returned. Access plans are stored and maintained in the program object for non-distributed SQL programs and in the SQL package for distributed SQL programs.
SQL package
An SQL package contains the access plans for a distributed SQL program. An SQL package is an object that is created when: v You successfully create a distributed SQL program by specifying the relational database (RDB) parameter on the CREATE SQL (CRTSQLxxx) commands.
SQL programming
13
v You run the Create SQL Package (CRTSQLPKG) command. When a distributed SQL program is created, the name of the SQL package and an internal consistency token are saved in the program. They are used at run time to find the SQL package and to verify that the SQL package is correct for this program. Because the name of the SQL package is critical for running distributed SQL programs, an SQL package cannot be: v Moved v Renamed v Duplicated v Restored to a different library
Module
A module is an Integrated Language Environment (ILE) object that you create by compiling source code using the Create Module (CRTxxxMOD) command (or any of the Create Bound Program (CRTBNDxxx) commands, where xxx is C, CBL, CPP, or RPG). You can run a module only if you use the Create Program (CRTPGM) command to bind it into a program. You typically bind several modules together, but you can bind a module by itself. Modules contain information about the SQL statements; however, the SQL access plans are not created until the modules are bound into either a program or service program. Related reference Create Program (CRTPGM) command
Service program
A service program is an Integrated Language Environment (ILE) object that provides a means of packaging externally supported callable routines (functions or procedures) into a separate object. Bound programs and other service programs can access these routines by resolving their imports to the exports provided by a service program. The connections to these services are made when the calling programs are created. This improves call performance to these routines without including the code in the calling program.
Creating a schema
A schema provides a logical grouping of SQL objects. To create a schema, use the CREATE SCHEMA statement. A schema consists of a library, a journal, a journal receiver, a catalog, and optionally, a data dictionary. Tables, views, and system objects (such as programs) can be created, moved, or restored into any system libraries. All system files can be created or moved into an SQL schema if the SQL schema does not contain a data dictionary. If the SQL schema contains a data dictionary then: v Source physical files or nonsource physical files with one member can be created, moved, or restored into an SQL schema.
14
v Logical files cannot be placed in an SQL schema because they cannot be described in the data dictionary. You can create and own many schemas. You can create a schema using the CREATE SCHEMA statement. For example, create a schema called DBTEMP:
CREATE SCHEMA DBTEMP
Creating a table
A table can be visualized as a two-dimensional arrangement of data that consists of rows and columns. To create a table, use the CREATE TABLE statement. The row is the horizontal part containing one or more columns. The column is the vertical part containing one or more rows of data of one data type. All data for a column must be of the same type. A table in SQL is a keyed or non-keyed physical file. You can create a table using the CREATE TABLE statement. The definition must include its name and the names and attributes of its columns. The definition can include other attributes of the table, such as the primary key. Example: Given that you have administrative authority, create a table named INVENTORY with the following columns: v Part number: Integer between 1 and 9999, and must not be null v Description: Character of length 0 to 24 v Quantity on hand: Integer between 0 and 100000 The primary key is PARTNO.
CREATE TABLE INVENTORY (PARTNO SMALLINT NOT NULL, DESCR VARCHAR(24 ), QONHAND INT, PRIMARY KEY(PARTNO))
To make this key a unique key, replace the keyword PRIMARY with UNIQUE. You can remove a constraint using the same ALTER TABLE statement:
ALTER TABLE CORPDATA.DEPARTMENT DROP PRIMARY KEY (DEPTNO)
SQL programming
15
16
v Delete and update rules that specify the action taken with respect to dependent rows when the parent row is deleted or updated. Optionally, you can specify a name for the constraint. If a name is not specified, one is automatically generated. After a referential constraint is defined, the system enforces the constraint on every INSERT, DELETE, and UPDATE operation performed through SQL or any other interface including iSeries Navigator, CL commands, utilities, or high-level language statements. Related reference CREATE TABLE ALTER TABLE Example: Adding referential constraints: You define a referential constraint that every department number in the sample employee table must appear in the department table. The referential constraint ensures that every employee belongs to an existing department. The following SQL statements create the CORPDATA.DEPARTMENT and CORPDATA.EMPLOYEE tables with those constraint relationships defined.
CREATE TABLE CORPDATA.DEPARTMENT (DEPTNO CHAR(3) NOT NULL PRIMARY KEY, DEPTNAME VARCHAR(29) NOT NULL, MGRNO CHAR(6), ADMRDEPT CHAR(3) NOT NULL CONSTRAINT REPORTS_TO_EXISTS REFERENCES CORPDATA.DEPARTMENT (DEPTNO) ON DELETE CASCADE) CREATE TABLE CORPDATA.EMPLOYEE (EMPNO CHAR(6) NOT NULL PRIMARY KEY, FIRSTNME VARCHAR(12) NOT NULL, MIDINIT CHAR(1) NOT NULL, LASTNAME VARCHAR(15) NOT NULL, WORKDEPT CHAR(3) CONSTRAINT WORKDEPT_EXISTS REFERENCES CORPDATA.DEPARTMENT (DEPTNO) ON DELETE SET NULL ON UPDATE RESTRICT, PHONENO CHAR(4), HIREDATE DATE, JOB CHAR(8), EDLEVEL SMALLINT NOT NULL, SEX CHAR(1), BIRTHDATE DATE, SALARY DECIMAL(9,2), BONUS DECIMAL(9,2), COMM DECIMAL(9,2), CONSTRAINT UNIQUE_LNAME_IN_DEPT UNIQUE (WORKDEPT, LASTNAME))
In this case, the DEPARTMENT table has a column of unique department numbers (DEPTNO) which functions as a primary key, and is a parent table in two constraint relationships: REPORTS_TO_EXISTS is a self-referencing constraint in which the DEPARTMENT table is both the parent and the dependent in the same relationship. Every non-null value of ADMRDEPT must match a value of DEPTNO. A department must report to an existing department in the database. The DELETE CASCADE rule indicates that if a row with a DEPTNO value n is deleted, every row in the table for which the ADMRDEPT is n is also deleted.
SQL programming
17
WORKDEPT_EXISTS establishes the EMPLOYEE table as a dependent table, and the column of employee department assignments (WORKDEPT) as a foreign key. Thus, every value of WORKDEPT must match a value of DEPTNO. The DELETE SET NULL rule says that if a row is deleted from DEPARTMENT in which the value of DEPTNO is n, then the value of WORKDEPT in EMPLOYEE is set to null in every row in which the value was n. The UPDATE RESTRICT rule says that a value of DEPTNO in DEPARTMENT cannot be updated if there are values of WORKDEPT in EMPLOYEE that match the current DEPTNO value. Constraint UNIQUE_LNAME_IN_DEPT in the EMPLOYEE table causes LASTNAME to be unique within a department. While this constraint is unlikely, it illustrates how a constraint made up of several columns can be defined at the table level.
Check pending
Referential constraints and check constraints can be in a check pending state, where potential violations of the constraints exist. For referential constraints, a violation occurs when potential mismatches exist between parent and foreign keys. For check constraints, a violation occurs when potential values exist in columns that are limited by the check constraint. When the system determines that a constraint might have been violated (such as after a restore operation), the constraint is marked as check pending. When this happens, restrictions are placed on the use of tables involved in the constraint. For referential constraints, the following restrictions apply: v No input or output operations are allowed on the dependent file. v Only read and insert operations are allowed on the parent file. When a check constraint is in check pending, the following restrictions apply: v Read operations are not allowed on the file. v Insert and update operations are allowed and the constraint is enforced. To get a constraint out of check pending, follow these steps: 1. Disable the relationship with the Change Physical File Constraint (CHGPFCST) CL command. 2. Correct the key (foreign, parent, or both) data for referential constraints or column data for check constraints. 3. Enable the constraint again with the CHGPFCST CL command. You can identify the rows that are in violation of the constraint with the Display Check Pending Constraint (DSPCPCST) CL command. Related concepts Check pending status in referential constraints
18
Related tasks Working with constraints that are in check pending status
If the specified table or view contains an identity column, you must specify INCLUDING IDENTITY on the CREATE TABLE statement if you want the identity column to exist in the new table. The default behavior for CREATE TABLE is EXCLUDING IDENTITY. The WITH NO DATA clause indicates that the column definitions are to be copied without the data. If you wanted to include the data in the new table, EMPLOYEE3, include the WITH DATA clause. If the specified query includes a non-SQL created physical file or logical file, any non-SQL result attributes are removed. Related concepts
SQL programming
19
Retrieving data using the SELECT statement on page 36 The SELECT statement tailors your query to gather data. You can use the SELECT statement to retrieve a specific row or retrieve data in a specific way. Related reference CREATE TABLE
This materialized query table specifies that the table is not populated at the time that it is created by using the DATA INITIALLY DEFERRED clause. REFRESH DEFERRED indicates that changes made to TRANS are not reflected in STRANS. Additionally, this table is maintained by the user, enabling the user to use ALTER, INSERT, DELETE, and UPDATE statements. To populate the materialized query table or refresh the table after it has been populated, use the REFRESH TABLE statement. This causes the query associated with the materialized query table to be run and causes the table to be filled with the results of the query. To populate the STRANS table, run the following statement:
REFRESH TABLE STRANS
You can create a materialized query table from an existing base table as long as the result of the select-statement provides a set of columns that match the columns in the existing table (same number of columns and compatible column definitions). For example, create a table TRANSCOUNT. Then, change the base table TRANSCOUNT into a materialized query table: To create the table:
CREATE TABLE TRANSCOUNT (ACCTID SMALLINT NOT NULL, LOCID SMALLINT, YEAR DATE CNT INTEGER)
20
GROUP BY ACCTID, LOCID, YEAR ) DATA INITIALLY DEFERRED REFRESH DEFERRED MAINTAINED BY USER
Finally, you can change a materialized query table back to a base table. For example:
ALTER TABLE TRANSCOUNT DROP MATERIALIZED QUERY
In this example, the table TRANSCOUNT is not dropped, but it is no longer a materialized query table. Related concepts Tables, rows, and columns on page 8 A table is a two-dimensional arrangement of data that consists of rows and columns.
This table is created in QTEMP. To reference the table using a schema name, use either SESSION or QTEMP. You can issue SELECT, INSERT, UPDATE, and DELETE statements against this table, the same as any other table. You can drop this table by issuing the DROP TABLE statement:
DROP TABLE ORDERS
SQL programming
21
This column is defined with a starting value of 500, incremented by 1 for every new row inserted, and will recycle when the maximum value is reached. In this example, the maximum value for the identity column is the maximum value for the data type. Because the data type is defined as SMALLINT, the range of values that can be assigned to ORDERNO is from 500 to 32 767. When this column value reaches 32 767, it will restart at 500 again. If 500 is still assigned to a column, and a unique key is specified on the identity column, a duplicate key error is returned. The next insert operation will attempt to use 501. If you do not have a unique key specified for the identity column, 500 is used again, regardless of how many times it appears in the table. For a larger range of values, specify the column to be data type INTEGER or even BIGINT. If you want the value of the identity column to decrease, specify a negative value for the INCREMENT option. It is also possible to specify the exact range of numbers by using MINVALUE and MAXVALUE. You can modify the attributes of an existing identity column using the ALTER TABLE statement. For example, you want to restart the identity column with a new value:
ALTER TABLE ORDER ALTER COLUMN ORDERNO RESTART WITH 1
The column ORDERNO remains as a SMALLINT column, but the identity attribute is dropped. The system will no longer generate values for this column. Related reference Comparison of identity columns and sequences on page 24 While identity columns and sequences are similar in many ways, there are also differences. Inserting values into an identity column on page 82 You can insert a value into an identity column or allow the system to insert a value for you. Updating an identity column on page 86 You can update the value in an identity column to a specified value or have the system generate a new value.
Using ROWID
Using ROWID is another way to have the system assign a unique value to a column. ROWID is similar to identity columns. But rather than being an attribute of a numeric column, it is a separate data type. To create a table similar to the identity column example:
CREATE TABLE ORDERS (ORDERNO ROWID GENERATED ALWAYS, SHIPPED_TO VARCHAR (36) , ORDER_DATE DATE)
22
Sequences are not tied to a column in a table and are accessed separately. Additionally, they are not treated as any part of a transactions unit of work. You create a sequence using the CREATE SEQUENCE statement. For an example similar to the identity column example, create a sequence ORDER_SEQ:
CREATE SEQUENCE ORDER_SEQ START WITH 500 INCREMENT BY 1 MAXVALUE 1000 CYCLE CACHE 24
This sequence is defined with a starting value of 500, incremented by 1 for every use, and recycles when the maximum value is reached. In this example, the maximum value for the sequence is 1000. When this value reaches 1000, it will restart at 500 again. After this sequence is created, you can insert values into a column using the sequence. For example, insert the next value of the sequence ORDER_SEQ into a table ORDERS with columns ORDERNO and CUSTNO. First, create the table ORDERS:
CREATE TABLE ORDERS (ORDERNO SMALLINT NOT NULL, CUSTNO SMALLINT);
In this example, the next value for sequence ORDER is inserted into the ORDERNO column. Issue the INSERT statement again. Then run the SELECT statement.
Table 3. Results for SELECT from table ORDERS ORDERNO 500 501 CUSTNO 12 12
You can also insert the previous value for the sequence ORDER by using the PREVIOUS VALUE expression. You can use NEXT VALUE and PREVIOUS VALUE in the following expressions: v Within the select-clause of a SELECT statement or SELECT INTO statement as long as the statement does not contain a DISTINCT keyword, a GROUP BY clause, an ORDER BY clause, a UNION keyword, an INTERSECT keyword, or an EXCEPT keyword v Within a VALUES clause of an INSERT statement v Within the select-clause of the fullselect of an INSERT statement
SQL programming
23
v Within the SET clause of a searched or positioned UPDATE statement, though NEXT VALUE cannot be specified in the select-clause of the subselect of an expression in the SET clause You can alter a sequence by issuing the ALTER SEQUENCE statement. Sequences can be altered in the following ways: v Restarting the sequence v Changing the increment between future sequence values v Setting or eliminating the minimum or maximum values v Changing the number of cached sequence numbers v Changing the attribute that determines whether the sequence can cycle or not v Changing whether sequence numbers must be generated in order of request For example, change the increment of values of sequence ORDER from 1 to 5:
ALTER SEQUENCE ORDER_SEQ INCREMENT BY 5
After this change is complete, run the INSERT statement again and then the SELECT statement. Now the table contains the following columns.
Table 4. Results for SELECT from table ORDERS ORDERNO 500 501 528 CUSTNO 12 12 12
Notice that the next value that the sequence uses is a 528. At first glance, this number appears to be incorrect. However, look at the events that lead up to this assignment. First, when the sequence was originally create, a cache value of 24 was assigned. The system assigns the first 24 values for this cache. Next, the sequence was altered. When the ALTER SEQUENCE statement is issued, the system drops the assigned values and starts up again with the next available value; in this case the original 24 that was cached, plus the next increment, 5. If the original CREATE SEQUENCE statement did not have the CACHE clause, the system automatically assigns a default cache value of 20. If that sequence was altered, then the next available value is 25. Related concepts Sequences on page 10 A sequence is a data area object that provides a quick and easy way of generating unique numbers.
24
v The IDENTITY_VAL_LOCAL function can be used to see the most recently assigned value for an identity column. A sequence has the following characteristics: v A sequence is a system object of type *DTAARA that is not tied to a table. v A sequence generates sequential values that can be used in any SQL statement. v There are two expressions used to retrieve the next values in the sequence and to look at the previous value assigned for the sequence. The PREVIOUS VALUE expression returns the most recently generated value for the specified sequence for a previous statement within the current session. The NEXT VALUE expression returns the next value for the specified sequence. The use of these expressions allows the same value to be used across several SQL statements within several tables. While these are not all of the characteristics of identity columns and sequences, these characteristics can help you determine which to use depending on your database design and the applications that use the database. Related reference Creating and altering an identity column on page 21 Every time a row is added to a table with an identity column, the identity column value for the new row is generated by the system.
After these statements are run, the table named DEPARTMENT displays the text description as Department Structure Table and the column named ADMRDEPT displays the heading Reports to Dept. The label for tables, views, indexes, sequence, SQL packages, and column text cannot be more than 50 characters and the label for column headings cannot be more than 60 characters (blanks included). The following are examples of LABEL ON statements for column headings: This LABEL ON statement provides column heading 1 and column heading 2:
*...+....1....+....2....+....3....+....4....+....5....+....6..* LABEL ON COLUMN CORPDATA.EMPLOYEE.EMPNO IS Employee Number
This LABEL ON statement provides three levels of column headings for the SALARY column:
*...+....1....+....2....+....3....+....4....+....5....+....6..* LABEL ON COLUMN CORPDATA.EMPLOYEE.SALARY IS Yearly Salary (in dollars)
This LABEL ON statement provides a DBCS column heading with two levels specified:
SQL programming
25
This LABEL ON statement provides the column text for the EDLEVEL column:
*...+....1....+....2....+....3....+....4....+....5....+....6..* LABEL ON COLUMN CORPDATA.EMPLOYEE.EDLEVEL TEXT IS Number of years of formal education
Adding a column
When you add a new column to a table, the column is initialized with its default value for all existing rows. If NOT NULL is specified, a default value must also be specified.
26
You can add a column to a table using the ADD COLUMN clause of the SQL ALTER TABLE statement. The altered table may consist of up to 8000 columns. The sum of the byte counts of the columns must not be greater than 32766 or, if a VARCHAR or VARGRAPHIC column is specified, 32740. If a LOB column is specified, the sum of record data byte counts of the columns must not be greater than 15 728 640. Related reference ALTER TABLE
Changing a column
You can change a column definition in a table using the ALTER COLUMN clause of the ALTER TABLE statement. When you change the data type of an existing column, the old and new attributes must be compatible. You can always change a character, graphic, or binary column from fixed length to varying length or LOB; or from varying length or LOB to fixed length. When you convert to a data type with a longer length, data is padded with the appropriate pad character. When you convert to a data type with a shorter length, data might be lost because of truncation. An inquiry message prompts you to confirm the request. If you have a column that does not allow the null value and you want to change it to now allow the null value, use the DROP NOT NULL clause. If you have a column that allows the null value and you want to prevent the use of null values, use the SET NOT NULL clause. If any of the existing values in that column are the null value, the ALTER TABLE will not be performed and an SQLCODE of -190 will result. Related reference Allowable conversions of data types When you change the data type of an existing column, the old and new attributes must be compatible. Related information ALTER TABLE
27
Table 5. Allowable conversions (continued) From data type DBCS-open DBCS-either DBCS-either DBCS-either DBCS-only DBCS-only DBCS-only DBCS graphic UCS-2 or UTF-16 graphic UCS-2 or UTF-16 graphic UCS-2 or UTF-16 graphic distinct type source type To data type UCS-2 or UTF-16 graphic Character DBCS-open UCS-2 or UTF-16 graphic DBCS-open DBCS graphic UCS-2 or UTF-16 graphic UCS-2 or UTF-16 graphic Character DBCS-open DBCS graphic source type distinct type
When you change an existing column, only the attributes that you specify are changed. All other attributes remain unchanged. For example, you have a table with the following table definition:
CREATE TABLE EX1 (COL1 CHAR(10) DEFAULT COL1, COL2 VARCHAR(20) ALLOCATE(10) CCSID 937, COL3 VARGRAPHIC(20) ALLOCATE(10) NOT NULL WITH DEFAULT)
After you run the following ALTER TABLE statement, COL2 still has an allocated length of 10 and CCSID 937, and COL3 still has an allocated length of 10.
ALTER TABLE EX1 ALTER COLUMN COL2 SET DATA TYPE VARCHAR(30) ALTER COLUMN COL3 DROP NOT NULL
Related reference Changing a column on page 27 You can change a column definition in a table using the ALTER COLUMN clause of the ALTER TABLE statement.
Deleting a column
You can delete a column using the DROP COLUMN clause of the ALTER TABLE statement. Dropping a column deletes that column from the table definition. If CASCADE is specified, any views, indexes, and constraints dependent on that column will also be dropped. If RESTRICT is specified, and any views, indexes, or constraints are dependent on the column, the column will not be dropped and SQLCODE of -196 will be issued.
ALTER TABLE DEPT DROP COLUMN NUMDEPT
28
Drop materialized query table Drop partition information Drop columns for which the RESTRICT option was specified Alter column definitions (this includes adding columns and dropping columns for which the CASCADE option was specified) 6. Add or alter materialized query table 7. Add partitioning to a table 8. Add constraints 2. 3. 4. 5. Within each of these steps, the order in which you specify the clauses is the order in which they are performed, with one exception. If any columns are being dropped, that operation is logically done before any column definitions are added or altered, in case record length is increased as a result of the ALTER TABLE statement.
When alias MYLIB.MYMBR2_ALIAS is specified on the following insert statement, the values are inserted into member MBR2 in MYLIB.MYFILE:
INSERT INTO MYLIB.MYMBR2_ALIAS VALUES(ABC, 6)
Alias names can also be specified on DDL statements. Assume that MYLIB.MYALIAS is an alias for table MYLIB.MYTABLE. The following DROP statement drops table MYLIB.MYTABLE:
DROP TABLE MYLIB.MYALIAS
If you really want to drop the alias name instead, specify the ALIAS keyword on the drop statement:
DROP ALIAS MYLIB.MYALIAS
SQL programming
29
After you create the view, you can use it in SQL statements just like a table. You can also change the data in the base table through the view. The following SELECT statement displays the contents of EMP_MANAGERS:
SELECT * FROM CORPDATA.EMP_MANAGERS
If the select list contains elements other than columns such as expressions, functions, constants, or special registers, and the AS clause was not used to name the columns, a column list must be specified for the view. In the following example, the columns of the view are LASTNAME and YEARSOFSERVICE.
CREATE VIEW CORPDATA.EMP_YEARSOFSERVICE (LASTNAME, YEARSOFSERVICE) AS SELECT LASTNAME, YEAR (CURRENT DATE - HIREDATE) FROM CORPDATA.EMPLOYEE
Because the results of querying this view change as the current year changes, they are not included here. You can also define the previous view by using the AS clause in the select list to name the columns in the view. For example:
CREATE VIEW CORPDATA.EMP_YEARSOFSERVICE AS SELECT LASTNAME, YEARS (CURRENT_DATE - HIREDATE) AS YEARSOFSERVICE FROM CORPDATA.EMPLOYEE
Using the UNION keyword, you can combine two or more subselects to form a single view. For example:
CREATE VIEW D11_EMPS_PROJECTS AS (SELECT EMPNO FROM CORPDATA.EMPLOYEE WHERE WORKDEPT = D11 UNION SELECT EMPNO FROM CORPDATA.EMPPROJACT WHERE PROJNO = MA2112 OR PROJNO = MA2113 OR PROJNO = AD3111)
30
Table 6. Results of creating a view as UNION (continued) EMPNO 000170 000180 000190 000200 000210 000220 000230 000240 200170 200220
Views are created with the sort sequence in effect at the time the CREATE VIEW statement is run. The sort sequence applies to all character, or UCS-2 or UTF-16 graphic comparisons in the CREATE VIEW statement subselect. You can also create views using the WITH CHECK OPTION clause to specify the level of checking when data is inserted or updated through the view. Related concepts Retrieving data using the SELECT statement on page 36 The SELECT statement tailors your query to gather data. You can use the SELECT statement to retrieve a specific row or retrieve data in a specific way. Sort sequences and normalization in SQL on page 99 A sort sequence defines how characters in a character set relate to each other when they are compared or ordered. Normalization allows you to compare strings that contain combining characters. Related reference Using the UNION keyword to combine subselects on page 68 Using the UNION keyword, you can combine two or more subselects to form a fullselect. CREATE VIEW
31
The WITH CASCADED CHECK OPTION clause specifies that every row that is inserted or updated through a view must conform to the definition of the view. In addition, the search conditions of all dependent views are checked when a row is inserted or updated. If a row does not conform to the definition of the view, that row cannot be retrieved through the view. For example, consider the following updatable view:
CREATE VIEW V1 AS SELECT COL1 FROM T1 WHERE COL1 > 10
Because no WITH CHECK OPTION is specified, the following INSERT statement is successful even though the value being inserted does not meet the search condition of the view.
INSERT INTO V1 VALUES (5)
Create another view over V1, specifying the WITH CASCADED CHECK OPTION clause:
CREATE VIEW V2 AS SELECT COL1 FROM V1 WITH CASCADED CHECK OPTION
The following INSERT statement fails because it produces a row that does not conform to the definition of V2:
INSERT INTO V2 VALUES (5)
The following INSERT statement fails only because V3 is dependent on V2, and V2 has a WITH CASCADED CHECK OPTION.
INSERT INTO V3 VALUES (5)
However, the following INSERT statement is successful because it conforms to the definition of V2. Because V3 does not have a WITH CASCADED CHECK OPTION, it does not matter that the statement does not conform to the definition of V3.
INSERT INTO V3 VALUES (200)
WITH LOCAL CHECK OPTION: The WITH LOCAL CHECK OPTION clause is identical to the WITH CASCADED CHECK OPTION clause except that you can update a row so that it can no longer be retrieved through the view. This can happen only when the view is directly or indirectly dependent on a view that was defined with no WITH CHECK OPTION clause. For example, consider the same updatable view used in the previous example:
CREATE VIEW V1 AS SELECT COL1 FROM T1 WHERE COL1 > 10
Create second view over V1, this time specifying WITH LOCAL CHECK OPTION:
CREATE VIEW V2 AS SELECT COL1 FROM V1 WITH LOCAL CHECK OPTION
The same INSERT statement that failed in the previous CASCADED CHECK OPTION example succeeds now because V2 does not have any search conditions, and the search conditions of V1 do not need to be checked since V1 does not specify a check option.
INSERT INTO V2 VALUES (5)
32
The following INSERT is successful again because the search condition on V1 is not checked due to the WITH LOCAL CHECK OPTION on V2, versus the WITH CASCADED CHECK OPTION in the previous example.
INSERT INTO V3 VALUES (5)
The difference between LOCAL and CASCADED CHECK OPTION lies in how many of the dependent views search conditions are checked when a row is inserted or updated. v WITH LOCAL CHECK OPTION specifies that the search conditions of only those dependent views that have the WITH LOCAL CHECK OPTION or WITH CASCADED CHECK OPTION are checked when a row is inserted or updated. v WITH CASCADED CHECK OPTION specifies that the search conditions of all dependent views are checked when a row is inserted or updated. Example: Cascaded check option: This example shows how the check option is enforced on a number of dependent views that are defined with or without a check option. Use the following table and views:
CREATE TABLE T1 (COL1 CHAR(10)) CREATE VIEW V1 AS SELECT COL1 FROM T1 WHERE COL1 LIKE A% CREATE VIEW V2 AS SELECT COL1 FROM V1 WHERE COL1 LIKE %Z WITH LOCAL CHECK OPTION CREATE VIEW V3 AS SELECT COL1 FROM V2 WHERE COL1 LIKE AB% CREATE VIEW V4 AS SELECT COL1 FROM V3 WHERE COL1 LIKE %YZ WITH CASCADED CHECK OPTION CREATE VIEW V5 AS SELECT COL1 FROM V4 WHERE COL1 LIKE ABC%
Different search conditions are going to be checked depending on which view is being operated on with an INSERT or UPDATE statement. v If V1 is operated on, no conditions are checked because V1 does not have a WITH CHECK OPTION specified. v If V2 is operated on, COL1 must end in the letter Z, but it doesnt need to start with the letter A. This is because the check option is LOCAL, and view V1 does not have a check option specified. v If V3 is operated on, COL1 must end in the letter Z, but it does not need to start with the letter A. V3 does not have a check option specified, so its own search condition must not be met. However, the search condition for V2 must be checked because V3 is defined on V2, and V2 has a check option. v If V4 is operated on,
SQL programming
33
COL1 must start with AB and must end with YZ. Because V4 has the WITH CASCADED CHECK OPTION specified, every search condition for every view on which V4 is dependent must be checked. v If V5 is operated on, COL1 must start with AB, but not necessarily ABC. This is because V5 does not specify a check option, so its own search condition does not need to be checked. However, because V5 is defined on V4, and V4 had a cascaded check option, every search condition for V4, V3, V2, and V1 must be checked. That is, COL1 must start with AB and end with YZ. If V5 were created WITH LOCAL CHECK OPTION, operating on V5 means that COL1 must start with ABC and end with YZ. The LOCAL CHECK OPTION adds the additional requirement that the third character must be a C.
Creating indexes
You can use indexes to sort and select data. In addition, indexes help the system retrieve data faster for better query performance. Use the CREATE INDEX statement to create indexes. The following example creates an index over the column LASTNAME in the CORPDATA.EMPLOYEE table:
CREATE INDEX CORPDATA.INX1 ON CORPDATA.EMPLOYEE (LASTNAME)
You can create any number of indexes. However, because the indexes are maintained by the system, a large number of indexes can adversely affect performance. One type of index, the encoded vector index (EVI), allows for faster scans that can be more easily processed in parallel. If an index is created that has exactly the same attributes as an existing index, the new index shares the existing indexes binary tree. Otherwise, another binary tree is created. If the attributes of the new index are exactly the same as another index, except that the new index has fewer columns, another binary tree is still created. It is still created because the extra columns prevent the index from being used by cursors or UPDATE statements that update those extra columns. Indexes are created with the sort sequence in effect at the time the CREATE INDEX statement is run. The sort sequence applies to all SBCS character fields, or UCS-2 or UTF-16 graphic fields of the index. Related concepts Sort sequences and normalization in SQL on page 99 A sort sequence defines how characters in a character set relate to each other when they are compared or ordered. Normalization allows you to compare strings that contain combining characters. Creating an index strategy Related reference CREATE INDEX
34
The result of the previous sample statement is a row of information for each column in the table. Some of the information is not visible because the width of the information is wider than the display screen. For more information about each column, specify a select-statement like this:
SELECT COLUMN_NAME, TABLE_NAME, DATA_TYPE, LENGTH, HAS_DEFAULT FROM CORPDATA.SYSCOLUMNS WHERE TABLE_NAME = DEPARTMENT
In addition to the column name for each column, the select-statement shows: v The name of the table that contains the column v The data type of the column v The length attribute of the column v If the column allows default values The result looks like this.
COLUMN_NAME DEPTNO DEPTNAME MGRNO ADMRDEPT TABLE_NAME DEPARTMENT DEPARTMENT DEPARTMENT DEPARTMENT DATA_TYPE CHAR VARCHAR CHAR CHAR LENGTH 3 29 6 3 HAS_DEFAULT N N Y N
Related reference
SQL programming
35
DROP
Comparisons might not be case sensitive if a shared-weight sort sequence is used where uppercase and lowercase characters are treated as the same characters. A SELECT statement can include the following: 1. The name of each column you want to include in the result. 2. The name of the table or view that contains the data. 3. A search condition to identify the rows that contain the information you want. 4. The name of each column used to group your data. 5. A search condition that uniquely identifies a group that contains the information you want. 6. The order of the results so a specific row among duplicates can be returned.
36
The SELECT and FROM clauses must be specified. The other clauses are optional. With the SELECT clause, you specify the name of each column you want to retrieve. For example:
SELECT EMPNO, LASTNAME, WORKDEPT
You can specify that only one column be retrieved, or as many as 8000 columns. The value of each column you name is retrieved in the order specified in the SELECT clause. If you want to retrieve all columns (in the same order as they appear in the tables definition), use an asterisk (*) instead of naming the columns:
SELECT *
The FROM clause specifies the table that you want to select data from. You can select columns from more than one table. When issuing a SELECT, you must specify a FROM clause. Issue the following statement:
SELECT * FROM EMPLOYEE
The result is all of the columns and rows from the table EMPLOYEE. The SELECT list can also contain expressions, including constants, special registers, and scalar fullselects. An AS clause can be used to give the resulting column a name. For example, issue the following statement:
SELECT LASTNAME, SALARY * .05 AS RAISE FROM EMPLOYEE WHERE EMPNO = 200140
SQL programming
37
In this case, the search condition consists of one predicate: WORKDEPT = C01. To further illustrate WHERE, put it into a SELECT statement. Assume that each department listed in the CORPDATA.DEPARTMENT table has a unique department number. You want to retrieve the department name and manager number from the CORPDATA.DEPARTMENT table for department C01. Issue the following statement:
SELECT DEPTNAME, MGRNO FROM CORPDATA.DEPARTMENT WHERE DEPTNO = C01
If the search condition contains character, or UCS-2 or UTF-16 graphic column predicates, the sort sequence that is in effect when the query is run is applied to those predicates. If a sort sequence is not being used, character constants must be specified in uppercase or lowercase to match the column or expression they are being compared to. Related concepts Sort sequences and normalization in SQL on page 99 A sort sequence defines how characters in a character set relate to each other when they are compared or ordered. Normalization allows you to compare strings that contain combining characters. Related reference Defining complex search conditions on page 50 In addition to the basic comparison predicates, such as = and >, a search condition can contain any of these predicates: BETWEEN, IN, EXISTS, IS NULL, and LIKE. Multiple search conditions within a WHERE clause on page 52 You can qualify your request further by coding a search condition that includes several predicates. Expressions in the WHERE clause: An expression in a WHERE clause names or specifies something that you want to compare to something else. The expressions you specify can be: v A column name names a column. For example:
... WHERE EMPNO = 000200
EMPNO names a column that is defined as a 6-byte character value. v An expression identifies two values that are added (+), subtracted (-), multiplied (*), divided (/), have exponentiation (**), or concatenated (CONCAT or ||) to result in a value. The most common operands of an expression are: A constant A column A host variable A function A special register A scalar fullselect Another expression For example:
38
When the order of evaluation is not specified by parentheses, the expression is evaluated in the following order: 1. Prefix operators 2. Exponentiation 3. Multiplication, division, and concatenation 4. Addition and subtraction Operators on the same precedence level are applied from left to right. v A constant specifies a literal value for the expression. For example:
... WHERE 40000 < SALARY
SALARY names a column that is defined as a 9-digit packed decimal value (DECIMAL(9,2)). It is compared to the numeric constant 40000. v A host variable identifies a variable in an application program. For example:
... WHERE EMPNO = :EMP
v A special register identifies a special value defined by the database manager. For example:
... WHERE LASTNAME = USER
v A scalar fullselect. A search condition can specify many predicates separated by AND and OR. No matter how complex the search condition, it supplies a TRUE or FALSE value when evaluated against a row. There is also an unknown truth value, which is effectively false. That is, if the value of a row is null, this null value is not returned as a result of a search because it is not less than, equal to, or greater than the value specified in the search condition. To fully understand the WHERE clause, you need to know the order SQL evaluates search conditions and predicates, and compares the values of expressions. This topic is discussed in the SQL Reference topic collection. Related concepts Using subqueries on page 91 You can use subqueries in a search condition as another way to select data. Subqueries can be used anywhere an expression can be used. Related reference Defining complex search conditions on page 50 In addition to the basic comparison predicates, such as = and >, a search condition can contain any of these predicates: BETWEEN, IN, EXISTS, IS NULL, and LIKE. Expressions Comparison operators: SQL supports several comparison operators.
Comparison operator <> or = or != = < > <= or > or !> > = or < or !< Description Not equal to Equal to Less than Greater than Less than or equal to (or not greater than) Greater than or equal to (or not less than)
SQL programming
39
NOT keyword: You can precede a predicate with the NOT keyword to specify that you want the opposite of the predicates value (that is, TRUE if the predicate is FALSE). NOT applies only to the predicate it precedes, not to all predicates in the WHERE clause. For example, to indicate that you are interested in all employees except those working in the department C01, you can say:
... WHERE NOT WORKDEPT = C01
GROUP BY clause
The GROUP BY clause allows you to find the characteristics of groups of rows rather than individual rows. When you specify a GROUP BY clause, SQL divides the selected rows into groups such that the rows of each group have matching values in one or more columns or expressions. Next, SQL processes each group to produce a single-row result for the group. You can specify one or more columns or expressions in the GROUP BY clause to group the rows. The items you specify in the SELECT statement are properties of each group of rows, not properties of individual rows in a table or view. Without a GROUP BY clause, the application of SQL aggregate functions returns one row. When GROUP BY is used, the function is applied to each group, thereby returning as many rows as there are groups. For example, the CORPDATA.EMPLOYEE table has several sets of rows, and each set consists of rows describing members of a specific department. To find the average salary of people in each department, you can issue:
SELECT WORKDEPT, DECIMAL (AVG(SALARY),5,0) FROM CORPDATA.EMPLOYEE GROUP BY WORKDEPT
Notes: 1. Grouping the rows does not mean ordering them. Grouping puts each selected row in a group, which SQL then processes to derive characteristics of the group. Ordering the rows puts all the rows in the results table in ascending or descending collating sequence. Depending on the implementation selected by the database manager, the resulting groups might appear to be ordered.
40
2. If there are null values in the column you specify in the GROUP BY clause, a single-row result is produced for the data in the rows with null values. 3. If the grouping occurs over character, or UCS-2 or UTF-16 graphic columns, the sort sequence in effect when the query is run is applied to the grouping. When you use GROUP BY, you list the columns or expressions you want SQL to use to group the rows. For example, suppose that you want a list of the number of people working on each major project described in the CORPDATA.PROJECT table. You can issue:
SELECT SUM(PRSTAFF), MAJPROJ FROM CORPDATA.PROJECT GROUP BY MAJPROJ
The result is a list of the companys current major projects and the number of people working on each project.
SUM(PRSTAFF) 6 5 10 8 5 4 3 32.5 MAJPROJ AD3100 AD3110 MA2100 MA2110 OP1000 OP2000 OP2010 ?
You can also specify that you want the rows grouped by more than one column or expression. For example, you can issue a select statement to find the average salary for men and women in each department, using the CORPDATA.EMPLOYEE table. To do this, you can issue:
SELECT WORKDEPT, SEX, DECIMAL(AVG(SALARY),5,0) AS AVG_WAGES FROM CORPDATA.EMPLOYEE GROUP BY WORKDEPT, SEX
SQL programming
41
Because you did not include a WHERE clause in this example, SQL examines and processes all rows in the CORPDATA.EMPLOYEE table. The rows are grouped first by department number and next (within each department) by sex before SQL derives the average SALARY value for each group. Related concepts Sort sequences and normalization in SQL on page 99 A sort sequence defines how characters in a character set relate to each other when they are compared or ordered. Normalization allows you to compare strings that contain combining characters. Related reference ORDER BY clause on page 43 The ORDER BY clause specifies the particular order in which you want selected rows returned. The order is sorted by ascending or descending collating sequence of a columns or an expressions value.
HAVING clause
The HAVING clause specifies a search condition for the groups selected by the GROUP BY clause. The HAVING clause says that you want only those groups that satisfy the condition in that clause. Therefore, the search condition you specify in the HAVING clause must test properties of each group rather than properties of individual rows in the group. The HAVING clause follows the GROUP BY clause and can contain the same kind of search condition as you can specify in a WHERE clause. In addition, you can specify aggregate functions in a HAVING clause. For example, suppose that you want to retrieve the average salary of women in each department. To do this, use the AVG aggregate function and group the resulting rows by WORKDEPT and specify a WHERE clause of SEX = F. To specify that you want this data only when all the female employees in the selected department have an education level equal to or greater than 16 (a college graduate), use the HAVING clause. The HAVING clause tests a property of the group. In this case, the test is on MIN(EDLEVEL), which is a group property:
SELECT WORKDEPT, DECIMAL(AVG(SALARY),5,0) AS AVG_WAGES, MIN(EDLEVEL) AS MIN_EDUC FROM CORPDATA.EMPLOYEE WHERE SEX=F GROUP BY WORKDEPT HAVING MIN(EDLEVEL)>=16
You can use multiple predicates in a HAVING clause by connecting them with AND and OR, and you can use NOT for any predicate of a search condition. Note: If you intend to update a column or delete a row, you cannot include a GROUP BY or HAVING clause in the SELECT statement within a DECLARE CURSOR statement. These clauses make it a read-only cursor. Predicates with arguments that are not aggregate functions can be coded in either WHERE or HAVING clauses. It is typically more efficient to code the selection criteria in the WHERE clause because it is handled earlier in the query processing. The HAVING selection is performed in post processing of the result table.
42
If the search condition contains predicates involving character, or UCS-2 or UTF-16 graphic columns, the sort sequence in effect when the query is run is applied to those predicates. Related concepts Sort sequences and normalization in SQL on page 99 A sort sequence defines how characters in a character set relate to each other when they are compared or ordered. Normalization allows you to compare strings that contain combining characters. Related reference Using a cursor on page 215 When SQL runs a SELECT statement, the resulting rows comprise the result table. A cursor provides a way to access a result table.
ORDER BY clause
The ORDER BY clause specifies the particular order in which you want selected rows returned. The order is sorted by ascending or descending collating sequence of a columns or an expressions value. For example, to retrieve the names and department numbers of female employees listed in the alphanumeric order of their department numbers, you can use this select-statement:
SELECT LASTNAME,WORKDEPT FROM CORPDATA.EMPLOYEE WHERE SEX=F ORDER BY WORKDEPT
SQL programming
43
The column specified in the ORDER BY clause does not need to be included in the SELECT clause. For example, the following statement will return all female employees ordered with the largest salary first:
SELECT LASTNAME,FIRSTNME FROM CORPDATA.EMPLOYEE WHERE SEX=F ORDER BY SALARY DESC
If an AS clause is specified to name a result column in the select-list, this name can be specified in the ORDER BY clause. The name specified in the AS clause must be unique in the select-list. For example, to retrieve the full names of employees listed in alphabetic order, you can use this select-statement:
SELECT LASTNAME CONCAT FIRSTNME AS FULLNAME FROM CORPDATA.EMPLOYEE ORDER BY FULLNAME
Instead of naming the columns to order the results, you can use a number. For example, ORDER BY 3 specifies that you want the results ordered by the third column of the results table, as specified by the select-list. Use a number to order the rows of the results table when the sequencing value is not a named column. You can also specify whether you want SQL to collate the rows in ascending (ASC) or descending (DESC) sequence. An ascending collating sequence is the default. In the previous select-statement, SQL first returns the row with the lowest FULLNAME expression (alphabetically and numerically), followed by rows with higher values. To order the rows in descending collating sequence based on this name, specify:
... ORDER BY FULLNAME DESC
You can specify a secondary ordering sequence (or several levels of ordering sequences) as well as a primary one. In the previous example, you might want the rows ordered first by department number, and within each department, ordered by employee name. To do this, specify:
... ORDER BY WORKDEPT, FULLNAME
If character columns, or UCS-2 or UTF-16 graphic columns are used in the ORDER BY clause, ordering for these columns is based on the sort sequence in effect when the query is run. Related concepts Sort sequences and normalization in SQL on page 99 A sort sequence defines how characters in a character set relate to each other when they are compared or ordered. Normalization allows you to compare strings that contain combining characters. Related reference GROUP BY clause on page 40 The GROUP BY clause allows you to find the characteristics of groups of rows rather than individual rows.
44
The result table for a SELECT INTO should contain just one row. For example, each row in the CORPDATA.EMPLOYEE table has a unique EMPNO (employee number) column. The result of a SELECT INTO statement for this table, if the WHERE clause contains an equal comparison on the EMPNO column, will be exactly one row (or no rows). Finding more than one row is an error, but one row is still returned. You can control which row will be returned in this error condition by specifying the ORDER BY clause. If you use the ORDER BY clause, the first row in the result table is returned. If you want more than one row to be the result of a SELECT INTO statement, use a DECLARE CURSOR statement to select the rows, followed by a FETCH statement to move the column values into host variables one or many rows at a time. When using the select-statement in an application program, list the column names to give your program more data independence. There are two reasons for this: 1. When you look at the source code statement, you can easily see the one-to-one correspondence between the column names in the SELECT clause and the host variables named in the INTO clause. 2. If a column is added to a table or view you access and you use SELECT * ..., and you create the program again from source, the INTO clause does not have a matching host variable named for the new column. The extra column causes you to get a warning (not an error) in the SQLCA (SQLWARN3 will contain a W). When using the GET DIAGNOSTICS statement, the RETURNED_SQLSTATE item will have a value of 01503. Related reference Using a cursor on page 215 When SQL runs a SELECT statement, the resulting rows comprise the result table. A cursor provides a way to access a result table.
To get the rows that do not have a null value for the manager number, you can change the WHERE clause like this:
WHERE MGRNO IS NOT NULL
SQL programming
45
Another predicate that is useful for comparing values that can contain the NULL value is the DISTINCT predicate. Comparing two columns using a normal equal comparison (COL1 = COL2) will be true if both columns contain an equal non-null value. If both columns are null, the result will be false because null is never equal to any other value, not even another null value. Using the DISTINCT predicate, null values are considered equal. So COL1 is NOT DISTINCT from COL2 will be true if both columns contain an equal non-null value and also when both columns are the null value. For example, suppose that you want to select information from two tables that contain null values. The first table T1 has a column C1 with the following values.
C1 2 1 null
For more information about the use of null values, see the SQL Reference topic collection.
| CURRENT DEGREE |
CURRENT PATH CURRENT_PATH CURRENT FUNCTION PATH
46
Contents The schema name used to qualify unqualified database object references where applicable in dynamically prepared SQL statements. The name of the relational database currently being used. The current time.
CURRENT SERVER CURRENT_SERVER CURRENT TIME CURRENT_TIME CURRENT TIMESTAMP CURRENT_TIMESTAMP CURRENT TIMEZONE CURRENT_TIMEZONE
The current date and time in timestamp format. A duration of time that links local time to Universal Time Coordinated (UTC) using the formula: local time CURRENT TIMEZONE = UTC It is taken from the system value QUTCOFFSET.
| | | |
The runtime authorization identifier (user profile) of the job. The authorization identifier (user profile) of the user connected to the database.
If a single statement contains more than one reference to any of CURRENT DATE, CURRENT TIME, or CURRENT TIMESTAMP special registers, or the CURDATE, CURTIME, or NOW scalar functions, all values are based on a single clock reading. For remotely run SQL statements, the special registers and their contents are shown in the following table.
Special registers CURRENT DATE CURRENT_DATE CURRENT TIME CURRENT_TIME CURRENT TIMESTAMP CURRENT_TIMESTAMP Contents The current date and time at the remote system, not the local system.
| |
CURRENT DEGREE CURRENT TIMEZONE CURRENT_TIMEZONE CURRENT SERVER CURRENT_SERVER CURRENT SCHEMA CURRENT PATH CURRENT_PATH CURRENT FUNCTION PATH
The number of tasks the data base manager should run in parallel on the remote system. A duration of time that links the remote system time to UTC. The name of the relational database currently being used. The current schema value at the remote system. The current path value at the remote system.
| | |
The runtime authorization identifier (user profile) of the job on the remote system. The authorization identifier (user profile) of the user connected to the database on the remote system.
SQL programming
47
When a query over a distributed table references a special register, the contents of the special register on the system that requests the query are used. For more information about distributed tables, see the DB2 Multisystem topic collection.
You can also use the CAST specification to cast data types directly:
SELECT CAST(BIRTHDATE AS CHAR(10)) FROM CORPDATA.EMPLOYEE
if HIREDATE is a date column, the character string 1950-01-01 is interpreted as a date. v A character string variable or constant used to set a date, time, or timestamp column in either the SET clause of an UPDATE statement, or the VALUES clause of an INSERT statement. Related reference Data types Specifying current date and time values: You can specify a current date, time, or timestamp in an expression by using one of these special registers: CURRENT DATE, CURRENT TIME, and CURRENT TIMESTAMP. The value of each is based on a time-of-day clock reading obtained during the running of the statement. Multiple references to CURRENT DATE, CURRENT TIME, or CURRENT TIMESTAMP within the same SQL statement use the same value. The following statement returns the age (in years) of each employee in the EMPLOYEE table when the statement is run:
48
The CURRENT TIMEZONE special register allows a local time to be converted to Universal Time Coordinated (UTC). For example, if you have a table named DATETIME that contains a time column type with a name of STARTT, and you want to convert STARTT to UTC, you can use the following statement:
SELECT STARTT - CURRENT TIMEZONE FROM DATETIME
Date/time arithmetic: Addition and subtraction are the only arithmetic operators applicable to date, time, and timestamp values. You can increment and decrement a date, time, or timestamp by a duration; or subtract a date from a date, a time from a time, or a timestamp from a timestamp. Related reference Datetime arithmetic in SQL
DISTINCT means that you want to select only the unique rows. If a selected row duplicates another row in the result table, the duplicate row is ignored (it is not put into the result table). For example, suppose you want a list of employee job codes. You do not need to know which employee has what job code. Because it is probable that several people in a department have the same job code, you can use DISTINCT to ensure that the result table has only unique values. The following example shows how to do this:
SELECT DISTINCT JOB FROM CORPDATA.EMPLOYEE WHERE WORKDEPT = D11
If you do not include DISTINCT in a SELECT clause, you might find duplicate rows in your result, because SQL returns the JOB columns value for each row that satisfies the search condition. Null values are treated as duplicate rows for DISTINCT. If you include DISTINCT in a SELECT clause and you also include a shared-weight sort sequence, fewer values might be returned. The sort sequence causes values that contain the same characters to be weighted the same. If MGR, Mgr, and mgr are all in the same table, only one of these values is returned.
SQL programming
49
Related concepts Sort sequences and normalization in SQL on page 99 A sort sequence defines how characters in a character set relate to each other when they are compared or ordered. Normalization allows you to compare strings that contain combining characters.
The BETWEEN keyword is inclusive. A more complex, but explicit, search condition that produces the same result is:
... WHERE HIREDATE >= 1987-01-01 AND HIREDATE <= 1987-12-31
v IN says you are interested in rows in which the value of the specified expression is among the values you listed. For example, to find the names of all employees in departments A00, C01, and E21, you can specify:
... WHERE WORKDEPT IN (A00, C01, E21)
v EXISTS says you are interested in testing for the existence of certain rows. For example, to find out if there are any employees that have a salary greater than 60000, you can specify:
EXISTS (SELECT * FROM EMPLOYEE WHERE SALARY > 60000)
v IS NULL says that you are interested in testing for null values. For example, to find out if there are any employees without a phone listing, you can specify:
... WHERE EMPLOYEE.PHONE IS NULL
v LIKE says you are interested in rows in which an expression is similar to the value you supply. When you use LIKE, SQL searches for a character string similar to the one you specify. The degree of similarity is determined by two special characters used in the string that you include in the search condition: _ % An underline character stands for any single character. A percent sign stands for an unknown string of 0 or more characters. If the percent sign starts the search string, then SQL allows 0 or more character(s) to precede the matching value in the column. Otherwise, the search string must begin in the first position of the column.
Note: If you are operating on MIXED data, the following distinction applies: an SBCS underline character refers to one SBCS character. No such restriction applies to the percent sign; that is, a percent sign refers to any number of SBCS or DBCS characters. See SQL Reference for more information about the LIKE predicate and MIXED data. Use the underline character or percent sign either when you do not know or do not care about all the characters of the columns value. For example, to find out which employees live in Minneapolis, you can specify:
... WHERE ADDRESS LIKE %MINNEAPOLIS%
SQL returns any row with the string MINNEAPOLIS in the ADDRESS column, no matter where the string occurs. In another example, to list the towns whose names begin with SAN, you can specify:
50
If you want to find any addresses where the street name isnt in your master street name list, you can use an expression in the LIKE expression. In this example, the STREET column in the table is assumed to be upper case.
... WHERE UCASE (:address_variable) NOT LIKE %||STREET||%
If you want to search for a character string that contains either the underscore or percent character, use the ESCAPE clause to specify an escape character. For example, to see all businesses that have a percent in their name, you can specify:
... WHERE BUSINESS_NAME LIKE %@%% ESCAPE @
The first and last percent characters in the LIKE string are interpreted as the normal LIKE percent characters. The combination @% is taken as the actual percent character. Related concepts Using subqueries on page 91 You can use subqueries in a search condition as another way to select data. Subqueries can be used anywhere an expression can be used. Sort sequences and normalization in SQL on page 99 A sort sequence defines how characters in a character set relate to each other when they are compared or ordered. Normalization allows you to compare strings that contain combining characters. Related reference Specifying a search condition using the WHERE clause on page 37 The WHERE clause specifies a search condition that identifies the row or rows that you want to retrieve, update, or delete. Expressions in the WHERE clause on page 38 An expression in a WHERE clause names or specifies something that you want to compare to something else. Predicates Special considerations for LIKE: Here are some considerations for using the LIKE predicate. v When host variables are used in place of string constants in a search pattern, you should consider using varying length host variables. This allows you to: Assign previously used string constants to host variables without any changes. Obtain the same selection criteria and results as if a string constant were used. v When fixed-length host variables are used in place of string constants in a search pattern, you should ensure that the value specified in the host variable matches the pattern previously used by the string constants. All characters in a host variable that are not assigned a value are initialized with a blank. For example, if you do a search using the string pattern ABC% in a varying length host variable, these are some of the values that can be returned:
ABCD ABCDE ABCxxx ABC
However, if you do a search using the search pattern ABC% contained in a host variable with a fixed length of 10, these values can be returned, assuming that the column has a length of 12:
ABCDE ABCD ABCxxx ABC
Note: All returned values start with ABC and end with at least 6 blanks. Blanks are used because the last 6 characters in the host variable are not assigned a specific value. If you want to do a search using a fixed-length host variable where the last 7 characters can be anything, search for ABC%%%%%%%. These are some of the values that can be returned:
ABCDEFGHIJ ABCXXXXXXX ABCDE ABCDD
SQL programming
51
Multiple search conditions within a WHERE clause: You can qualify your request further by coding a search condition that includes several predicates. The search condition you specify can contain any of the comparison operators or the predicates BETWEEN, DISTINCT, IN, LIKE, EXISTS, IS NULL, and IS NOT NULL. You can combine any two predicates with AND and OR. In addition, you can use the NOT keyword to specify that the search condition that you want is the negated value of the specified search condition. A WHERE clause can have as many predicates as you want. v AND says that, for a row to qualify, the row must satisfy both predicates of the search condition. For example, to find out which employees in department D21 were hired after December 31, 1987, specify:
... WHERE WORKDEPT = D21 AND HIREDATE > 1987-12-31
v OR says that, for a row to qualify, the row can satisfy the condition set by either or both predicates of the search condition. For example, to find out which employees are in either department C01 or D11, you can specify :
... WHERE WORKDEPT = C01 OR WORKDEPT = D11
Note: You can also use IN to specify this request: WHERE WORKDEPT IN (C01, D11). v NOT says that, to qualify, a row must not meet the criteria set by the search condition or predicate that follows the NOT. For example, to find all employees in the department E11 except those with a job code equal to analyst, you can specify:
... WHERE WORKDEPT = E11 AND NOT JOB = ANALYST
When SQL evaluates search conditions that contain these connectors, it does so in a specific order. SQL first evaluates the NOT clauses, next evaluates the AND clauses, and then the OR clauses. You can change the order of evaluation by using parentheses. The search conditions enclosed in parentheses are evaluated first. For example, to select all employees in departments E11 and E21 who have education levels greater than 12, you can specify:
... WHERE EDLEVEL > 12 AND (WORKDEPT = E11 OR WORKDEPT = E21)
The parentheses determine the meaning of the search condition. In this example, you want all rows that have a: v WORKDEPT value of E11 or E21, and v EDLEVEL value greater than 12 If you did not use parentheses:
... WHERE EDLEVEL > 12 AND WORKDEPT = E11 OR WORKDEPT = E21
Your result is different. The selected rows are rows that have: v WORKDEPT = E11 and EDLEVEL > 12, or v WORKDEPT = E21, regardless of the EDLEVEL value | If you are combining multiple equal comparisons, you can write the predicate with the ANDs as shown | in the following example: | ... WHERE WORKDEPT = E11 AND EDLEVEL = 12 AND JOB = CLERK |
52
| | | | | | |
When two lists are used, the first item in the first list is compared to the first item in the second list, and so on through both lists. Thus, each list must contain the same number of entries. Using lists is identical to writing the query with AND. Lists can only be used with the equal and not equal comparison operators. Related reference Specifying a search condition using the WHERE clause on page 37 The WHERE clause specifies a search condition that identifies the row or rows that you want to retrieve, update, or delete.
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
In this example, the SALARY descending order with the top 10 returned. The RANK column shows the relative ranking of each salary. Notice that there are two rows with the same salary at position 2. Each of those rows is assigned the same rank value. The following row is assigned the value of 4. RANK returns a value for a row that is one more than the total number of rows that precede that row. There are gaps in the numbering sequence whenever there are duplicates. In contrast, the DENSE_RANK column shows a value of 3 for the row directly after the duplicate rows. DENSE_RANK returns a value for a row that is one more than the number of distinct row values that precede it. There will never be gaps in the numbering sequence.
SQL programming
53
| ROW_NUMBER returns a unique number for each row. For rows that contain duplicate values according | to the specified ordering, the assignment of a row number is arbitrary; the row numbers could be | assigned in a different order for the duplicate rows when the query is run another time. | Example: Ranking groups | | | | | | | Suppose that you want to find out which department has the highest average salary. The following query groups the data by department, determines the average salary for each department, and ranks the resulting averages.
SELECT WORKDEPT, INT(AVG(SALARY)) AS AVERAGE, RANK() OVER(ORDER BY AVG(SALARY) DESC) AS AVG_SALARY FROM EMPLOYEE GROUP BY WORKDEPT
| Example: Ranking within a department | Suppose that you want a list of employees along with how their bonus ranks within their department. | Using the PARTITION BY clause, you can specify groups that are to be numbered separately. | SELECT LASTNAME, WORKDEPT, BONUS, DENSE_RANK() OVER(PARTITION BY WORKDEPT ORDER BY BONUS DESC) | AS BONUS_RANK_IN_DEPT | FROM EMPLOYEE | WHERE WORKDEPT LIKE E% | | This query returns the following information. | | | | | | | | | | |
Table 11. Results of the previous query LASTNAME GEYER HENDERSON SCHNEIDER SCHWARTZ SMITH PARKER SETRIGHT SPRINGER SPENSER WORKDEPT E01 E11 E11 E11 E11 E11 E11 E11 E21 BONUS 800.00 600.00 500.00 500.00 400.00 300.00 300.00 300.00 500.00 BONUS_RANK_in_DEPT 1 1 2 2 3 4 4 4 1
54
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
Table 11. Results of the previous query (continued) LASTNAME LEE GOUNOT WONG ALONZO MENTA WORKDEPT E21 E21 E21 E21 E21 BONUS 500.00 500.00 500.00 500.00 400.00 BONUS_RANK_in_DEPT 1 1 1 1 2
55
v If you do not list the column names you want, but instead use SELECT *, SQL returns rows that consist of all the columns of the first table, followed by all the columns of the second table, and so on. v You must be authorized to select rows from each table or view specified in the FROM clause. v The sort sequence is applied to all character, or UCS-2 or UTF-16 graphic columns being joined. Inner join: An inner join returns only the rows from each table that have matching values in the join columns. Any rows that do not have a match between the tables do not appear in the result table. With an inner join, column values from one row of a table are combined with column values from another row of another (or the same) table to form a single row of data. SQL examines both tables specified for the join to retrieve data from all the rows that meet the search condition for the join. There are two ways of specifying an inner join: using the JOIN syntax, and using the WHERE clause. Suppose you want to retrieve the employee numbers, names, and project numbers for all employees that are responsible for a project. In other words, you want the EMPNO and LASTNAME columns from the CORPDATA.EMPLOYEE table and the PROJNO column from the CORPDATA.PROJECT table. Only employees with last names starting with S or later should be considered. To find this information, you need to join the two tables. Inner join using the JOIN syntax: To use the inner join syntax, both of the tables you are joining are listed in the FROM clause, along with the join condition that applies to the tables. The join condition is specified after the ON keyword and determines how the two tables are to be compared to each other to produce the join result. The condition can be any comparison operator; it does not need to be the equal operator. Multiple join conditions can be specified in the ON clause separated by the AND keyword. Any additional conditions that do not relate to the actual join are specified in either the WHERE clause or as part of the actual join in the ON clause.
SELECT EMPNO, LASTNAME, PROJNO FROM CORPDATA.EMPLOYEE INNER JOIN CORPDATA.PROJECT ON EMPNO = RESPEMP WHERE LASTNAME > S
In this example, the join is done on the two tables using the EMPNO and RESPEMP columns from the tables. Since only employees that have last names starting with at least S are to be returned, this additional condition is provided in the WHERE clause. This query returns the following output.
EMPNO 000250 000060 000100 000020 LASTNAME SMITH STERN SPENSER THOMPSON PROJNO AD3112 MA2110 OP2010 PL2100
Inner join using the WHERE clause: To use the WHERE clause to perform the same join as you perform using the INNER JOIN syntax, enter both the join condition and the additional selection condition in the WHERE clause. The tables to be joined are listed in the FROM clause, separated by commas.
56
SELECT EMPNO, LASTNAME, PROJNO FROM CORPDATA.EMPLOYEE, CORPDATA.PROJECT WHERE EMPNO = RESPEMP AND LASTNAME > S
This query returns the same output as the previous example. Joining data with the USING clause: You can use the USING clause for a shorthand way of defining join conditions. The USING clause is equivalent to a join condition where each column from the left table is compared to a column with the same name in the right table. For example, look at the USING clause in this statement:
SELECT EMPNO, ACSTDATE FROM CORPDATA.PROJACT INNER JOIN CORPDATA.EMPPROJACT USING (PROJNO, ACTNO) WHERE ACSDATE > 1982-12-31;
The syntax in this statement is valid and equivalent to the join condition in the following statement:
SELECT EMPNO, ACSTDATE FROM CORPDATA.PROJACT INNER JOIN CORPDATA.EMPPROJACT ON CORPDATA.PROJACT.PROJNO = CORPDATA.EMPPROJACT.PROJNO AND CORPDATA.PROJACT.ACTNO = CORPDATA.EMPPROJACT.ACTNO WHERE ACSTDATE > 1982-12-31;
Left outer join: A left outer join returns all the rows that an inner join returns plus one row for each of the other rows in the first table that do not have a match in the second table. Suppose you want to find all employees and the projects they are currently responsible for. You want to see those employees that are not currently in charge of a project as well. The following query will return a list of all employees whose names are greater than S, along with their assigned project numbers.
SELECT EMPNO, LASTNAME, PROJNO FROM CORPDATA.EMPLOYEE LEFT OUTER JOIN CORPDATA.PROJECT ON EMPNO = RESPEMP WHERE LASTNAME > S
The result of this query contains some employees that do not have a project number. They are listed in the query, but have the null value returned for their project number.
EMPNO 000020 000060 000100 000170 000180 000190 000250 000280 000300 000310 200170 LASTNAME THOMPSON STERN SPENSER YOSHIMURA SCOUTTEN WALKER SMITH SCHNEIDER SMITH SETRIGHT YAMAMOTO PROJNO PL2100 MA2110 OP2010 AD3112 SQL programming
57
PROJNO -
Note: Using the RRN scalar function to return the relative record number for a column in the table on the right in a left outer join or exception join will return a value of 0 for the unmatched rows. Right outer join: A right outer join returns all the rows that an inner join returns plus one row for each of the other rows in the second table that do not have a match in the first table. It is the same as a left outer join with the tables specified in the opposite order. The query that was used as the left outer join example can be rewritten as a right outer join as follows:
SELECT EMPNO, LASTNAME, PROJNO FROM CORPDATA.PROJECT RIGHT OUTER JOIN CORPDATA.EMPLOYEE ON EMPNO = RESPEMP WHERE LASTNAME > S
The results of this query are identical to the results from the left outer join query. Exception join: A left exception join returns only the rows from the first table that do not have a match in the second table. Using the same tables as before, return those employees that are not responsible for any projects.
SELECT EMPNO, LASTNAME, PROJNO FROM CORPDATA.EMPLOYEE EXCEPTION JOIN CORPDATA.PROJECT ON EMPNO = RESPEMP WHERE LASTNAME > S
An exception join can also be written as a subquery using the NOT EXISTS predicate. The previous query can be rewritten in the following way:
58
SELECT EMPNO, LASTNAME FROM CORPDATA.EMPLOYEE WHERE LASTNAME > S AND NOT EXISTS (SELECT * FROM CORPDATA.PROJECT WHERE EMPNO = RESPEMP)
The only difference in this query is that it cannot return values from the PROJECT table. There is a right exception join, too, that works just like a left exception join but with the tables reversed. Cross join: A cross join, also known as a Cartesian Product join, returns a result table where each row from the first table is combined with each row from the second table. The number of rows in the result table is the product of the number of rows in each table. If the tables involved are large, this join can take a very long time. A cross join can be specified in two ways: using the JOIN syntax or by listing the tables in the FROM clause separated by commas without using a WHERE clause to supply join criteria. Suppose that the following tables exist.
Table 13. Table A ACOL1 A1 A2 A3 Table 14. Table B BCOL1 B1 B2 BCOL2 BB1 BB2 ACOL2 AA1 AA2 AA3
The result table for either of these SELECT statements looks like this.
ACOL1 A1 A1 A2 A2 A3 A3 ACOL2 AA1 AA1 AA2 AA2 AA3 AA3 BCOL1 B1 B2 B1 B2 B1 B2 BCOL2 BB1 BB2 BB1 BB2 BB1 BB2
SQL programming
59
Like the left and right outer joins, a full outer join returns matching rows from both tables. However, a full outer join also returns nonmatching rows from both tables. While DB2 UDB for iSeries does not support the full outer join syntax, you can simulate a full outer join by using a left outer join and a right exception join. Suppose that you want to find all employees and all of their projects. You want to see those employees that are not currently in charge of a project as well. The following query returns a list of all employees whose names are greater than S, along with their assigned project numbers:
SELECT EMPNO, LASTNAME, PROJNO FROM CORPDATA.EMPLOYEE LEFT OUTER JOIN CORPDATA.PROJECT ON EMPNO = RESPEMP WHERE LASTNAME > S UNION (SELECT EMPNO, LASTNAME, PROJNO FROM CORPDATA.PROJECT EXCEPTION JOIN CORPDATA.EMPLOYEE ON EMPNO = RESPEMP WHERE LASTNAME > S);
Multiple join types in one statement: Sometimes you need to join more than two tables to produce the result that you want. If you want to return all the employees, their department names, and the projects they are responsible for, if any, you need to join the EMPLOYEE table, the DEPARTMENT table, and the PROJECT table to get the information. The following example shows the query and the results:
SELECT EMPNO, LASTNAME, DEPTNAME, PROJNO FROM CORPDATA.EMPLOYEE INNER JOIN CORPDATA.DEPARTMENT ON WORKDEPT = DEPTNO LEFT OUTER JOIN CORPDATA.PROJECT ON EMPNO = RESPEMP WHERE LASTNAME > S EMPNO 000020 000060 000100 000170 000180 000190 000250 000280 000300 000310 LASTNAME THOMPSON STERN SPENSER YOSHIMURA SCOUTTEN WALKER SMITH SCHNEIDER SMITH SETRIGHT DEPTNAME PLANNING MANUFACTURING SYSTEMS SOFTWARE SUPPORT MANUFACTURING SYSTEMS MANUFACTURING SYSTEMS MANUFACTURING SYSTEMS ADMINISTRATION SYSTEMS OPERATIONS OPERATIONS OPERATIONS PROJNO PL2100 MA2110 OP2010 AD3112 -
60
DEPARTMENT and EMPLOYEE tables, and the salaries are in the EMPLOYEE table. You can use a table expression in the FROM clause to select the maximum salary for each department. You can also add a correlation name, T2, following the nested table expression to name the derived table. The outer select then uses T2 to qualify columns that are selected from the derived table, in this case MAXSAL and WORKDEPT. Note that the MAX(SALARY) column selected in the nested table expression must be named in order to be referenced in the outer select. The AS clause is used to do that.
SELECT MGRNO, T1.DEPTNO, MAXSAL FROM CORPDATA.DEPARTMENT T1, (SELECT MAX(SALARY) AS MAXSAL, WORKDEPT FROM CORPDATA.EMPLOYEE E1 GROUP BY WORKDEPT) T2 WHERE T1.DEPTNO = T2.WORKDEPT ORDER BY DEPTNO
Common table expressions can be specified before the full-select in a SELECT statement, an INSERT statement, or a CREATE VIEW statement. They can be used when the same result table needs to be shared in a full-select. Common table expressions are preceded with the keyword WITH. For example, suppose you want a table that shows the minimum and maximum of the average salary of a certain set of departments. The first character of the department number has some meaning and you want to get the minimum and maximum for those departments that start with the letter D and those that start with the letter E. You can use a common table expression to select the average salary for each department. Again, you must name the derived table; in this case, the name is DT. You can then specify a SELECT statement using a WHERE clause to restrict the selection to only the departments that begin with a certain letter. Specify the minimum and maximum of column AVGSAL from the derived table DT. Specify a UNION to get the results for the letter E and the results for the letter D.
WITH DT AS (SELECT E.WORKDEPT AS DEPTNO, AVG(SALARY) AS AVGSAL FROM CORPDATA.DEPARTMENT D , CORPDATA.EMPLOYEE E WHERE D.DEPTNO = E.WORKDEPT GROUP BY E.WORKDEPT) SELECT E, MAX(AVGSAL), MIN(AVGSAL) FROM DT WHERE DEPTNO LIKE E% UNION SELECT D, MAX(AVGSAL), MIN(AVGSAL) FROM DT WHERE DEPTNO LIKE D%
SQL programming
61
Suppose that you want to write a query against your ordering database that will return the top 5 items (in total quantity ordered) within the last 1000 orders from customers who also ordered item XXX.
WITH X AS (SELECT ORDER_ID, CUST_ID FROM ORDERS ORDER BY ORD_DATE DESC FETCH FIRST 1000 ROWS ONLY), Y AS (SELECT CUST_ID, LINE_ID, ORDER_QTY FROM X, ORDERLINE WHERE X.ORDER_ID = ORDERLINE.ORDER_ID) SELECT LINE_ID FROM (SELECT LINE_ID FROM Y WHERE Y.CUST_ID IN (SELECT DISTINCT CUST_ID FROM Y WHERE LINE.ID = XXX ) GROUP BY LINE_ID ORDER BY SUM(ORDER_QTY) DESC) FETCH FIRST 5 ROWS ONLY
The first common table expression (X) returns the most recent 1000 order numbers. The result is ordered by the date in descending order and then only the first 1000 of those ordered rows are returned as the result table. The second common table expression (Y) joins the most recent 1000 orders with the line item table and returns (for each of the 1000 orders) the customer, line item, and quantity of the line item for that order. The derived table in the main select statement returns the line items for the customers who are in the top 1000 orders who ordered item XXX. The results for all customers who ordered XXX are then grouped by the line item and the groups are ordered by the total quantity of the line item. Finally, the outer select selects only the first 5 rows from the ordered list that the derived table returned. | Using recursive queries | Some applications work with data that is recursive in nature. To query this type of data, you can use a | recursive common table expression or a recursive view. | | | | | | | | | | | | | | | | | | | | | | | | For example, a Bill of Materials (BOM) application works with the expansion of parts and its component subparts. For example, a chair might be made of a seat unit and a leg assembly. The seat unit might consist of a seat and two arms. Each of these parts can be further broken down into its subparts until there is a list of all the parts needed to build a chair. In the following trip planner examples, airline flights and train connections are used to find transportation paths between cities. The following table definitions and data are used in the examples.
CREATE TABLE FLIGHTS (DEPARTURE CHAR(20), ARRIVAL CHAR(20), CARRIER CHAR(15), FLIGHT_NUMBER CHAR(5), PRICE INT) INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INSERT INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO INTO FLIGHTS FLIGHTS FLIGHTS FLIGHTS FLIGHTS FLIGHTS FLIGHTS FLIGHTS FLIGHTS FLIGHTS FLIGHTS VALUES(New York, Paris, Atlantic, 234, 400) VALUES(Chicago, Miami, NA Air, 2334, 300) VALUES(New York, London, Atlantic, 5473, 350) VALUES(London, Athens , Mediterranean, 247, 340) VALUES(Athens, Nicosia , Mediterranean, 2356, 280) VALUES(Paris, Madrid , Euro Air, 3256, 380) VALUES(Paris, Cairo , Euro Air, 63, 480) VALUES(Chicago, Frankfurt, Atlantic, 37, 480) VALUES(Frankfurt, Moscow, Asia Air, 2337, 580) VALUES(Frankfurt, Beijing, Asia Air, 77, 480) VALUES(Moscow, Tokyo, Asia Air, 437, 680)
62
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
VALUES(Frankfurt, Vienna, Euro Air, 59, 200) VALUES(Paris, Rome, Euro Air, 534, 340) VALUES(Miami, Lima, SA Air, 5234, 530) VALUES(New York, Los Angeles, NA Air, 84, 330) VALUES(Los Angeles, Tokyo, Pacific Air, 824, 530) VALUES(Tokyo, Hong Kong, Asia Air, 94, 330) VALUES(Washington, Toronto, NA Air, 104, 250)
CREATE TABLE TRAINS(DEPARTURE CHAR(20), ARRIVAL CHAR(20), RAILLINE CHAR(15), TRAIN CHAR(5), PRICE INT) INSERT INTO TRAINS VALUES(Chicago, Washington, UsTrack, 323, 90) INSERT INTO TRAINS VALUES(Madrid, Barcelona, EuroTrack, 5234, 60) INSERT INTO TRAINS VALUES(Washington , Boston , UsTrack, 232, 50)
Now that the tables are set up, the data can be queried to find information about the airline network. Suppose you want to find out what cities you can fly to if you start in Chicago, and how many separate flights it will take to get there. The following query shows you that information.
WITH destinations (origin, departure, arrival, flight_count) AS (SELECT a.departure, a.departure, a.arrival, 1 FROM flights a WHERE a.departure = Chicago UNION ALL SELECT r.origin, b.departure, b.arrival, r.flight_count + 1 FROM destinations r, flights b WHERE r.arrival = b.departure) SELECT origin, departure, arrival, flight_count FROM destinations
This recursive query is written in two parts. The first part of the common table expression is called the intialization fullselect. It selects the first rows for the result set of the common table expression. In this example, it selects the two rows in the flights table that get you directly to another location from Chicago. It also initializes the number of flight legs to one for each row it selects. The second part of the recursive query joins the rows from the current result set of the common table expression with other rows from the original table. It is called the iterative fullselect. This is where the recursion is introduced. Notice that the rows that have already been selected for the result set are referenced by using the name of the common table expression as the table name and the common table expression result column names as the column names.
SQL programming
63
| | | | | | |
In this recursive part of the query, any rows from the original table that you can get to from each of the previously selected arrival cities are selected. A previously selected rows arrival city becomes the new departure city. Each row from this recursive select increments the flight count to the destination by one more flight. As these new rows are added to the common table expression result set, they are also fed into the iterative fullselect to generate more result set rows. In the data for the final result, you can see that the total number of flights is actually the total number of recursive joins (plus 1) it took to get to that arrival city.
| A recursive view looks very similar to a recursive common table expression. You can write the previous | recursive common table expression as a recursive view like this: | CREATE VIEW destinations (origin, departure, arrival, flight_count) AS SELECT departure, departure, arrival, 1 | FROM flights | WHERE departure = Chicago | UNION ALL | SELECT r.origin, b.departure, b.arrival, r.flight_count + 1 | FROM destinations r, flights b | WHERE r.arrival = b.departure) | | The interactive fullselect part of this view definition refers to the view itself. Selection from this view | returns the same rows as you get from the previous recursive common table expression. | Example: Two starting cities | | | | | | | | | | | | | | Now, to make the query a bit more complicated, suppose you are willing to fly from either Chicago or New York, and you want to know where you could go and how much it would cost.
WITH destinations (departure, arrival, connections, cost) AS (SELECT a.departure, a.arrival, 0, price FROM flights a WHERE a.departure = Chicago OR a.departure = New York UNION ALL SELECT r.departure, b.arrival, r.connections + 1, r.cost + b.price FROM destinations r, flights b WHERE r.arrival = b.departure) SELECT departure, arrival, connections, cost FROM destinations
64
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
Table 16. Results of the previous query (continued) DEPARTURE New York New York New York Chicago New York New York Chicago ARRIVAL Rome Athens Tokyo Tokyo Nicosia Hong Kong Hong Kong CONNECTIONS 1 1 1 2 2 2 3 COST 740 690 860 1,740 970 1,190 2,070
For each returned row, the results show the starting departure city and the final destination city. It counts the number of connections needed rather than the total number of flight and adds up the total cost for all the flights.
SQL programming
65
| | | | | | | | | | | |
Table 17. Results of the previous query (continued) DEPARTURE Chicago Chicago Chicago Chicago ARRIVAL Toronto Boston Tokyo Hong Kong CONNECTIONS 1 1 2 3 FLIGHTS 1 0 3 4 TRAINS 1 2 0 0 COST 340 140 1,740 2,070
In this example, there are two parts of the common table expression that provide initialization values to the query: one for flights and one for trains. For each of the result rows, there are two recursive references to get from the previous arrival location to the next possible destination: one for continuing by air, the other for continuing by train. In the final results, you would see how many connections are needed and how many airline or train trips can be taken.
| Example: DEPTH FIRST and BREADTH FIRST options | The two examples here show the difference in the result set row order based on whether the recursion is | processed depth first or breadth first. | Note: The search clause is not supported for recursive views. You can define a view that contains a recursive common table expression to get this function. | | | | | | | | | | The option to determine the result using breadth first or depth first is a recursive relationship sort based on the recursive join column specified for the SEARCH BY clause. When the recursion is handled breadth first, all children are processed first, then all grandchildren, then all great grandchildren. When the recursion is handled depth first, the full recursive ancestry chain of one child is processed before going to the next child. In both of these cases, you specify an extra column name that is used by the recursive process to keep track of the depth first or breadth first ordering. This column must be used in the ORDER BY clause of the outer query to get the rows back in the specified order. If this column is not used in the ORDER BY, the DEPTH FIRST or BREADTH FIRST processing option is ignored.
| The selection of which column to use for the SEARCH BY column is important. To have any meaning in | the result, it must be the column that is used in the iterative fullselect to join from the initialization | fullselect. In this example, ARRIVAL is the column to use. | The following query returns that information: | WITH destinations (departure, arrival, connections, cost) AS (SELECT f.departure, f.arrival, 0, price | FROM flights f | WHERE f.departure = Chicago | UNION ALL | SELECT r.departure, b.arrival, r.connections + 1, | r.cost + b.price | FROM destinations r, flights b | WHERE r.arrival = b.departure) | SEARCH DEPTH FIRST BY arrival SET ordcol | | SELECT * FROM destinations | ORDER BY ordcol |
66
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
In this result data, you can see that all destinations that are generated from the Chicago-to-Miami row are listed before the destinations from the Chicago-to-Frankfort row. Next, you can run the same query but request the result to be ordered breadth first.
WITH destinations (departure, arrival, connections, cost) AS (SELECT f.departure, f.arrival, 0, price FROM flights f WHERE f.departure=Chicago UNION ALL SELECT r.departure, b.arrival, r.connections + 1, r.cost + b.price FROM destinations r, flights b WHERE r.arrival = b.departure) SEARCH BREADTH FIRST BY arrival SET ordcol SELECT * FROM destinations ORDER BY ordcol
In this result data, you can see that all the direct connections from Chicago are listed before the connecting flights. The data is identical to the results from the previous query, but in a breadth first order.
Example: Cyclic
The key to any recursive process, whether it is a recursive programming algorithm or querying recursive data, is that the recursion must be finite. If not, you will get into a never ending loop. The CYCLE option
SQL programming
67
| allows you to safeguard against cyclic data. Not only will it terminate repeating cycles but it also allows | you to optionally output a cycle mark indicator that may lead you to find cyclic data. | Note: The cycle clause is not supported for recursive views. You can define a view that contains a recursive common table expression to get this function. | | For a final example, suppose we have a cycle in the data. By adding one more row to the table, there is | now a flight from Cairo to Paris and one from Paris to Cairo. Without accounting for possible cyclic data | like this, it is quite easy to generate a query that will go into an infinite loop processing the data. | | | | | | | | | | | | | | | | | | The following query returns that information:
INSERT INTO FLIGHTS VALUES(Cairo, Paris, Euro Air, 1134, 440) WITH destinations (departure, arrival, connections, cost, itinerary) AS (SELECT f.departure, f.arrival, 1, price, CAST(f.departure CONCAT f.arrival AS VARCHAR(2000)) FROM flights f WHERE f.departure = New York UNION ALL SELECT r.departure, b.arrival, r.connections + 1 , r.cost + b.price, CAST(r.itinerary CONCAT b.arrival AS VARCHAR(2000)) FROM destinations r, flights b WHERE r.arrival = b.departure) CYCLE arrival SET cyclic_data TO 1 DEFAULT 0 SELECT departure, arrival, itinerary, cyclic_data FROM destinations ORDER BY cyclic_data
In this example, the ARRIVAL column is defined in the CYCLE clause as the column to use for detecting a cycle in the data. When a cycle is found, a special column, CYCLIC_DATA in this case, is set to the character value of 1 for the cycling row in the result set. All other rows will contain the default value of 0. When a cycle on the ARRIVAL column is found, processing will not proceed any further in the data so the infinite loop will not happen. To see if your data actually has a cyclic reference, the CYCLIC_DATA column can be referenced in the outer query.
68
When SQL encounters the UNION keyword, it processes each subselect to form an interim result table, then it combines the interim result table of each subselect and deletes duplicate rows to form a combined result table. You can use different clauses and techniques when coding select-statements. You can use UNION to eliminate duplicates when merging lists of values obtained from several tables. For example, you can obtain a combined list of employee numbers that includes: v People in department D11 v People whose assignments include projects MA2112, MA2113, and AD3111 The combined list is derived from two tables and contains no duplicates. To do this, specify:
SELECT EMPNO FROM CORPDATA.EMPLOYEE WHERE WORKDEPT = D11 UNION SELECT EMPNO FROM CORPDATA.EMPPROJACT WHERE PROJNO = MA2112 OR PROJNO = MA2113 OR PROJNO = AD3111 ORDER BY EMPNO
To better understand the results from these SQL statements, imagine that SQL goes through the following process: Step 1. SQL processes the first SELECT statement:
SELECT EMPNO FROM CORPDATA.EMPLOYEE WHERE WORKDEPT = D11
SQL programming
69
Step 3. SQL combines the two interim result tables, removes duplicate rows, and orders the result:
SELECT EMPNO FROM CORPDATA.EMPLOYEE WHERE WORKDEPT = D11 UNION SELECT EMPNO FROM CORPDATA.EMPPROJACT WHERE PROJNO=MA2112 OR PROJNO= MA2113 OR PROJNO= AD3111 ORDER BY EMPNO
The query returns a combined result table with values in ascending sequence.
EMPNO 000060 000150 000160 000170 000180 000190 000200 000210 000220 000230
70
When you use UNION: v Any ORDER BY clause must appear after the last subselect that is part of the union. In this example, the results are sequenced on the basis of the first selected column, EMPNO. The ORDER BY clause specifies that the combined result table is to be in collated sequence. ORDER BY is not allowed in a view. v A name may be specified on the ORDER BY clause if the result columns are named. A result column is named if the corresponding columns in each of the unioned select-statements have the same name. An AS clause can be used to assign a name to columns in the select list.
SELECT A + B AS X ... UNION SELECT X ... ORDER BY X
If the result columns are unnamed, use a positive integer to order the result. The number refers to the position of the expression in the list of expressions you include in your subselects.
SELECT A + B ... UNION SELECT X ... ORDER BY 1
To identify which subselect each row is from, you can include a constant at the end of the select list of each subselect in the union. When SQL returns your results, the last column contains the constant for the subselect that is the source of that row. For example, you can specify:
SELECT A, B, A1 ... UNION SELECT X, Y, B2...
When a row is returned, it includes a value (either A1 or B2) to indicate the table that is the source of the rows values. If the column names in the union are different, SQL uses the set of column names specified in the first subselect when interactive SQL displays or prints the results, or in the SQLDA resulting from processing an SQL DESCRIBE statement. Note: Sort sequence is applied after the fields across the UNION pieces are made compatible. The sort sequence is used for the distinct processing that implicitly occurs during UNION processing. Related concepts Sort sequences and normalization in SQL on page 99 A sort sequence defines how characters in a character set relate to each other when they are compared or ordered. Normalization allows you to compare strings that contain combining characters. Related reference Creating and using views on page 29 A view can be used to access data in one or more tables or views. You create a view by using a SELECT statement. Specifying the UNION ALL keyword: If you want to keep duplicates in the result of a UNION operation, specify the UNION ALL keyword instead of just UNION. This topic uses the same steps and example as Using the UNION keyword to combine subselects on page 68.
SQL programming
71
72
(SELECT PROJNO FROM CORPDATA.PROJECT UNION ALL SELECT PROJNO FROM CORPDATA.PROJECT) UNION ALL SELECT PROJNO FROM CORPDATA.EMPPROJACT
When you include the UNION ALL in the same SQL statement as a UNION operator, however, the result of the operation depends on the order of evaluation. Where there are no parentheses, evaluation is from left to right. Where parentheses are included, the parenthesized subselect is evaluated first, followed, from left to right, by the other parts of the statement.
To better understand the results from these SQL statements, imagine that SQL goes through the following process: Step 1. SQL processes the first SELECT statement:
SELECT EMPNO FROM CORPDATA.EMPLOYEE WHERE WORKDEPT = D11
SQL programming
73
Step 3. SQL takes the first interim result table, removes all of the rows that also appear in the second interim result table, removes duplicate rows, and orders the result:
SELECT EMPNO FROM CORPDATA.EMPLOYEE WHERE WORKDEPT = D11 EXCEPT SELECT EMPNO FROM CORPDATA.EMPPROJACT WHERE PROJNO=MA2112 OR PROJNO= MA2113 OR PROJNO= AD3111 ORDER BY EMPNO
74
This query returns a combined result table with values in ascending sequence.
EMPNO 000060 000200 000220 200170 200220
To better understand the results from these SQL statements, imagine that SQL goes through the following process: Step 1. SQL processes the first SELECT statement:
SELECT EMPNO FROM CORPDATA.EMPLOYEE WHERE WORKDEPT = D11
SQL programming
75
Step 3. SQL takes the first interim result table, compares it to the second interim result table, and returns the rows that exist in both tables minus any duplicate rows, and orders the results.
SELECT EMPNO FROM CORPDATA.EMPLOYEE WHERE WORKDEPT = D11 INTERSECT SELECT EMPNO FROM CORPDATA.EMPPROJACT WHERE PROJNO=MA2112 OR PROJNO= MA2113 OR PROJNO= AD3111 ORDER BY EMPNO
This query returns a combined result table with values in ascending sequence.
EMPNO 000150
76
SQL programming
77
If the full-select contains DISTINCT in the select list and a column in the select list contains numeric data that is not valid, the data is considered equal to a null value if the query is completed as a sort. If an existing index is used, the data is not considered equal to a null. The impact of data mapping errors on the ORDER BY clause depends on the situation: v If the data mapping error occurs while data is being assigned to a host variable in a SELECT INTO or FETCH statement, and that same expression is used in the ORDER BY clause, the result record is ordered based on the value of the expression. It is not ordered as if it were a null (higher than all other values). This is because the expression was evaluated before the assignment to the host variable is attempted. v If the data mapping error occurs while an expression in the select-list is being evaluated and the same expression is used in the ORDER BY clause, the result column is normally ordered as if it were a null value (higher than all other values). If the ORDER BY clause is implemented by using a sort, the result column is ordered as if it were a null value. If the ORDER BY clause is implemented by using an existing index, in the following cases, the result column is ordered based on the actual value of the expression in the index: The expression is a date column with a date format of *MDY, *DMY, *YMD, or *JUL, and a date conversion error occurs because the date is not within the valid range for dates. The expression is a character column and a character cannot be converted. The expression is a decimal column and a numeric value that is not valid is detected.
The INTO clause names the columns for which you specify values. The VALUES clause specifies a value for each column named in the INTO clause. The value you specify can be: v A constant. Inserts the value provided in the VALUES clause. v A null value. Inserts the null value, using the keyword NULL. The column must be defined as capable of containing a null value or an error occurs. v A host variable. Inserts the contents of a host variable. v A special register. Inserts a special register value; for example, USER. v An expression. Inserts the value that results from an expression. v A scalar fullselect inserts the value that is the result of running the select statement. v The DEFAULT keyword. Inserts the default value of the column. The column must have a default value defined for it or allow the NULL value, or an error occurs. You must provide a value in the VALUES clause for each column named in an INSERT statements column list. The column name list can be omitted if all columns in the table have a value provided in the
78
VALUES clause. If a column has a default value, the keyword DEFAULT may be used as a value in the VALUES clause. This causes the default value for the column to be placed in the column. It v v v is a good idea to name all columns into which you are inserting values because: Your INSERT statement is more descriptive. You can verify that you are providing the values in the proper order based on the column names. You have better data independence. The order in which the columns are defined in the table does not affect your INSERT statement.
| | |
If the column is defined to allow null values or to have a default, you do not need to name it in the column name list or specify a value for it. The default value is used. If the column is defined to have a default value, the default value is placed in the column. If DEFAULT was specified for the column definition without an explicit default value, SQL places the default value for that data type in the column. If the column does not have a default value defined for it, but is defined to allow the null value (NOT NULL was not specified in the column definition), SQL places the null value in the column. v For numeric columns, the default value is 0. v For fixed length character or graphic columns, the default is blanks. v For fixed length binary columns, the default is hexadecimal zeros. v For varying length character, graphic, or binary columns and for LOB columns, the default is a zero length string. v For date, time, and timestamp columns, the default value is the current date, time, or timestamp. When inserting a block of records, the default date/time value is extracted from the system when the block is written. This means that the column will be assigned the same default value for each row in the block. v For DataLink columns, the default value corresponds to DLVALUE(,URL,). v For distinct-type columns, the default value is the default value of the corresponding source type. v For ROWID columns or columns that are defined AS IDENTITY, the database manager generates a default value. When your program attempts to insert a row that duplicates another row already in the table, an error might occur. Multiple null values may or may not be considered duplicate values, depending on the option used when the index was created. v If the table has a primary key, unique key, or unique index, the row is not inserted. Instead, SQL returns an SQLCODE of -803. v If the table does not have a primary key, unique key, or unique index, the row can be inserted without error. If SQL finds an error while running the INSERT statement, it stops inserting data. If you specify COMMIT(*ALL), COMMIT(*CS), COMMIT(*CHG), or COMMIT(*RR), no rows are inserted. Rows already inserted by this statement, in the case of INSERT with a select-statement or blocked insert, are deleted. If you specify COMMIT(*NONE), any rows already inserted are not deleted. A table created by SQL is created with the Reuse Deleted Records parameter of *YES. This allows the database manager to reuse any rows in the table that were marked as deleted. The CHGPF command can be used to change the attribute to *NO. This causes INSERT to always add rows to the end of the table. The order in which rows are inserted does not guarantee the order in which they will be retrieved. If the row is inserted without error, the SQLERRD(3) field of the SQLCA has a value of 1. Note: For blocked INSERT or for INSERT with select-statement, more than one row can be inserted. The number of rows inserted is reflected in SQLERRD(3) in the SQLCA. It is also available from the ROW_COUNT diagnostics item in the GET DIAGNOSTICS statement.
SQL programming
79
You can also insert multiple rows into a table using the VALUES clause. The following example inserts two rows into the PROJECT table. Values for the Project number (PROJNO) , Project name (PROJNAME), Department number (DEPTNO), and Responsible employee (RESPEMP) are given in the values list. The value for the Project start date (PRSTDATE) uses the current date. The rest of the columns in the table that are not listed in the column list are assigned their default value.
INSERT INTO PROJECT (PROJNO, PROJNAME, DEPTNO, RESPEMP, PRSTDATE) VALUES(HG0023, NEW NETWORK, E11, 200280, CURRENT DATE), (HG0024, NETWORK PGM, E11", 200310, CURRENT DATE)
The select-statement embedded in the INSERT statement is no different from the select-statement you use to retrieve data. With the exception of FOR READ ONLY, FOR UPDATE, or the OPTIMIZE clause, you can use all the keywords, functions, and techniques used to retrieve data. SQL inserts all the rows that meet the search conditions into the table you specify. Inserting rows from one table into another table does not affect any existing rows in either the source table or the target table. You should consider the following when inserting multiple rows into a table: Notes: 1. The number of columns implicitly or explicitly listed in the INSERT statement must equal the number of columns listed in the select-statement. 2. The data in the columns you are selecting must be compatible with the columns you are inserting into when using the INSERT with select-statement. 3. In the event the select-statement embedded in the INSERT returns no rows, an SQLCODE of 100 is returned to alert you that no rows were inserted. If you successfully insert rows, the
80
SQLERRD(3) field of the SQLCA has an integer representing the number of rows SQL actually inserted. This value is also available from the ROW_COUNT diagnostics item in the GET DIAGNOSTICS statement. 4. If SQL finds an error while running the INSERT statement, SQL stops the operation. If you specify COMMIT (*CHG), COMMIT(*CS), COMMIT (*ALL), or COMMIT(*RR), nothing is inserted into the table and a negative SQLCODE is returned. If you specify COMMIT(*NONE), any rows inserted before the error remain in the table.
DSTRUCT is a host structure array with five elements that is declared in the program. The five elements correspond to EMPNO, FIRSTNME, MIDINIT, LASTNAME, and WORKDEPT. DSTRUCT has a dimension of at least ten to accommodate inserting ten rows. ISTRUCT is a host structure array that is declared in the program. ISTRUCT has a dimension of at least ten small integer fields for the indicators. Blocked INSERT statements are supported for non-distributed SQL applications and for distributed applications where both the application server and the application requester are System i products. Related concepts Embedded SQL programming
SQL programming
81
ALTER TABLE CORPDATA.PROJECT ADD CONSTRAINT RESP_EMP_EXISTS FOREIGN KEY (RESPEMP) REFERENCES CORPDATA.EMPLOYEE ON DELETE RESTRICT
Notice that the parent table columns are not specified in the REFERENCES clause. The columns are not required to be specified as long as the referenced table has a primary key or eligible unique key which can be used as the parent key. Every row inserted into the PROJECT table must have a value of DEPTNO that is equal to some value of DEPTNO in the department table. (The null value is not allowed because DEPTNO in the project table is defined as NOT NULL.) The row must also have a value of RESPEMP that is either equal to some value of EMPNO in the employee table or is null. The following INSERT statement fails because there is no matching DEPTNO value (A01) in the DEPARTMENT table.
INSERT INTO CORPDATA.PROJECT (PROJNO, PROJNAME, DEPTNO, RESPEMP) VALUES (AD3120, BENEFITS ADMIN, A01, 000010)
Likewise, the following INSERT statement is unsuccessful because there is no EMPNO value of 000011 in the EMPLOYEE table.
INSERT INTO CORPDATA.PROJECT (PROJNO, PROJNAME, DEPTNO, RESPEMP) VALUES (AD3130, BILLING, D21, 000011)
The following INSERT statement completes successfully because there is a matching DEPTNO value of E01 in the DEPARTMENT table and a matching EMPNO value of 000010 in the EMPLOYEE table.
INSERT INTO CORPDATA.PROJECT (PROJNO, PROJNAME, DEPTNO, RESPEMP) VALUES (AD3120, BENEFITS ADMIN, E01, 000010)
In this case, a value is generated by the system for the identity column automatically. You can also write this statement using the DEFAULT keyword:
INSERT INTO ORDERS (SHIPPED_TO, ORDER_DATE, ORDERNO) VALUES (BME TOOL, 2002-02-04, DEFAULT)
After the insert, you can use the IDENTITY_VAL_LOCAL function to determine the value that the system assigned to the column. Sometimes a value for an identity column is specified by the user, such as in this INSERT statement using a SELECT:
INSERT INTO ORDERS OVERRIDING USER VALUE (SELECT * FROM TODAYS_ORDER)
In this case, OVERRIDING USER VALUE tells the system to ignore the value provided for the identity column from the SELECT and to generate a new value for the identity column. OVERRIDING USER VALUE must be used if the identity column was created with the GENERATED ALWAYS clause; it is optional for GENERATED BY DEFAULT. If OVERRIDING USER VALUE is not specified for a GENERATED BY DEFAULT identity column, the value provided for the column in the SELECT is inserted.
82
You can force the system to use the value from the select for a GENERATED ALWAYS identity column by specifying OVERRIDING SYSTEM VALUE. For example, issue the following statement:
INSERT INTO ORDERS OVERRIDING SYSTEM VALUE (SELECT * FROM TODAYS_ORDER)
This INSERT statement uses the value from SELECT; it does not generate a new value for the identity column. You cannot provide a value for an identity column created using GENERATED ALWAYS without using the OVERRIDING SYSTEM VALUE clause. Related reference Creating and altering an identity column on page 21 Every time a row is added to a table with an identity column, the identity column value for the new row is generated by the system. Scalar functions
Suppose that an employee is relocated. To update the CORPDATA.EMPLOYEE table to reflect the move, run the following statement:
UPDATE CORPDATA.EMPLOYEE SET JOB = :PGM-CODE, PHONENO = :PGM-PHONE WHERE EMPNO = :PGM-SERIAL
Use the SET clause to specify a new value for each column that you want to update. The SET clause names the columns that you want updated and provides the values that you want them changed to. You can specify the following types of values: v A column name. Replace the columns current value with the contents of another column in the same row. v A constant. Replace the columns current value with the value provided in the SET clause. v A null value. Replace the columns current value with the null value, using the keyword NULL. The column must be defined as capable of containing a null value when the table was created, or an error occurs. v A host variable. Replace the columns current value with the contents of a host variable. v A special register. Replace the columns current value with a special register value; for example, USER. v An expression. Replace the columns current value with the value that results from an expression. v A scalar fullselect. Replace the columns current value with the value that the subquery returns. v The DEFAULT keyword. Replace the columns current value with the default value of the column. The column must have a default value defined for it or allow the NULL value, or an error occurs. The following UPDATE statement uses many different values:
UPDATE WORKTABLE SET COL1 = ASC, COL2 = NULL, COL3 = :FIELD3,
SQL programming
83
COL4 = CURRENT TIME, COL5 = AMT - 6.00, COL6 = COL7 WHERE EMPNO = :PGM-SERIAL
To identify the rows to be updated, use the WHERE clause: v To update a single row, use a WHERE clause that selects only one row. v To update several rows, use a WHERE clause that selects only the rows you want to update. You can omit the WHERE clause. If you do, SQL updates each row in the table or view with the values you supply. If the database manager finds an error while running your UPDATE statement, it stops updating and returns a negative SQLCODE. If you specify COMMIT(*ALL), COMMIT(*CS), COMMIT(*CHG), or COMMIT(*RR), no rows in the table are changed (rows already changed by this statement, if any, are restored to their previous values). If COMMIT(*NONE) is specified, any rows already changed are not restored to previous values. If the database manager cannot find any rows that meet the search condition, an SQLCODE of +100 is returned. Note: The UPDATE statement may have updated more than one row. The number of rows updated is reflected in SQLERRD(3) of the SQLCA. This value is also available from the ROW_COUNT diagnostics item in the GET DIAGNOSTICS statement. The SET clause of an UPDATE statement can be used in many ways to determine the actual values to be set in each row being updated. The following example lists each column with its corresponding value:
UPDATE EMPLOYEE SET WORKDEPT = D11, PHONENO = 7213, JOB = DESIGNER WHERE EMPNO = 000270
You can also write this UPDATE statement by specifying all of the columns and then all of the values:
UPDATE EMPLOYEE SET (WORKDEPT, PHONENO, JOB) = (D11, 7213, DESIGNER) WHERE EMPNO = 000270
This same technique can be used to update a list of columns with multiple values returned from a single select.
84
This update will update all of the rows in CL_SCHED with the values from MYCOPY.
Update rules
The action taken on dependent tables when an UPDATE is performed on a parent table depends on the update rule specified for the referential constraint. If no update rule was defined for a referential constraint, the UPDATE NO ACTION rule is used. UPDATE NO ACTION Specifies that the row in the parent table can be updated if no other row depends on it. If a dependent row exists in the relationship, the UPDATE fails. The check for dependent rows is performed at the end of the statement. UPDATE RESTRICT Specifies that the row in the parent table can be updated if no other row depends on it. If a dependent row exists in the relationship, the UPDATE fails. The check for dependent rows is performed immediately. The subtle difference between the RESTRICT rule and the NO ACTION rule is easiest seen when looking at the interaction of triggers and referential constraints. Triggers can be defined to fire either before or after an operation (an UPDATE statement, in this case). A before trigger fires before the UPDATE is performed and therefore before any checking of constraints. An after trigger is fired after the UPDATE is performed, and after a constraint rule of RESTRICT (where checking is performed immediately), but before a constraint rule of NO ACTION (where checking is performed at the end of the statement). The triggers and rules occur in the following order: 1. A before trigger is fired before the UPDATE and before a constraint rule of RESTRICT or NO ACTION. 2. An after trigger is fired after a constraint rule of RESTRICT, but before a NO ACTION rule. If you are updating a dependent table, any non-null foreign key values that you change must match the primary key for each relationship in which the table is a dependent. For example, department numbers in the employee table depend on the department numbers in the department table. You can assign an employee to no department (the null value), but not to a department that does not exist. If an UPDATE against a table with a referential constraint fails, all changes made during the update operation are undone. Related reference Journaling on page 108 The DB2 UDB for iSeries journal support provides an audit trail and forward and backward recovery.
SQL programming
85
Commitment control on page 109 The DB2 UDB for iSeries commitment control support provides a means for processing a group of database changes, such as update, insert, data definition language (DDL), or delete operations, as a single unit of work (also referred to as a transaction). Examples: UPDATE rules: These examples illustrate the UPDATE rules for tables with referential constraints. For example, you cannot update a department number from the DEPARTMENT table if the department is still responsible for a project that is described by a dependent row in the PROJECT table. The following UPDATE statement fails because the PROJECT table has rows that are dependent on DEPARTMENT.DEPTNO that has a value of D01 (the row targeted by the WHERE statement). If this UPDATE statement were to be allowed, the referential constraint between the PROJECT and DEPARTMENT tables would be broken.
UPDATE CORPDATA.DEPARTMENT SET DEPTNO = D99 WHERE DEPTNAME = DEVELOPMENT CENTER
The following statement fails because it violates the referential constraint that exists between the primary key DEPTNO in DEPARTMENT and the foreign key DEPTNO in PROJECT:
UPDATE CORPDATA.PROJECT SET DEPTNO = D00 WHERE DEPTNO = D01;
The statement attempts to change all department numbers of D01 to department number D00. Because D00 is not a value of the primary key DEPTNO in DEPARTMENT, the statement fails.
A value is generated by the system for the identity column automatically. You can override having the system generate a value by using the OVERRIDING SYSTEM VALUE clause:
UPDATE ORDERS OVERRIDING SYSTEM VALUE SET (ORDERNO, ORDER_DATE)= (553, 2002-02-05) WHERE SHIPPED_TO = BME TOOL
Related reference Creating and altering an identity column on page 21 Every time a row is added to a table with an identity column, the identity column value for the new row is generated by the system.
86
points to the row you want to update. If a FOR UPDATE OF, an ORDER BY, a FOR READ ONLY, or a SCROLL clause without the DYNAMIC clause is not specified, all columns can be updated. If a multiple-row FETCH statement has been specified and run, the cursor is positioned on the last row of the block. Therefore, if the WHERE CURRENT OF clause is specified on the UPDATE statement, the last row in the block is updated. If a row within the block must be updated, the program must first position the cursor on that row. Then the UPDATE WHERE CURRENT OF can be specified. Consider the following example:
Table 21. Updating a table Scrollable Cursor SQL Statement EXEC SQL DECLARE THISEMP DYNAMIC SCROLL CURSOR FOR SELECT EMPNO, WORKDEPT, BONUS FROM CORPDATA.EMPLOYEE WHERE WORKDEPT = D11 FOR UPDATE OF BONUS END-EXEC. EXEC SQL OPEN THISEMP END-EXEC. EXEC SQL WHENEVER NOT FOUND GO TO CLOSE-THISEMP END-EXEC. EXEC SQL FETCH NEXT FROM THISEMP FOR 5 ROWS INTO :DEPTINFO :IND-ARRAY END-EXEC. ... determine if any employees in department D11 receive a bonus less than $500.00. If so, update that record to the new minimum of $500.00. EXEC SQL FETCH RELATIVE :NUMBACK FROM THISEMP END-EXEC. EXEC SQL UPDATE CORPDATA.EMPLOYEE SET BONUS = 500 WHERE CURRENT OF THISEMP END-EXEC. EXEC SQL FETCH RELATIVE :NUMBACK FROM THISEMP FOR 5 ROWS INTO :DEPTINFO :IND-ARRAY END-EXEC. ... positions to the record in the block to update by fetching in the reverse order. ... updates the bonus for the employee in department D11 that is under the new $500.00 minimum. DEPTINFO and IND-ARRAY are declared in the program as a host structure array and an indicator array. Comments
... positions to the beginning of the same block that was already fetched and fetches the block again. (NUMBACK -(5 NUMBACK - 1))
SQL programming
87
Table 21. Updating a table (continued) Scrollable Cursor SQL Statement ... branch back to determine if any more employees in the block have a bonus under $500.00. ... branch back to fetch and process the next block of rows. CLOSE-THISEMP. EXEC SQL CLOSE THISEMP END-EXEC. Comments
Related reference Using a cursor on page 215 When SQL runs a SELECT statement, the resulting rows comprise the result table. A cursor provides a way to access a result table.
For example, suppose that department D11 is moved to another site. You delete each row in the CORPDATA.EMPLOYEE table with a WORKDEPT value of D11 as follows:
DELETE FROM CORPDATA.EMPLOYEE WHERE WORKDEPT = D11
The WHERE clause tells SQL which rows you want to delete from the table. SQL deletes all the rows that satisfy the search condition from the base table. Deleting rows from a view deletes the rows from the base table. You can omit the WHERE clause, but it is best to include one, because a DELETE statement without a WHERE clause deletes all the rows from the table or view. To delete a table definition as well as the table contents, issue the DROP statement. If SQL finds an error while running your DELETE statement, it stops deleting data and returns a negative SQLCODE. If you specify COMMIT(*ALL), COMMIT(*CS), COMMIT(*CHG), or COMMIT(*RR), no rows in the table are deleted (rows already deleted by this statement, if any, are restored to their previous values). If COMMIT(*NONE) is specified, any rows already deleted are not restored to their previous values. If SQL cannot find any rows that satisfy the search condition, an SQLCODE of +100 is returned. Note: The DELETE statement may have deleted more than one row. The number of rows deleted is reflected in SQLERRD(3) of the SQLCA. This value is also available from the ROW_COUNT diagnostics item in the GET DIAGNOSTICS statement. Related reference DROP DELETE
88
89
SQLCA contains the number of rows that were affected by referential constraints in all tables. The SQLERRD(3) value is also available from the ROW_COUNT item in the GET DIAGNOSTICS statement. The SQLERRD(5) value is available from the DB2_ROW_COUNT_SECONDARY item. The subtle difference between RESTRICT and NO ACTION rules is easiest seen when looking at the interaction of triggers and referential constraints. Triggers can be defined to fire either before or after an operation (a DELETE statement, in this case). A before trigger fires before the DELETE is performed and therefore before any checking of constraints. An after trigger is fired after the DELETE is performed, and after a constraint rule of RESTRICT (where checking is performed immediately), but before a constraint rule of NO ACTION (where checking is performed at the end of the statement). The triggers and rules occur in the following order: 1. A before trigger is fired before the DELETE and before a constraint rule of RESTRICT or NO ACTION. 2. An after trigger is fired after a constraint rule of RESTRICT, but before a NO ACTION rule. Example: DELETE rules: Suppose that deleting a department from the DEPARTMENT table sets WORKDEPT in the EMPLOYEE table to null for every employee assigned to that department. Consider the following DELETE statement:
DELETE FROM CORPDATA.DEPARTMENT WHERE DEPTNO = E11
Given the tables and the data in the DB2 Universal Database for iSeries sample tables on page 281, one row is deleted from table DEPARTMENT, and table EMPLOYEE is updated to set the value of WORKDEPT to its default wherever the value was E11. A question mark (?) in the following sample data reflects the null value. The results appear as follows:
Table 22. DEPARTMENT table. Contents of the table after the DELETE statement is complete. DEPTNO A00 B01 C01 D01 D11 D21 E01 E21 F22 G22 H22 I22 J22 DEPTNAME SPIFFY COMPUTER SERVICE DIV. PLANNING INFORMATION CENTER DEVELOPMENT CENTER MANUFACTURING SYSTEMS ADMINISTRATION SYSTEMS SUPPORT SERVICES SOFTWARE SUPPORT BRANCH OFFICE F2 BRANCH OFFICE G2 BRANCH OFFICE H2 BRANCH OFFICE I2 BRANCH OFFICE J2 MGRNO 000010 000020 000030 ? 000060 000070 000050 000100 ? ? ? ? ? ADMRDEPT A00 A00 A00 A00 D01 D01 A00 E01 E01 E01 E01 E01 E01
Note that there were no cascaded delete operations in the DEPARTMENT table because no department reported to department E11. Below are the snapshots of one affected portion of the EMPLOYEE table before and after the DELETE statement is completed.
90
Table 23. Partial EMPLOYEE table. Partial contents before the DELETE statement. EMPNO 000230 000240 000250 000260 000270 000280 000290 000300 000310 000320 000330 000340 FIRSTNME JAMES SALVATORE DANIEL SYBIL MARIA ETHEL JOHN PHILIP MAUDE RAMLAL WING JASON R MI J M S P L R R X F V LASTNAME JEFFERSON MARINO SMITH JOHNSON PEREZ SCHNEIDER PARKER SMITH SETRIGHT MEHTA LEE GOUNOT WORKDEPT PHONENO D21 D21 D21 D21 D21 E11 E11 E11 E11 E21 E21 E21 2094 3780 0961 8953 9001 0997 4502 2095 3332 9990 2103 5696 HIREDATE 1966-11-21 1979-12-05 1960-10-30 1975-09-11 1980-09-30 1967-03-24 1980-05-30 1972-06-19 1964-09-12 1965-07-07 1976-02-23 1947-05-05
Table 24. Partial EMPLOYEE table. Partial contents after the DELETE statement. EMPNO 000230 000240 000250 000260 000270 000280 000290 000300 000310 000320 000330 000340 FIRSTNME JAMES SALVATORE DANIEL SYBIL MARIA ETHEL JOHN PHILIP MAUDE RAMLAL WING JASON R MI J M S P L R R X F V LASTNAME JEFFERSON MARINO SMITH JOHNSON PEREZ SCHNEIDER PARKER SMITH SETRIGHT MEHTA LEE GOUNOT WORKDEPT D21 D21 D21 D21 D21 ? ? ? ? E21 E21 E21 PHONENO 2094 3780 0961 8953 9001 0997 4502 2095 3332 9990 2103 5696 HIREDATE 1966-11-21 1979-12-05 1960-10-30 1975-09-11 1980-09-30 1967-03-24 1980-05-30 1972-06-19 1964-09-12 1965-07-07 1976-02-23 1947-05-05
Related reference DB2 Universal Database for iSeries sample tables on page 281 These sample tables are referred to and used in the SQL programming and the SQL reference topic collections.
Using subqueries
You can use subqueries in a search condition as another way to select data. Subqueries can be used anywhere an expression can be used. Conceptually, a subquery is evaluated whenever a new row or a group of rows must be processed. In fact, if the subquery is the same for every row or group, it is evaluated only once. Subqueries like this are said to be uncorrelated. Some subqueries return different values from row to row or group to group. The mechanism that allows this is called correlation, and the subqueries are said to be correlated. Related reference
SQL programming
91
Expressions in the WHERE clause on page 38 An expression in a WHERE clause names or specifies something that you want to compare to something else. Defining complex search conditions on page 50 In addition to the basic comparison predicates, such as = and >, a search condition can contain any of these predicates: BETWEEN, IN, EXISTS, IS NULL, and LIKE.
But you cannot go further because the CORPDATA.EMPLOYEE table does not include project number data. You do not know which employees are working on project MA2100 without issuing another SELECT statement against the CORPDATA.EMP_ACT table. With SQL, you can nest one SELECT statement within another to solve this problem. The inner SELECT statement is called a subquery. The SELECT statement surrounding the subquery is called the outer-level SELECT. Using a subquery, you can issue just one SQL statement to retrieve the employee numbers, names, and job codes for employees who work on the project MA2100:
SELECT EMPNO, LASTNAME, JOB FROM CORPDATA.EMPLOYEE WHERE EMPNO IN (SELECT EMPNO FROM CORPDATA.EMPPROJACT WHERE PROJNO = MA2100)
To better understand what will result from this SQL statement, imagine that SQL goes through the following process: Step 1: SQL evaluates the subquery to obtain a list of EMPNO values:
(SELECT EMPNO FROM CORPDATA.EMPPROJACT WHERE PROJNO= MA2100)
Step 2: The interim result table then serves as a list in the search condition of the outer-level SELECT statement. Essentially, this is the statement that is run:
SELECT EMPNO, LASTNAME, JOB FROM CORPDATA.EMPLOYEE WHERE EMPNO IN (000010, 000110)
92
Subqueries and search conditions: A subquery can be part of a search condition. The search condition is in the form of operand operator operand. Either operand can be a subquery. In the following example, the first operand is EMPNO and operator is IN. The search condition can be part of a WHERE or HAVING clause. The clause can include more than one search condition that contains a subquery. A search condition that contains a subquery, like any other search condition, can be enclosed in parentheses, can be preceded by the keyword NOT, and can be linked to other search conditions through the keywords AND and OR. For example, the WHERE clause of a query can look something like this:
WHERE (subquery1) = X AND (Y > SOME (subquery2) OR Z = 100)
Subqueries can also appear in the search conditions of other subqueries. Such subqueries are said to be nested at some level of nesting. For example, a subquery within a subquery within an outer-level SELECT is nested at a nesting level of two. SQL allows nesting down to a nesting level of 32. Usage notes on subqueries: Here are some considerations for using subqueries to refine your search conditions. 1. When nesting SELECT statements, you can use as many as you need to satisfy your requirements (1 to 255 subqueries), although performance is slower for each additional subquery. | | | | | | | | | | 2. For predicates that use the keywords ALL, ANY, SOME, or EXISTS, the number of rows returned from the subquery can vary from zero to many. For all other subqueries, the number of rows returned must be zero or one. 3. For the following predicates, a row fullselect can be used for the subquery. This means that the subquery can return more than one value for a row. v Basic predicate with equal or not equal comparisons v Quantified predicates using =ANY, =ALL, and =SOME v IN and NOT IN predicates
If a row fullselect is used: v The select list must not contain SELECT *. Explicit values must be specified. v A row fullselect must be compared to a row expression. A row expression is a list of values | enclosed in parentheses. There must be the same number of values returned from the subquery as | there are in the row expression. | v The row expression for an IN or NOT IN predicate cannot contain an untyped parameter marker. | Use CAST to supply a result data type for these parameter markers. | v The subquery cannot contain UNION, EXCEPT, or INTERSECT or a correlated reference. | | 4. A subquery cannot include the ORDER BY, FOR READ ONLY, FETCH FIRST n ROWS, UPDATE, or OPTIMIZE clause. | Including subqueries in the WHERE or HAVING clause: You can include a subquery in a WHERE or HAVING clause by using a basic or quantified comparison, the IN keyword, or the EXISTS keyword.
SQL programming
93
Basic comparisons | | | | | | | | | | | You can use a subquery before or after any of the comparison operators. The subquery can return only one row. It can return multiple values for the row if the equal or not equal operators are used. SQL compares each value from the subquery row with the corresponding value on the other side of the comparison operator. For example, suppose that you want to find the employee numbers, names, and salaries for employees whose education level is higher than the average education level throughout the company.
SELECT EMPNO, LASTNAME, SALARY FROM CORPDATA.EMPLOYEE WHERE EDLEVEL > (SELECT AVG(EDLEVEL) FROM CORPDATA.EMPLOYEE)
| SQL first evaluates the subquery and then substitutes the result in the WHERE clause of the SELECT statement. In this example, the result is the company-wide average educational level. Besides returning a single row, a subquery can return no rows. If it does, the result of the compare is unknown. Quantified comparisons (ALL, ANY, and SOME) You can use a subquery after a comparison operator followed by the keyword ALL, ANY, or SOME. When used in this way, the subquery can return zero, one, or many rows, including null values. You can use ALL, ANY, and SOME in the following ways: v Use ALL to indicate that the value you supplied must compare in the indicated way to ALL the rows the subquery returns. For example, suppose you use the greater-than comparison operator with ALL:
... WHERE expression > ALL (subquery)
To satisfy this WHERE clause, the value of the expression must be greater than the result for each of the rows (that is, greater than the highest value) returned by the subquery. If the subquery returns an empty set (that is, no rows were selected), the condition is satisfied. v Use ANY or SOME to indicate that the value you supplied must compare in the indicated way to at least one of the rows the subquery returns. For example, suppose you use the greater-than comparison operator with ANY:
... WHERE expression > ANY (subquery)
To satisfy this WHERE clause, the value in the expression must be greater than at least one of the rows (that is, greater than the lowest value) returned by the subquery. If what the subquery returns is the empty set, the condition is not satisfied. Note: The results when a subquery returns one or more null values may surprise you, unless you are familiar with formal logic. IN keyword You can use IN to say that the value in the expression must be among the rows returned by the subquery. Using IN is equivalent to using =ANY or =SOME. Using ANY and SOME were previously described. You can also use the IN keyword with the NOT keyword in order to select rows when the value is not among the rows returned by the subquery. For example, you can use:
... WHERE WORKDEPT NOT IN (SELECT ...)
EXISTS keyword In the subqueries presented so far, SQL evaluates the subquery and uses the result as part of the WHERE clause of the outer-level SELECT. In contrast, when you use the keyword EXISTS, SQL checks whether the subquery returns one or more rows. If it does, the condition is satisfied. If it returns no rows, the condition is not satisfied. For example:
94
SELECT EMPNO,LASTNAME FROM CORPDATA.EMPLOYEE WHERE EXISTS (SELECT * FROM CORPDATA.PROJECT WHERE PRSTDATE > 1982-01-01);
In the example, the search condition is true if any project represented in the CORPDATA.PROJECT table has an estimated start date that is later than January 1, 1982. This example does not show the full power of EXISTS, because the result is always the same for every row examined for the outer-level SELECT. As a consequence, either every row appears in the results, or none appear. In a more powerful example, the subquery itself would be correlated, and change from row to row. As shown in the example, you do not need to specify column names in the select-list of the subquery of an EXISTS clause. Instead, you should code SELECT *. You can also use the EXISTS keyword with the NOT keyword in order to select rows when the data or condition you specify does not exist. You can use the following:
... WHERE NOT EXISTS (SELECT ...)
Correlated subqueries
A correlated subquery is a subquery that SQL might need to re-evaluate when it examines each new row (the WHERE clause) or each group of rows (the HAVING clause) in the outer-level SELECT statement. Correlated names and references: A correlated reference can appear in a search condition in a subquery. The reference is always in the form of X.C, where X is a correlation name and C is the name of a column in the table that X represents. You can define a correlation name for any table appearing in a FROM clause. A correlation name provides a unique name for a table in a query. The same table name can be used many times within a query and its nested subselects. Specifying different correlation names for each table reference makes it possible to uniquely designate which table a column refers to. The correlation name is defined in the FROM clause of a query. This query can be the outer-level SELECT, or any of the subqueries that contain the one with the reference. Suppose, for example, that a query contains subqueries A, B, and C, and that A contains B and B contains C. Then a correlation name used in C can be defined in B, A, or the outer-level SELECT. To define a correlation name, include the correlation name after the table name. Leave one or more blanks between a table name and its correlation name, and place a comma after the correlation name if it is followed by another table name. The following FROM clause defines the correlation names TA and TB for the tables TABLEA and TABLEB, and no correlation name for the table TABLEC.
FROM TABLEA TA, TABLEC, TABLEB TB
Any number of correlated references can appear in a subquery. For example, one correlated name in a search condition can be defined in the outer-level SELECT, while another can be defined in a containing subquery. Before the subquery is executed, a value from the referenced column is always substituted for the correlated reference. Example: Correlated subquery in a WHERE clause: Suppose that you want a list of all the employees whose education levels are higher than the average education levels in their respective departments. To get this information, SQL must search the CORPDATA.EMPLOYEE table.
SQL programming
95
For each employee in the table, SQL needs to compare the employees education level to the average education level for the employees department. In the subquery, you tell SQL to calculate the average education level for the department number in the current row. For example:
SELECT EMPNO, LASTNAME, WORKDEPT, EDLEVEL FROM CORPDATA.EMPLOYEE X WHERE EDLEVEL > (SELECT AVG(EDLEVEL) FROM CORPDATA.EMPLOYEE WHERE WORKDEPT = X.WORKDEPT)
A correlated subquery looks like an uncorrelated one, except for the presence of one or more correlated references. In the example, the single correlated reference is the occurrence of X.WORKDEPT in the subselects FROM clause. Here, the qualifier X is the correlation name defined in the FROM clause of the outer SELECT statement. In that clause, X is introduced as the correlation name of the table CORPDATA.EMPLOYEE. Now, consider what happens when the subquery is executed for a given row of CORPDATA.EMPLOYEE. Before it is executed, the occurrence of X.WORKDEPT is replaced with the value of the WORKDEPT column for that row. Suppose, for example, that the row is for CHRISTINE I HAAS. Her work department is A00, which is the value of WORKDEPT for this row. The subquery executed for this row is:
(SELECT AVG(EDLEVEL) FROM CORPDATA.EMPLOYEE WHERE WORKDEPT = A00)
Thus, for the row considered, the subquery produces the average education level of Christines department. This is then compared in the outer statement to Christines own education level. For some other row for which WORKDEPT has a different value, that value appears in the subquery in place of A00. For example, for the row for MICHAEL L THOMPSON, this value is B01, and the subquery for his row delivers the average education level for department B01. The result table produced by the query has the following values.
Table 25. Result set for previous query EMPNO 000010 000030 000070 000090 000110 000160 000180 000210 000220 000240 000260 000280 000320 000340 200010 200220 LASTNAME HAAS KWAN PULASKI HENDERSON LUCCHESSI PIANKA SCOUTTEN JONES LUTZ MARINO JOHNSON SCHNEIDER MEHTA GOUNOT HEMMINGER JOHN WORKDEPT A00 C01 D21 E11 A00 D11 D11 D11 D11 D21 D21 E11 E21 E21 A00 D11 EDLEVEL 18 20 16 16 19 17 17 17 18 17 16 17 16 16 18 18
96
Table 25. Result set for previous query (continued) EMPNO 200240 200280 200340 LASTNAME MONTEVERDE SCHWARTZ ALONZO WORKDEPT D21 E11 E21 EDLEVEL 17 17 16
Example: Correlated subquery in a HAVING clause: Suppose that you want a list of all the departments whose average salaries are higher than the average salaries of their areas (all departments whose WORKDEPT begins with the same letter belong to the same area). To get this information, SQL must search the CORPDATA.EMPLOYEE table. For each department in the table, SQL compares the departments average salary to the average salary of the area. In the subquery, SQL calculates the average salary for the area of the department in the current group. For example:
SELECT WORKDEPT, DECIMAL(AVG(SALARY),8,2) FROM CORPDATA.EMPLOYEE X GROUP BY WORKDEPT HAVING AVG(SALARY) > (SELECT AVG(SALARY) FROM CORPDATA.EMPLOYEE WHERE SUBSTR(X.WORKDEPT,1,1) = SUBSTR(WORKDEPT,1,1))
Consider what happens when the subquery is executed for a given department of CORPDATA.EMPLOYEE. Before it is executed, the occurrence of X.WORKDEPT is replaced with the value of the WORKDEPT column for that group. Suppose, for example, that the first group selected has A00 for the value of WORKDEPT. The subquery executed for this group is:
(SELECT AVG(SALARY) FROM CORPDATA.EMPLOYEE WHERE SUBSTR(A00,1,1) = SUBSTR(WORKDEPT,1,1))
Thus, for the group considered, the subquery produces the average salary for the area. This value is then compared in the outer statement to the average salary for department A00. For some other group for which WORKDEPT is B01, the subquery results in the average salary for the area where department B01 belongs. The result table produced by the query has the following values.
WORKDEPT D21 E01 E21 AVG SALARY 25668.57 40175.00 24086.66
Example: Correlated subquery in a select-list: Suppose that you want a list of all the departments, including the department name, number, and managers name. Department names and numbers are found in the CORPDATA.DEPARTMENT table. However, DEPARTMENT has only the managers number, not the managers name. To find the name of the manager for each department, you need to find the employee number from the EMPLOYEE table that
SQL programming
97
matches the manager number in the DEPARTMENT table and return the name for the row that matches. Only departments that currently have managers assigned are to be returned. Execute the following SQL statement:
SELECT DEPTNO, DEPTNAME, (SELECT FIRSTNME CONCAT CONCAT MIDINIT CONCAT CONCAT LASTNAME FROM EMPLOYEE X WHERE X.EMPNO = Y.MGRNO) AS MANAGER_NAME FROM DEPARTMENT Y WHERE MGRNO IS NOT NULL
For each row returned for DEPTNO and DEPTNAME, the system finds where EMPNO = MGRNO and returns the managers name. The result table produced by the query has the following values.
Table 26. Result set for previous query DEPTNO A00 B01 C01 D11 D21 E01 E11 E21 DEPTNAME SPIFFY COMPUTER SERVICE DIV. PLANNING INFORMATION CENTER MANUFACTURING SYSTEMS ADMINISTRATION SYSTEMS SUPPORT SERVICES OPERATIONS SOFTWARE SUPPORT MANAGER_NAME CHRISTINE I HAAS MICHAEL L THOMPSON SALLY A KWAN IRVING F STERN EVA D PULASKI JOHN B GEYER EILEEN W HENDERSON THEODORE Q SPENSER
Example: Correlated subquery in an UPDATE statement: When you use a correlated subquery in an UPDATE statement, the correlation name refers to the rows that you want to update. For example, when all activities of a project must be completed before September 1983, your department considers that project to be a priority project. You can use the following SQL statement to evaluate the projects in the CORPDATA.PROJECT table, and write a 1 (a flag to indicate PRIORITY) in the PRIORITY column (a column you added to CORPDATA.PROJECT for this purpose) for each priority project.
UPDATE CORPDATA.PROJECT X SET PRIORITY = 1 WHERE 1983-09-01 > (SELECT MAX(EMENDATE) FROM CORPDATA.EMPPROJACT WHERE PROJNO = X.PROJNO)
As SQL examines each row in the CORPDATA.EMPPROJACT table, it determines the maximum activity end date (EMENDATE) for all activities of the project (from the CORPDATA.PROJECT table). If the end date of each activity associated with the project is before September 1983, the current row in the CORPDATA.PROJECT table qualifies and is updated. Update the master order table with any changes to the quantity ordered. If the quantity in the orders table is not set (the NULL value), keep the value that is in the master order table.
UPDATE MASTER_ORDERS X SET QTY=(SELECT COALESCE (Y.QTY, X.QTY) FROM ORDERS Y WHERE X.ORDER_NUM = Y.ORDER_NUM) WHERE X.ORDER_NUM IN (SELECT ORDER_NUM FROM ORDERS)
98
In this example, each row of the MASTER_ORDERS table is checked to see if it has a corresponding row in the ORDERS table. If it does have a matching row in the ORDERS table, the COALESCE function is used to return a value for the QTY column. If QTY in the ORDERS table has a non-null value, that value is used to update the QTY column in the MASTER_ORDERS table. If the QTY value in the ORDERS table is NULL, the MASTER_ORDERS QTY column is updated with its own value. Example: Correlated subquery in a DELETE statement: When you use a correlated subquery in a DELETE statement, the correlation name represents the row that you want to delete. SQL evaluates the correlated subquery once for each row in the table named in the DELETE statement to decide whether to delete the row. Suppose that a row in the CORPDATA.PROJECT table is deleted. Rows related to the deleted project in the CORPDATA.EMPPROJACT table must also be deleted. To do this, run the following statement:
DELETE FROM CORPDATA.EMPPROJACT X WHERE NOT EXISTS (SELECT * FROM CORPDATA.PROJECT WHERE PROJNO = X.PROJNO)
SQL determines, for each row in the CORPDATA.EMP_ACT table, whether a row with the same project number exists in the CORPDATA.PROJECT table. If not, the CORPDATA.EMP_ACT row is deleted.
SQL programming
99
Handling duplicate rows on page 49 When SQL evaluates a select-statement, several rows might qualify to be in the result table, depending on the number of rows that satisfy the search condition of the select-statement. Some of the rows in the result table might be duplicate. Defining complex search conditions on page 50 In addition to the basic comparison predicates, such as = and >, a search condition can contain any of these predicates: BETWEEN, IN, EXISTS, IS NULL, and LIKE. Using the UNION keyword to combine subselects on page 68 Using the UNION keyword, you can combine two or more subselects to form a fullselect. Sort sequence
In the following examples, the results are shown for each statement using: v *HEX sort sequence v Shared-weight sort sequence using the language identifier ENU v Unique-weight sort sequence using the language identifier ENU Note: ENU is chosen as a language identifier by specifying either SRTSEQ(*LANGIDUNQ), or SRTSEQ(*LANGIDSHR) and LANGID(ENU), on the CRTSQLxxx, STRSQL, or RUNSQLSTM commands, or by using the SET OPTION statement.
The following table shows the result using a *HEX sort sequence. The rows are sorted based on the EBCDIC value in the JOB column. In this case, all lowercase letters sort before the uppercase letters.
100
Table 28. Result of using the *HEX sort sequence ID 100 90 80 10 50 30 20 40 70 60 NAME Plotz Koonitz James Sanders Hanes Merenghi Pernal OBrien Rothman Quigley DEPT 42 42 20 20 15 38 20 38 15 38 JOB mgr sales Clerk Mgr Mgr MGR Sales Sales Sales SALES YEARS 6 6 0 7 10 5 8 6 7 0 SALARY 18352.80 18001.75 13504.60 18357.50 20659.80 17506.75 18171.25 18006.00 16502.83 16808.30 COMM 0 1386.70 128.20 0 0 0 612.45 846.55 1152.00 650.25
The following table shows how sorting is done for a unique-weight sort sequence. After the sort sequence is applied to the values in the JOB column, the rows are sorted. Notice that after the sort, lowercase letters are before the same uppercase letters, and the values mgr, Mgr, and MGR are adjacent to each other.
Table 29. Result of using the unique-weight sort sequence for the ENU language identifier ID 80 100 10 50 30 90 20 40 70 60 NAME James Plotz Sanders Hanes Merenghi Koonitz Pernal OBrien Rothman Quigley DEPT 20 42 20 15 38 42 20 38 15 38 JOB Clerk mgr Mgr Mgr MGR sales Sales Sales Sales SALES YEARS 0 6 7 10 5 6 8 6 7 0 SALARY 13504.60 18352.80 18357.50 20659.80 17506.75 18001.75 18171.25 18006.00 16502.83 16808.30 COMM 128.20 0 0 0 0 1386.70 612.45 846.55 1152.00 650.25
The following table shows how sorting is done for a shared-weight sort sequence. After the sort sequence is applied to the values in the JOB column, the rows are sorted. For the sort comparison, each lowercase letter is treated the same as the corresponding uppercase letter. In this table, notice that all the values MGR, mgr and Mgr are mixed together.
Table 30. Result of using the shared-weight sort sequence for the ENU language identifier ID 80 10 30 50 100 20 40 NAME James Sanders Merenghi Hanes Plotz Pernal OBrien DEPT 20 20 38 15 42 20 38 JOB Clerk Mgr MGR Mgr mgr Sales Sales YEARS 0 7 5 10 6 8 6 SALARY 13504.60 18357.50 17506.75 20659.80 18352.80 18171.25 18006.00 COMM 128.20 0 0 0 0 612.45 846.55
SQL programming
101
Table 30. Result of using the shared-weight sort sequence for the ENU language identifier (continued) ID 60 70 90 NAME Quigley Rothman Koonitz DEPT 38 15 42 JOB SALES Sales sales YEARS 0 7 6 SALARY 16808.30 16502.83 18001.75 COMM 650.25 1152.00 1386.70
The first table shows how row selection is done with a *HEX sort sequence. The rows that match the row selection criteria for the column JOB are selected exactly as specified in the select statement. Only the uppercase MGR is selected.
Table 31. Result of using the *HEX sort sequence ID 30 NAME Merenghi DEPT 38 JOB MGR YEARS 5 SALARY 17506.75 COMM 0
Table 2 shows how row selection is done with a unique-weight sort sequence. The lowercase and uppercase letters are treated as unique. The lowercase mgr is not treated the same as uppercase MGR. Therefore, the lowercase mgr is not selected.
Table 32. Result of using unique-weight sort sequence for the ENU language identifier ID 30 NAME Merenghi DEPT 38 JOB MGR YEARS 5 SALARY 17506.75 COMM 0
The following table shows how row selection is done with a shared-weight sort sequence. The rows that match the row selection criteria for the column JOB are selected by treating uppercase letters the same as lowercase letters. Notice that all the values mgr, Mgr and MGR are selected.
Table 33. Result of using the shared-weight sort sequence for the ENU language identifier ID 10 30 50 100 NAME Sanders Merenghi Hanes Plotz DEPT 20 38 15 42 JOB Mgr MGR Mgr mgr YEARS 7 5 10 6 SALARY 18357.50 17506.75 20659.80 18352.80 COMM 0 0 0 0
102
The following SQL statements and tables show how views and sort sequences work. View V1, used in the following examples, was created with a shared-weight sort sequence of SRTSEQ(*LANGIDSHR) and LANGID(ENU). The CREATE VIEW statement is as follows:
CREATE VIEW V1 AS SELECT * FROM STAFF WHERE JOB = MGR AND ID < 100 Table 34. SELECT * FROM V1 ID 10 30 50 NAME Sanders Merenghi Hanes DEPT 20 38 15 JOB Mgr MGR Mgr YEARS 7 5 10 SALARY 18357.50 17506.75 20659.80 COMM 0 0 0
Any queries run against view V1 are run against the result table shown above. The query shown below is run with a sort sequence of SRTSEQ(*LANGIDUNQ) and LANGID(ENU).
Table 35. SELECT * FROM V1 WHERE JOB = MGR using the unique-weight sort sequence for ENU language identifier ID 30 NAME Merenghi DEPT 38 JOB MGR YEARS 5 SALARY 17506.75 COMM 0
SQL programming
103
The ICU support (5722-SS1 Option 39) properly handles data that is not normalized, producing the same results as if the data were normalized. The ICU sort sequence table can sort all character, graphic, and Unicode (UTF-8, UTF-16 and UCS-2) data. For example, a UTF-8 character column named NAME contains the following names (the hex values of the column are given as well).
NAME Gmez Gomer Gumby HEX (NAME) 47C3B36D657A 476F6D6572 47756D6279
An ICU sort sequence table named en_us correctly orders the NAME values as follows.
NAME Gomer Gmez Gumby
When an ICU sort sequence table is specified, the performance of SQL statements that use the table can be much slower than the performance of SQL statements that use a non-ICU sort sequence table or use a *HEX sort sequence. The slower performance results from calling the ICU support to get the weighted value for each piece of data that needs to be sorted. An ICU sort sequence table can provide more sorting function but at the cost of slower running SQL statements. However, indexes created with an ICU sort sequence table can be created over columns to help reduce the need of calling the ICU support. In this case, the index key already contains the ICU weighted value, so there is no need to call the ICU support. Related concepts International Components for Unicode
Normalization
Normalization allows you to compare strings that contain combining characters. Data tagged with a UTF-8 or UTF-16 CCSID can contain combining characters. Combining characters allow a resulting character to be composed of more than one character. After the first character of the compound character, one of many different non-spacing characters such as umlauts and accents can follow in the data string. If the resulting character is one that is already defined in the character set, normalization of the string results in multiple combining characters being replaced by the value of the defined character. For example, if your string contained the letter a followed by an .., the string is normalized to contain the single character . Normalization makes it possible to accurately compare strings. If data is not normalized, two strings that look identical on the display may not compare equal since the stored representation can be different. When UTF-8 and UTF-16 string data is not normalized, it is possible that a column in a table can have
104
one row with the letter a followed by the umlaut character and another row with the combined character. These two values are not both compare equal in a comparison predicate: WHERE C1 = . For this reason, it is recommended that all string columns in a table are stored in normalized form. You can normalize the data yourself before inserting or updating it, or you can define a column in a table to be automatically normalized by the database. To have the database perform the normalization, specify NORMALIZED as part of the column definition. This option is only allowed for columns that are tagged with a CCSID of 1208 (UTF-8) or 1200 (UTF-16). The database assumes all columns in a table have been normalized. The NORMALIZED clause can also be specified for function and procedure parameters. If it is specified for an input parameter, the normalization will be done by the database for the parameter value before invoking the function or procedure. If it is specified for an output parameter, the clause is not enforced; it is assumed that the users routine code will return a normalized value. The NORMALIZE_DATA option in the QAQQINI file is used to indicate whether the system is to perform normalization when working with UTF-8 and UTF-16 data. This option controls whether the system will normalize literals, host variables, parameter markers, and expressions that combine strings before using them in SQL. The option is initialized to not perform normalization. This is the correct value for you if the data in your tables and any literal values in your applications is always normalized already through some other mechanism or never contains characters which will need to be normalized. If this is the case, you will want to avoid the overhead of system normalization in your query. If your data is not already normalized, you will want to switch the value of this option to have the system perform normalization for you. Related tasks Controlling queries dynamically with the query options file QAQQINI
Data protection
DB2 UDB for iSeries provides various methods for protecting SQL data from unauthorized users and for ensuring data integrity.
105
the SQL statement remotely is checked using the user profiles of the application server job and the owner of the SQL package. The higher authority is the authority that is used. *OWNER is the default for SQL (*SQL) naming. For dynamic SQL statements: v If the USRPRF value is *USER, the authority to run the SQL statement locally is checked using the user profile of the person running the program. The authority to run the SQL statement remotely is checked using the user profile of the application server job. v If the USRPRF value is *OWNER and DYNUSRPRF is *USER, the authority to run the SQL statement locally is checked using the user profile of the person running the program. The authority to run the SQL statement remotely is checked using the user profile of the application server job. v If the USRPRF value is *OWNER and DYNUSRPRF is *OWNER, the authority to run the SQL statement locally is checked using the user profiles of the user running the program and the owner of the program. The authority to run the SQL statement remotely is checked using the user profiles of the application server job and the owner of the SQL package. The highest authority is the authority that is used. Because of security concerns, you should use the *OWNER parameter value for DYNUSRPRF carefully. This option gives the access authority of the owner program or package to those who run the program. For interactive SQL statements, authority is checked against the authority of the person processing the statement. Adopted authority is not used for interactive SQL statements. Related reference Security Reference PDF GRANT (Table or View Privileges) REVOKE (Table or View Privileges)
Authorization ID
An authorization ID is a user profile object that identifies a unique user. You can use the Create User Profile (CRTUSRPRF) command to create an authorization ID.
Views
Views can prevent unauthorized users from having access to sensitive data. The application program can access the data it needs in a table, without having access to sensitive or restricted data in the table. A view can restrict access to particular columns by not specifying those columns in the SELECT list (for example, employee salaries). A view can also restrict access to particular rows in a table by specifying a WHERE clause (for example, allowing access only to the rows associated with a particular department number).
Auditing
DB2 UDB for iSeries is designed to comply with the U.S. government C2 security level. A key feature of the C2 level is the ability to perform auditing on the system. DB2 UDB for iSeries uses the audit facilities managed by the system security function. Auditing can be performed on an object level, user, or system level. The system value QAUDCTL controls whether auditing is performed at the object or user level. The Change User Audit (CHGUSRAUD) command and Change Object Audit (CHGOBJAUD) command specify which users and objects are audited. The system value QAUDLVL controls what types of actions are audited (for example, authorization failures, creates, deletes, grants, revokes, and so on.) DB2 UDB for iSeries can also audit row changes through the DB2 UDB for iSeries journal support. In some cases, entries in the auditing journal will not be in the same order as they occured. For example, a job that is running under commitment control deletes a table, creates a new table with the same name
106
as the one that was deleted, then does a commit. This will be recorded in the auditing journal as a create followed by a delete. This is because objects that are created are journaled immediately. An object that is deleted under commitment control is hidden and not actually deleted until a commit is done. Once the commit is done, the action is journaled. Related reference Security Reference PDF
Data integrity
Data integrity protects data from being destroyed or changed by unauthorized persons, system operation or hardware failures (such as physical damage to a disk), programming errors, interruptions before a job is completed (such as a power failure), or interference from running applications at the same time (such as serialization problems).
Concurrency
Concurrency is the ability for multiple users to access and change data in the same table or view at the same time without risk of losing data integrity. This ability is automatically supplied by the DB2 Universal Database for iSeries database manager. Locks are implicitly acquired on tables and rows to protect concurrent users from changing the same data at precisely the same time. Typically, DB2 UDB for iSeries will acquire locks on rows to ensure integrity. However, some situations require DB2 UDB for iSeries to acquire a more exclusive table level lock instead of row locks. For example, an update (exclusive) lock on a row currently held by one cursor can be acquired by another cursor in the same program (or in a DELETE or UPDATE statement not associated with the cursor). This will prevent a positioned UPDATE or positioned DELETE statement that references the first cursor until another FETCH is performed. A read (shared no-update) lock on a row currently held by one cursor will not prevent another cursor in the same program (or DELETE or UPDATE statement) from acquiring a lock on the same row. Default and user-specifiable lock-wait time-out values are supported. DB2 UDB for iSeries creates tables, views, and indexes with the default record wait time (60 seconds) and the default file wait time (*IMMED). This lock wait time is used for DML statements. You can change these values by using the CL commands Change Physical File (CHGPF), Change Logical File (CHGLF), and Override Database File (OVRDBF). The lock wait time used for all DDL statements and the LOCK TABLE statement, is the job default wait time (DFTWAIT). You can change this value by using the CL commands Change Job (CHGJOB) or Change Class (CHGCLS). In the event that a large record wait time is specified, deadlock detection is provided. For example, assume one job has an exclusive lock on row 1 and another job has an exclusive lock on row 2. If the first job attempts to lock row 2, it will wait because the second job is holding the lock. If the second job then attempts to lock row 1, DB2 UDB for iSeries will detect that the two jobs are in a deadlock and an error will be returned to the second job. You can explicitly prevent other users from using a table at the same time by using the SQL LOCK TABLE statement. Using COMMIT(*RR) will also prevent other users from using a table during a unit of work. In order to improve performance, DB2 UDB for iSeries will frequently leave the open data path (ODP) open. This performance feature also leaves a lock on tables referenced by the ODP, but does not leave any locks on rows. A lock left on a table may prevent another job from performing an operation on that table.
SQL programming
107
In most cases, however, DB2 UDB for iSeries will detect that other jobs are holding locks and events will be signalled to those jobs. The event causes DB2 UDB for iSeries to close any ODPs (and release the table locks) that are associated with that table and are currently only open for performance reasons. Note that the lock wait time out must be large enough for the events to be signalled and the other jobs to close the ODPs or an error will be returned. Unless the LOCK TABLE statement is used to acquire table locks, or either COMMIT(*ALL) or COMMIT(*RR) is used, data which has been read by one job can be immediately changed by another job. Typically, the data that is read at the time the SQL statement is executed and therefore it is very current (for example, during FETCH). In the following cases, however, data is read before the execution of the SQL statement and therefore the data may not be current (for example, during OPEN). v ALWCPYDTA(*OPTIMIZE) was specified and the optimizer determined that making a copy of the data performs better than not making a copy. v Some queries require the database manager to create a temporary result table. The data in the temporary result table will not reflect changes made after the cursor was opened. A temporary result table is required when: The total length in bytes of storage for the columns specified in an ORDER BY clause exceeds 2000 bytes. ORDER BY and GROUP BY clauses specify different columns or columns in a different order. UNION or DISTINCT clauses are specified. ORDER BY or GROUP BY clauses specify columns which are not all from the same table. Joining a logical file defined by the JOINDFT data definition specifications (DDS) keyword with another file. Joining or specifying GROUP BY on a logical file which is based on multiple database file members. The query contains a join in which at least one of the files is a view which contains a GROUP BY clause. The query contains a GROUP BY clause which references a view that contains a GROUP BY clause. v A basic subquery is evaluated when the query is opened. Related reference LOCK TABLE
Journaling
The DB2 UDB for iSeries journal support provides an audit trail and forward and backward recovery. Forward recovery can be used to take an older version of a table and apply the changes logged on the journal to the table. Backward recovery can be used to remove changes logged on the journal from the table. When an SQL schema is created, a journal and journal receiver are created in the schema. When SQL creates the journal and journal receiver, they are only created on a user auxiliary storage pool (ASP) if the ASP clause is specified on the CREATE SCHEMA statement. However, because placing journal receivers on their own ASPs can improve performance, the person managing the journal might want to create all future journal receivers on a separate ASP. When a table is created into the schema, it is automatically journaled to the journal DB2 UDB for iSeries created in the schema (QSQJRN). A table created in a non-schema will also have journaling started if a journal named QSQJRN exists in that library. After this point, it is your responsibility to use the journal functions to manage the journal, the journal receivers, and the journaling of tables to the journal. For example, if a table is moved into a schema, no automatic change to the journaling status occurs. If a table is restored, the normal journal rules apply. That is, if the table was journaled at the time of the save, it is journaled to the same journal at restore time. If the table was not journaled at the time of the save, it is not journaled at restore time.
108
The journal created in the SQL collection is normally the journal used for logging all changes to SQL tables. You can, however, use the system journal functions to journal SQL tables to a different journal. A user can stop journaling on any table using the journal functions, but doing so prevents an application from running under commitment control. If journaling is stopped on a parent table of a referential constraint with a delete rule of NO ACTION, CASCADE, SET NULL, or SET DEFAULT, all update and delete operations will be prevented. Otherwise, an application is still able to function if you have specified COMMIT(*NONE); however, this does not provide the same level of integrity that journaling and commitment control provide. Related concepts Journal management Related reference Updating tables with referential constraints on page 85 If you are updating a parent table, you cannot modify a primary key for which dependent rows exist.
Commitment control
The DB2 UDB for iSeries commitment control support provides a means for processing a group of database changes, such as update, insert, data definition language (DDL), or delete operations, as a single unit of work (also referred to as a transaction). A commit operation guarantees that the group of operations is completed. A rollback operation guarantees that the group of operations is backed out. A savepoint can be used to break a transaction into smaller units that can be rolled back. A commit operation can be issued through several different interfaces. For example, v An SQL COMMIT statement v A CL COMMIT command v A language commit statement (such as an RPG COMMIT statement) A rollback operation can be issued through several different interfaces. For example, v An SQL ROLLBACK statement v A CL ROLLBACK command v A language rollback statement (such as an RPG ROLBK statement) The only SQL statements that cannot be committed or rolled back are: v DROP SCHEMA v GRANT or REVOKE if an authority holder exists for the specified object If commitment control was not already started when either an SQL statement is executed with an isolation level other than COMMIT(*NONE) or a RELEASE statement is executed, then DB2 UDB for iSeries sets up the commitment control environment by implicitly calling the CL command Start Commitment Control (STRCMTCTL). DB2 UDB for iSeries specifies NFYOBJ(*NONE) and CMTSCOPE(*ACTGRP) parameters along with LCKLVL on the STRCMTCTL command. The LCKLVL specified is the lock level on the COMMIT parameter on the CRTSQLxxx, STRSQL, or RUNSQLSTM commands. In REXX, the LCKLVL specified is the lock level on the SET OPTION statement. You may use the STRCMTCTL command to specify a different CMTSCOPE, NFYOBJ, or LCKLVL. If you specify CMTSCOPE(*JOB) to start the job level commitment definition, DB2 UDB for iSeries uses the job level commitment definition for programs in that activation group. Notes: 1. When using commitment control, the tables referred to in the application program by Data Manipulation Language statements must be journaled.
SQL programming
109
2. Note that the LCKLVL specified is only the default lock level. After commitment control is started, the SET TRANSACTION SQL statement and the lock level specified on the COMMIT parameter on the CRTSQLxxx, STRSQL, or RUNSQLSTM commands will override the default lock level. For cursors that use column functions, GROUP BY, or HAVING, and are running under commitment control, a ROLLBACK HOLD has no effect on the cursors position. In addition, the following occurs under commitment control: v If COMMIT(*CHG) and (ALWBLK(*NO) or (ALWBLK(*READ)) is specified for one of these cursors, a message (CPI430B) is sent that says COMMIT(*CHG) requested but not allowed. v If COMMIT(*ALL), COMMIT(*RR), or COMMIT(*CS) with the KEEP LOCKS clause is specified for one of the cursors, DB2 UDB for iSeries will lock all referenced tables in shared mode (*SHRNUP). The lock prevents concurrent application processes from executing any but read-only operations on the named table. A message (either SQL7902 or CPI430A) is sent that says COMMIT(*ALL), COMMIT(*RR), or COMMIT(*CS) with the KEEP LOCKS clause is specified for one of the cursors requested but not allowed. Message SQL0595 may also be sent. For cursors where either COMMIT(*ALL), COMMIT(*RR), or COMMIT(*CS) with the KEEP LOCKS clause is specified and either catalog files are used or a temporary result table is required, DB2 UDB for iSeries will lock all referenced tables in shared mode (*SHRNUP). This will prevent concurrent processes from executing anything but read-only operations on the table(s). A message (either SQL7902 or CPI430A) is sent that says COMMIT(*ALL) is requested but not allowed. Message SQL0595 may also be sent. If ALWBLK(*ALLREAD) and COMMIT(*CHG) were specified, when the program was precompiled, all read-only cursors will allow blocking of rows and a ROLLBACK HOLD will not roll the cursor position back. If COMMIT(*RR) is requested, the tables will be locked until the query is closed. If the cursor is read-only, the table will be locked (*SHRNUP). If the cursor is in update mode, the table will be locked (*EXCLRD). Since other users will be locked out of the table, running with repeatable read will prevent concurrent access of the table. If an isolation level other then COMMIT(*NONE) was specified and the application issues a ROLLBACK or the activation group ends abnormally (and the commitment definition is not *JOB), all updates, inserts, deletes, and DDL operations made within the unit of work are backed out. If the application issues a COMMIT or the activation group ends normally, all updates, inserts, deletes, and DDL operations made within the unit of work are committed. DB2 UDB for iSeries uses locks on rows to keep other jobs from accessing changed data before a unit of work completes. If COMMIT(*ALL) is specified, read locks on rows fetched are also used to prevent other jobs from changing data that was read before a unit of work completes. This will not prevent other jobs from reading the unchanged rows. This ensures that, if the same unit of work rereads a row, it gets the same result. Read locks do not prevent other jobs from fetching the same rows. Commitment control handles up to 500 million distinct row changes in a unit of work. If COMMIT(*ALL) or COMMIT(*RR) is specified, all rows read are also included in the limit. (If a row is changed or read more than once in a unit of work, it is only counted once toward the limit.) Holding a large number of locks adversely affects system performance and does not allow concurrent users to access rows locked in the unit of work until the end of the unit of work. It is in your best interest to keep the number of rows processed in a unit of work small. Commitment control will allow up to 512 files for each journal to be open under commitment control or closed with pending changes in a unit of work.
110
COMMIT HOLD and ROLLBACK HOLD allow you to keep the cursor open and start another unit of work without issuing an OPEN statement again. The HOLD value is not available when you are connected to a remote database that is not on a System i platform. However, the WITH HOLD option on DECLARE CURSOR can be used to keep the cursor open after a commit. This type of cursor is supported when you are connected to a remote database that is not on a System i platform. Such a cursor is closed on a rollback.
Table 36. Row lock duration
SQL statement SELECT INTO SET variable VALUES INTO COMMIT parameter (see note 5) *NONE *CHG *CS (See note 6) *ALL (See note 2 and 7) Duration of row locks No locks No locks Row locked when read and released From read until ROLLBACK or COMMIT No locks No locks From read until the next FETCH From read until ROLLBACK or COMMIT When row is not updated or deleted from read until next FETCH When row is updated from read until next FETCH When row is deleted from read until next DELETE When row is not updated or deleted from read until next FETCH When row is updated or deleted from read until COMMIT or ROLLBACK When row is not updated or deleted from read until next FETCH When row is updated or deleted from read until COMMIT or ROLLBACK From read until ROLLBACK or COMMIT No locks From insert until ROLLBACK or COMMIT From insert until ROLLBACK or COMMIT From insert until ROLLBACK or COMMIT No locks No locks Each row locked while being read From read until ROLLBACK or COMMIT Each row locked while being updated From read until ROLLBACK or COMMIT From read until ROLLBACK or COMMIT From read until ROLLBACK or COMMIT Each row locked while being deleted From read until ROLLBACK or COMMIT From read until ROLLBACK or COMMIT From read until ROLLBACK or COMMIT From From From From read read read read until until until until next FETCH ROLLBACK or COMMIT ROLLBACK or COMMIT ROLLBACK or COMMIT Lock type
READ READ
FETCH (read-only cursor) *NONE *CHG *CS (See note 6) *ALL (See note 2 and 7) FETCH (update or delete capable cursor) (See note 1) *NONE
UPDATE
*CHG
UPDATE
*CS
UPDATE
*ALL INSERT (target table) *NONE *CHG *CS *ALL *NONE *CHG *CS *ALL *NONE *CHG *CS *ALL *NONE *CHG *CS *ALL *NONE *CHG *CS *ALL *NONE *CHG *CS *ALL *NONE *CHG *CS *ALL (see note 2)
READ READ UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE UPDATE READ READ READ READ
UPDATE (non-cursor)
DELETE (non-cursor)
Lock released when row deleted From read until ROLLBACK or COMMIT From read until ROLLBACK or COMMIT From read until ROLLBACK or COMMIT From From From From read read read read until until until until next FETCH next FETCH next FETCH ROLLBACK or COMMIT
SQL programming
111
READ READ
Notes: 1. A cursor is open with UPDATE or DELETE capabilities if the result table is not read-only and if one of the following is true: v The cursor is defined with a FOR UPDATE clause. v The cursor is defined without a FOR UPDATE, FOR READ ONLY, or ORDER BY clause and the program contains at least one of the following: Cursor UPDATE referring to the same cursor-name Cursor DELETE referring to the same cursor-name An EXECUTE or EXECUTE IMMEDIATE statement and ALWBLK(*READ) or ALWBLK(*NONE) was specified on the CRTSQLxxx command. 2. A table or view can be locked exclusively in order to satisfy COMMIT(*ALL). If a subselect is processed that includes a UNION, or if the processing of the query requires the use of a temporary result, an exclusive lock is acquired to protect you from seeing uncommitted changes. 3. An UPDATE lock on rows of the target table and a READ lock on the rows of the subselect table. 4. A table or view can be locked exclusively in order to satisfy repeatable read. Row locking is still done under repeatable read. The locks acquired and their duration are identical to *ALL. 5. Repeatable read (*RR) row locks will be the same as the locks indicated for *ALL. 6. If the KEEP LOCKS clause is specified with *CS, any read locks are held until the cursor is closed or until a COMMIT or ROLLBACK is done. If no cursors are associated with the isolation clause, then locks are held until the completion of the SQL statement. 7. If the USE AND KEEP EXCLUSIVE LOCKS clause is specified with the *RS or *RR isolation level, an UPDATE lock on the row will be obtained instead of a READ lock.
Related concepts Commitment control Related reference DECLARE CURSOR Isolation level
Savepoints
A savepoint is a named entity that represents the state of data and schemas at a particular point within a unit of work. You can create savepoints within a transaction. If the transaction rolls back, changes are undone to the specified savepoint, rather than to the beginning of the transaction. You can set a savepoint using the SAVEPOINT SQL statement. For example, create a savepoint called STOP_HERE:
SAVEPOINT STOP_HERE ON ROLLBACK RETAIN CURSORS
Program logic in the application dictates whether the savepoint name is reused as the application progresses, or if the savepoint name denotes a unique milestone in the application that should not be reused. If the savepoint represents a unique milestone that should not be moved with another SAVEPOINT statement, specify the UNIQUE keyword. This prevents the accidental reuse of the name that can occur by invoking a stored procedure that uses the identical savepoint name in a SAVEPOINT statement. However, if the SAVEPOINT statement is used in a loop, then the UNIQUE keyword should not be used. The following SQL statement sets a unique savepoint named START_OVER.
112
To rollback to a savepoint, use the ROLLBACK statement with the TO SAVEPOINT clause. The following example illustrates using the SAVEPOINT and ROLLBACK TO SAVEPOINT statements: This application logic books airline reservations on a preferred date, then books hotel reservations. If the hotel is unavailable, it rolls back the airline reservations and then repeats the process for another date. Up to 3 dates are tried.
got_reservations =0; EXEC SQL SAVEPOINT START_OVER UNIQUE ON ROLLBACK RETAIN CURSORS; if (SQLCODE != 0) return; for (i=0; i<3 & got_reservations == 0; ++i) { Book_Air(dates(i), ok); if (ok) { Book_Hotel(dates(i), ok); if (ok) got_reservations = 1; else { EXEC SQL ROLLBACK TO SAVEPOINT START_OVER; if (SQLCODE != 0) return; } } } EXEC SQL RELEASE SAVEPOINT START_OVER;
Savepoints are released using the RELEASE SAVEPOINT statement. If a RELEASE SAVEPOINT statement is not used to explicitly release a savepoint, it is released at the end of the current savepoint level or at the end of the transaction. The following statement releases savepoint START_OVER.
RELEASE SAVEPOINT START_OVER
Savepoints are released when the transaction is committed or rolled back. Once the savepoint name is released, a rollback to the savepoint name is no longer possible. The COMMIT or ROLLBACK statement releases all savepoint names established within a transactions. Since all savepoint names are released within the transaction, all savepoint names can be reused following a commit or rollback. Savepoints are scoped to a single connection only. Once a savepoint is established, it is not distributed to all remote databases that the application connects to. The savepoint only applies to the current database that the application is connected to when the savepoint is established. A single statement can implicitly or explicitly invoke a user-defined function, trigger, or stored procedure. This is known as nesting. In some cases when a new nesting level is initiated, a new savepoint level is also initiated. A new savepoint level isolates the invoking application from any savepoint activity by the lower level routine or trigger. Savepoints can only be referenced within the same savepoint level (or scope) in which they are defined. A ROLLBACK TO SAVEPOINT statement cannot be used to rollback to a savepoint established outside the current savepoint level. Likewise, a RELEASE SAVEPOINT statement cannot be used to release a savepoint established outside the current savepoint level. The following table summarizes when savepoint levels are initiated and terminated:
A new savepoint level is initiated when: A new unit of work is started That savepoint level ends when: COMMIT or ROLLBACK is issued
SQL programming
113
A new savepoint level is initiated when: A trigger is invoked A user-defined function is invoked
That savepoint level ends when: The trigger completes The user-defined function returns to the invoker
A stored procedure is invoked, and that stored procedure The stored procedure returns to the caller was created with the NEW SAVEPOINT LEVEL clause There is a BEGIN for an ATOMIC compound SQL statement There is an END for an ATOMIC compound statement
A savepoint that is established in a savepoint level is implicitly released when that savepoint level is terminated.
Atomic operations
When running under COMMIT(*CHG), COMMIT(*CS), or COMMIT(*ALL), all operations are guaranteed to be atomic. That is, they will complete or they will appear not to have started. This is true regardless of when or how the function was ended or interrupted (such as power failure, abnormal job end, or job cancel). If COMMIT (*NONE) is specified, however, some underlying database data definition functions are not atomic. The following SQL data definition statements are guaranteed to be atomic: v ALTER TABLE (See note 1) v COMMENT ON (See note 2) v LABEL ON (See note 2) v GRANT (See note 3) v REVOKE (See note 3) v DROP TABLE (See note 4) v DROP VIEW (See note 4) v DROP INDEX v DROP PACKAGE v REFRESH TABLE Notes: 1. If constraints need to be added or removed, as well as column definitions changed, the operations are processed one at a time, so the entire SQL statement is not atomic. The order of operation is: v Remove constraints v Drop columns for which the RESTRICT option was specified v All other column definition changes (DROP COLUMN CASCADE, ALTER COLUMN, ADD COLUMN) v Add constraints 2. If multiple columns are specified for a COMMENT ON or LABEL ON statement, the columns are processed one at a time, so the entire SQL statement is not atomic, but the COMMENT ON or LABEL ON to each individual column or object will be atomic. 3. If multiple tables, SQL packages, or users are specified for a GRANT or REVOKE statement, the tables are processed one at a time, so the entire SQL statement is not atomic, but the GRANT or REVOKE to each individual table will be atomic. 4. If dependent views need to be dropped during DROP TABLE or DROP VIEW, each dependent view is processed one at a time, so the entire SQL statement is not atomic.
114
The following data definition statements are not atomic because they involve more than one database operation: v ALTER PROCEDURE v ALTER SEQUENCE v v v v v v v v v v v v v v v v v v CREATE ALIAS CREATE DISTINCT TYPE CREATE FUNCTION CREATE INDEX CREATE PROCEDURE CREATE SCHEMA CREATE SEQUENCE CREATE TABLE CREATE TRIGGER CREATE VIEW DROP ALIAS DROP DISTINCT TYPE DROP FUNCTION DROP PROCEDURE DROP SCHEMA DROP SEQUENCE DROP TRIGGER RENAME (See note 1)
Note: RENAME is atomic only if the name or the system name is changed. When both are changed, the RENAME is not atomic. For example, a CREATE TABLE can be interrupted after the DB2 UDB for iSeries physical file has been created, but before the member has been added. Therefore, in the case of create statements, if an operation ends abnormally, you may need to drop the object and then create it again. In the case of a DROP SCHEMA statement, you may need to drop the schema again or use the CL command Delete Library (DLTLIB) to remove the remaining parts of the schema.
Constraints
DB2 UDB for iSeries supports unique, referential, and check constraints. A unique constraint is a rule that guarantees that the values of a key are unique. A referential constraint is a rule that all non-null values of foreign keys in a dependent table have a corresponding parent key in a parent table. A check constraint is a rule that limits the values allowed in a column or group of columns. DB2 UDB for iSeries will enforce the validity of the constraint during any DML (data manipulation language) statement. Certain operations (such as restore of the dependent table), however, cause the validity of the constraint to be unknown. In this case, DML statements may be prevented until DB2 UDB for iSeries has verified the validity of the constraint. v Unique constraints are implemented with indexes. If an index that implements a unique constraint is invalid, the Edit Rebuild of Access Paths (EDTRBDAP) command can be used to display any indexes that currently require rebuild. v If DB2 UDB for iSeries does not currently know whether a referential constraint or check constraint is valid, the constraint is considered to be in a check pending state. The Edit Check Pending Constraints (EDTCPCST) command can be used to display any indexes that currently require rebuild.
SQL programming
115
Related concepts Constraints on page 9 A constraint is a rule enforced by the database manager to limit the values that can be inserted, deleted, or updated in a table. Adding and using check constraints: A check constraint ensures the validity of data during insert and update operations by limiting the allowed values in a column or group of columns. Use the SQL CREATE TABLE and ALTER TABLE statements to add or drop check constraints. In this example, the following statement creates a table with three columns and a check constraint over COL2 that limits the values allowed in that column to positive integers:
CREATE TABLE T1 (COL1 INT, COL2 INT CHECK (COL2>0), COL3 INT)
fails because the value to be inserted into COL2 does not meet the check constraint; that is, -1 is not greater than 0. The following statement is successful:
INSERT INTO T1 VALUES (1, 1, 1)
This ALTER TABLE statement attempts to add a second check constraint that limits the value allowed in COL1 to 1 and also effectively rules that values in COL2 be greater than 1. This constraint is not allowed because the second part of the constraint is not met by the existing data (the value of 1 in COL2 is not less than the value of 1 in COL1). Related reference ALTER TABLE CREATE TABLE
116
When an SQL table is restored, the definitions for the SQL triggers that are defined for the table are also restored. The SQL trigger definitions are automatically added to the SYSTRIGGERS, SYSTRIGDEP, SYSTRIGCOL, and SYSTRIGUPD catalogs. The program object that is created from the SQL CREATE TRIGGER statement must also be saved and restored when the SQL table is saved and restored. The saving and restoring of the program object is not automated by the database manager. The precautions for self-referencing triggers should be reviewed when restoring SQL tables to a new library. When an *SQLUDT object is restored for a user-defined type, the user-defined type is automatically added to the SYSTYPES catalog. The appropriate functions needed to cast between the user-defined type and the source type are also created, as long as the type and functions do not already exist. When a *DTAARA for a sequence is restored, the sequence is automatically added to the SYSSEQUENCES catalog. If the catalog is not successfully updated, the *DTAARA will be modified so it cannot be used as a sequence and an SQL9020 informational message will be output in the job log. Either a distributed SQL program or its associated SQL package can be saved and restored to any number of systems. This allows any number of copies of the SQL programs on different systems to access the same SQL package on the same application server. This also allows a single distributed SQL program to connect to any number of application servers that have the SQL package restored (CRTSQLPKG can also be used). SQL packages cannot be restored to a different library. Note: Restoring a schema to an existing library or to a schema that has a different name does not restore the journal, journal receivers, or IDDU dictionary (if one exists). If the schema is restored to a schema with a different name, the catalog views in that schema will only reflect objects in the old schema. The catalog views in QSYS2, however, will appropriately reflect all objects.
Damage tolerance
DB2 UDB for iSeries provides several mechanisms to reduce or eliminate damage caused by disk errors. For example, mirroring, checksums, and RAID disks can all reduce the possibility of disk problems. The DB2 UDB for iSeries functions also have a certain amount of tolerance to damage caused by disk errors or system errors. A DROP operation always succeeds, regardless of the damage. This ensures that should damage occur, at least the table, view, SQL package, index, procedure, function, or distinct type can be deleted and restored or created again. In the event that a disk error has damaged a small portion of the rows in a table, the DB2 UDB for iSeries database manager allows you to read rows still accessible.
Index recovery
DB2 UDB for iSeries provides several functions to deal with index recovery. v System managed index protection The EDTRCYAP CL command allows a user to instruct DB2 UDB for iSeries to guarantee that in the event of a system or power failure, the amount of time required to recover all indexes on the system is kept below a specified time. The system automatically journals enough information in a system journal to limit the recovery time to the specified amount. v Journaling of indexes DB2 UDB for iSeries supplies an index journaling function that makes it unnecessary to rebuild an entire index due to a power or system failure. If the index is journaled, the system database support automatically makes sure the index is in synchronization with the data in the tables without having to rebuild it from scratch. SQL indexes are not journaled automatically. You can, however, use the CL command Start Journal Access Path (STRJRNAP) to journal any index created by DB2 UDB for iSeries. v Index rebuild
SQL programming
117
All indexes on the system have a maintenance option that specifies when an index is maintained. SQL indexes are created with an attribute of *IMMED maintenance. In the event of a power failure or an abnormal system failure, if indexes are not protected by one of the previously described techniques, those indexes in the process of change might need to be rebuilt by the database manager to make sure that they agree with the actual data. All indexes on the system have a recovery option that specifies when an index should be rebuilt if necessary. All SQL indexes with an attribute of UNIQUE are created with a recovery attribute of *IPL (this means that these indexes are rebuilt before the i5/OS operating system is started). All other SQL indexes are created with the *AFTIPL recovery option (this means that after the operating system is started, indexes are asynchronously rebuilt). During an IPL, the operator can see a display showing the indexes that need to be rebuilt and their recovery options. The operator can override the recovery options. v Save and restore of indexes The save/restore function allows you to save indexes when a table is saved by using ACCPTH(*YES) on the Save Object (SAVOBJ) or Save Library (SAVLIB) CL commands. In the event of a restore when the indexes have also been saved, there is no need to rebuild the indexes. Any indexes not previously saved and restored are automatically and asynchronously rebuilt by the database manager.
Catalog integrity
To ensure that the information in the catalog is always accurate, DB2 UDB for iSeries prevents users from explicitly changing the information in the catalog and by implicitly maintaining the information when an SQL object described in the catalog is changed. The integrity of the catalog is maintained whether objects in the schema are changed by SQL statements, i5/OS CL commands, System/38 Environment CL commands, System/36 Environment functions, or any other product or utility on a System i platform. For example, you can delete a table by running an SQL DROP statement, issuing an i5/OS Delete File (DLTF) CL command, issuing a System/38 Delete File (DLTF) CL command, or entering option 4 on a WRKF or WRKOBJ display. Regardless of the interface used to delete the table, the database manager removes the description of the table from the catalog when the table is deleted. The following table lists various functions and their associated effects on the catalog.
Table 37. Effects of various functions on catalogs Function Add constraint to table Remove of constraint from table Create object into schema Delete of object from schema Restore of object into schema Change of object long comment Change of object label (text) Change of object owner Move of object from a schema Move of object into schema Rename of object Effect on the catalog Information added to catalog Related information removed from catalog Information added to catalog Related information removed from catalog Information added to catalog Comment updated in catalog Label updated in catalog Owner updated in catalog Related information removed from catalog Information added to catalog Name of object updated in catalog
118
Routines
Routines are pieces of code or programs that you can call to perform operations.
Stored procedures
A procedure (often called a stored procedure) is a program that can be called to perform operations. A procedure can include both host language statements and SQL statements. Procedures in SQL provide the same benefits as procedures in a host language. DB2 UDB stored procedure support provides a way for an SQL application to define and then call a procedure through SQL statements. Stored procedures can be used in both distributed and nondistributed DB2 UDB applications. One of the advantages of using stored procedures is that for distributed applications, the execution of one CALL statement on the application requester, or client, can perform any amount of work on the application server. You may define a procedure as either an SQL procedure or an external procedure. An external procedure can be any supported high level language program (except System/36 programs and procedures) or a REXX procedure. The procedure does not need to contain SQL statements, but it may contain SQL statements. An SQL procedure is defined entirely in SQL, and can contain SQL statements that include SQL control statements. Coding stored procedures requires that the user understand the following: v Stored procedure definition through the CREATE PROCEDURE statement v Stored procedure invocation through the CALL statement v Parameter passing conventions v Methods for returning a completion status to the program invoking the procedure. You may define stored procedures by using the CREATE PROCEDURE statement. The CREATE PROCEDURE statement adds procedure and parameter definitions to the catalog tables SYSROUTINES and SYSPARMS. These definitions are then accessible by any SQL CALL statement on the system. To create an external procedure or an SQL procedure, you can use the SQL CREATE PROCEDURE statement. The following sections describe the SQL statements used to define and call the stored procedure, information about passing parameters to the stored procedure, and examples of stored procedure usage.
SQL programming
119
For more information about stored procedures, see Stored Procedures, Triggers, and User-Defined Functions on DB2 Universal Database for iSeries PDF Related concepts Stored procedures on page 10 A stored procedure is a program that can be called with the SQL CALL statement. Java SQL routines Related reference DRDA stored procedure considerations on page 281 The i5/OS Distributed Relational Database Architecture (DRDA) server supports the return of one or more result sets in a stored procedure. CREATE PROCEDURE
This CREATE PROCEDURE statement: v Names the procedure P1 v Defines one parameter which is used both as an input parameter and an output parameter. The parameter is a character field of length ten. Parameters can be defined to be type IN, OUT, or INOUT. The parameter type determines when the values for the parameters get passed to and from the procedure. v Defines the name of the program which corresponds to the procedure, which is PROC1 in MYLIB. MYLIB.PROC1 is the program which is called when the procedure is called on a CALL statement. v Indicates that the procedure P1 (program MYLIB.PROC1) is written in C. The language is important since it impacts the types of parameters that can be passed. It also affects how the parameters are passed to the procedure (for example, for ILE C procedures, a NUL-terminator is passed on character, graphic, date, time, and timestamp parameters). v Defines the CALL type to be GENERAL WITH NULLS. This indicates that the parameter for the procedure can possibly contain the NULL value, and therefore will like an additional argument passed to the procedure on the CALL statement. The additional argument is an array of N short integers, where N is the number of parameters that are declared in the CREATE PROCEDURE statement. In this example, the array contains only one element since there is only parameter. It is important to note that it is not necessary to define a procedure in order to call it. However, if no procedure definition is found, either from a prior CREATE PROCEDURE or from a DECLARE PROCEDURE in this program, certain restrictions and assumptions are made when the procedure is called on the CALL statement. For example, the NULL indicator argument cannot be passed. Related reference Using the embedded CALL statement where no procedure definition exists on page 126 A static CALL statement without a corresponding CREATE PROCEDURE statement is processed with these rules.
120
This CREATE PROCEDURE statement: v Names the procedure UPDATE_SALARY_1. v Defines parameter EMPLOYEE_NUMBER which is an input parameter and is a character data type of length 6 and parameter RATE which is an input parameter and is a decimal data type. v Indicates the procedure is an SQL procedure that modifies SQL data. v Defines the procedure body as a single UPDATE statement. When the procedure is called, the UPDATE statement is executed using the values passed for EMPLOYEE_NUMBER and RATE. Instead of a single UPDATE statement, logic can be added to the SQL procedure using SQL control statements. SQL control statements consist of: v An assignment statement v v v v v v v v v v v v v v v A CALL statement A CASE statement A compound statement A FOR statement A GET DIAGNOSTICS statement A GOTO statement An IF statement an ITERATE statement a LEAVE statement a LOOP statement a REPEAT statement a RESIGNAL statement a RETURN statement a SIGNAL statement a WHILE statement
The following example takes as input the employee number and a rating that was received on the last evaluation. The procedure uses a CASE statement to determine the appropriate increase and bonus for the update.
CREATE PROCEDURE UPDATE_SALARY_2 (IN EMPLOYEE_NUMBER CHAR(6), IN RATING INT) LANGUAGE SQL MODIFIES SQL DATA CASE RATING
SQL programming
121
WHEN 1 THEN UPDATE CORPDATA.EMPLOYEE SET SALARY = SALARY * 1.10, BONUS = 1000 WHERE EMPNO = EMPLOYEE_NUMBER; WHEN 2 THEN UPDATE CORPDATA.EMPLOYEE SET SALARY = SALARY * 1.05, BONUS = 500 WHERE EMPNO = EMPLOYEE_NUMBER; ELSE UPDATE CORPDATA.EMPLOYEE SET SALARY = SALARY * 1.03, BONUS = 0 WHERE EMPNO = EMPLOYEE_NUMBER; END CASE
This CREATE PROCEDURE statement: v Names the procedure UPDATE_SALARY_2. v Defines parameter EMPLOYEE_NUMBER which is an input parameter and is a character data type of length 6 and parameter RATING which is an input parameter and is an integer data type. v Indicates the procedure is an SQL procedure that modifies SQL data. v Defines the procedure body. When the procedure is called, input parameter RATING is checked and the appropriate update statement is executed. Multiple statements can be added to a procedure body by adding a compound statement. Within a compound statement, any number of SQL statements can be specified. In addition, SQL variables, cursors, and handlers can be declared. The following example takes as input the department number. It returns the total salary of all the employees in that department and the number of employees in that department who get a bonus.
CREATE PROCEDURE RETURN_DEPT_SALARY (IN DEPT_NUMBER CHAR(3), OUT DEPT_SALARY DECIMAL(15,2), OUT DEPT_BONUS_CNT INT) LANGUAGE SQL READS SQL DATA P1: BEGIN DECLARE EMPLOYEE_SALARY DECIMAL(9,2); DECLARE EMPLOYEE_BONUS DECIMAL(9,2); DECLARE TOTAL_SALARY DECIMAL(15,2)DEFAULT 0; DECLARE BONUS_CNT INT DEFAULT 0; DECLARE END_TABLE INT DEFAULT 0; DECLARE C1 CURSOR FOR SELECT SALARY, BONUS FROM CORPDATA.EMPLOYEE WHERE WORKDEPT = DEPT_NUMBER; DECLARE CONTINUE HANDLER FOR NOT FOUND SET END_TABLE = 1; DECLARE EXIT HANDLER FOR SQLEXCEPTION SET DEPT_SALARY = NULL; OPEN C1; FETCH C1 INTO EMPLOYEE_SALARY, EMPLOYEE_BONUS; WHILE END_TABLE = 0 DO SET TOTAL_SALARY = TOTAL_SALARY + EMPLOYEE_SALARY + EMPLOYEE_BONUS; IF EMPLOYEE_BONUS > 0 THEN SET BONUS_CNT = BONUS_CNT + 1; END IF; FETCH C1 INTO EMPLOYEE_SALARY, EMPLOYEE_BONUS; END WHILE; CLOSE C1; SET DEPT_SALARY = TOTAL_SALARY; SET DEPT_BONUS_CNT = BONUS_CNT; END P1
122
This CREATE PROCEDURE statement: v Names the procedure RETURN_DEPT_SALARY. v Defines parameter DEPT_NUMBER which is an input parameter and is a character data type of length 3, parameter DEPT_SALARY which is an output parameter and is a decimal data type, and parameter DEPT_BONUS_CNT which is an output parameter and is an integer data type. v Indicates the procedure is an SQL procedure that reads SQL data v Defines the procedure body. Declares SQL variables EMPLOYEE_SALARY and TOTAL_SALARY as decimal fields. Declares SQL variables BONUS_CNT and END_TABLE which are integers and are initialized to 0. Declares cursor C1 that selects the columns from the employee table. Declares a continue handler for NOT FOUND, which, when called sets variable END_TABLE to 1. This handler is called when the FETCH has no more rows to return. When the handler is called, SQLCODE and SQLSTATE are reinitialized to 0. Declares an exit handler for SQLEXCEPTION. If called, DEPT_SALARY is set to NULL and the processing of the compound statement is terminated. This handler is called if any errors occur, that is, the SQLSTATE class is not 00, 01 or 02. Since indicators are always passed to SQL procedures, the indicator value for DEPT_SALARY is -1 when the procedure returns. If this handler is called, SQLCODE and SQLSTATE are reinitialized to 0. If the handler for SQLEXCEPTION is not specified and an error occurs that is not handled in another handler, execution of the compound statement is terminated and the error is returned in the SQLCA. Similar to indicators, the SQLCA is always returned from SQL procedures. Includes an OPEN, FETCH, and CLOSE of cursor C1. If a CLOSE of the cursor is not specified, the cursor is closed at the end of the compound statement since SET RESULT SETS is not specified in the CREATE PROCEDURE statement. Includes a WHILE statement which loops until the last record is fetched. For each row retrieved, the TOTAL_SALARY is incremented and, if the employees bonus is more than 0, the BONUS_CNT is incremented. Returns DEPT_SALARY and DEPT_BONUS_CNT as output parameters. Compound statements can be made atomic so if an error occurs that is not expected, the statements within the atomic statement are rolled back. The atomic compound statements are implemented using SAVEPOINTS. If the compound statement is successful, the transaction is committed. The following example takes as input the department number. It ensures the EMPLOYEE_BONUS table exists, and inserts the name of all employees in the department who get a bonus. The procedure returns the total count of all employees who get a bonus.
CREATE PROCEDURE CREATE_BONUS_TABLE (IN DEPT_NUMBER CHAR(3), INOUT CNT INT) LANGUAGE SQL MODIFIES SQL DATA CS1: BEGIN ATOMIC DECLARE NAME VARCHAR(30) DEFAULT NULL; DECLARE CONTINUE HANDLER FOR SQLSTATE 42710 SELECT COUNT(*) INTO CNT FROM DATALIB.EMPLOYEE_BONUS; DECLARE CONTINUE HANDLER FOR SQLSTATE 23505 SET CNT = CNT - 1; DECLARE UNDO HANDLER FOR SQLEXCEPTION SET CNT = NULL; IF DEPT_NUMBER IS NOT NULL THEN CREATE TABLE DATALIB.EMPLOYEE_BONUS (FULLNAME VARCHAR(30), BONUS DECIMAL(10,2), PRIMARY KEY (FULLNAME)); FOR_1:FOR V1 AS C1 CURSOR FOR SELECT FIRSTNME, MIDINIT, LASTNAME, BONUS
SQL programming
123
FROM CORPDATA.EMPLOYEE WHERE WORKDEPT = CREATE_BONUS_TABLE.DEPT_NUMBER DO IF BONUS > 0 THEN SET NAME = FIRSTNME CONCAT CONCAT MIDINIT CONCAT CONCAT LASTNAME; INSERT INTO DATALIB.EMPLOYEE_BONUS VALUES(CS1.NAME, FOR_1.BONUS); SET CNT = CNT + 1; END IF; END FOR FOR_1; END IF; END CS1
This CREATE PROCEDURE statement: v Names the procedure CREATE_BONUS_TABLE. v Defines parameter DEPT_NUMBER which is an input parameter and is a character data type of length 3 and parameter CNT which is an input/output parameter and is an integer data type. v Indicates the procedure is an SQL procedure that modifies SQL data v Defines the procedure body. Declares SQL variable NAME as varying character. Declares a continue handler for SQLSTATE 42710, table already exists. If the EMPLOYEE_BONUS table already exists, the handler is called and retrieves the number of records in the table. The SQLCODE and SQLSTATE are reset to 0 and processing continues with the FOR statement. Declares a continue handler for SQLSTATE 23505, duplicate key. If the procedure attempts to insert a name that already exists in the table, the handler is called and decrements CNT. Processing continues on the SET statement following the INSERT statement. Declares an UNDO handler for SQLEXCEPTION. If called, the previous statements are rolled back, CNT is set to 0, and processing continues after the compound statement. In this case, since there is no statement following the compound statement, the procedure returns. Uses the FOR statement to declare cursor C1 to read the records from the EMPLOYEE table. Within the FOR statement, the column names from the select list are used as SQL variables that contain the data from the row fetched. For each row, data from columns FIRSTNME, MIDINIT, and LASTNAME are concatenated together with a blank in between and the result is put in SQL variable NAME. SQL variables NAME and BONUS are inserted into the EMPLOYEE_BONUS table. Because the data type of the select list items must be known when the procedure is created, the table specified in the FOR statement must exist when the procedure is created. An SQL variable name can be qualified with the label name of the FOR statement or compound statement in which it is defined. In the example, FOR_1.BONUS refers to the SQL variable that contains the value of column BONUS for each row selected. CS1.NAME is the variable NAME defined in the compound statement with the beginning label CS1. Parameter names can also be qualified with the procedure name. CREATE_BONUS_TABLE.DEPT_NUMBER is the DEPT_NUMBER parameter for the procedure CREATE_BONUS_TABLE. If unqualified SQL variable names are used in SQL statements where column names are also allowed, and the variable name is the same as a column name, the name will be used to refer to the column. You can also use dynamic SQL in an SQL procedure. The following example creates a table that contains all employees in a specific department. The department number is passed as input to the procedure and is concatenated to the table name.
CREATE PROCEDURE CREATE_DEPT_TABLE (IN P_DEPT CHAR(3)) LANGUAGE SQL BEGIN DECLARE STMT CHAR(1000); DECLARE MESSAGE CHAR(20); DECLARE TABLE_NAME CHAR(30); DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET MESSAGE = ok;
124
SET TABLE_NAME = CORPDATA.DEPT_ CONCAT P_DEPT CONCAT _T; SET STMT = DROP TABLE CONCAT TABLE_NAME; PREPARE S1 FROM STMT; EXECUTE S1; SET STMT = CREATE TABLE CONCAT TABLE_NAME CONCAT ( EMPNO CHAR(6) NOT NULL, FIRSTNME VARCHAR(12) NOT NULL, MIDINIT CHAR(1) NOT NULL, LASTNAME CHAR(15) NOT NULL, SALARY DECIMAL(9,2)); PREPARE S2 FROM STMT; EXECUTE S2; SET STMT = INSERT INTO CONCAT TABLE_NAME CONCAT SELECT EMPNO, FIRSTNME, MIDINIT, LASTNAME, SALARY FROM CORPDATA.EMPLOYEE WHERE WORKDEPT = ?; PREPARE S3 FROM STMT; EXECUTE S3 USING P_DEPT; END
This CREATE PROCEDURE statement: v Names the procedure CREATE_DEPT_TABLE v Defines parameter P_DEPT which is an input parameter and is a character data type of length 3. v Indicates the procedure is an SQL procedure. v Defines the procedure body. Declares SQL variable STMT and an SQL variable TABLE_NAME as character. Declares a CONTINUE handler. The procedure attempts to DROP the table in case it already exists. If the table does not exist, the first EXECUTE fails. With the handler, processing will continue. Sets variable TABLE_NAME to DEPT_ followed by the characters passed in parameter P_DEPT, followed by _T. Sets variable STMT to the DROP statement, and prepares and executes the statement. Sets variable STMT to the CREATE statement, and prepares and executes the statement. Sets variable STMT to the INSERT statement, and prepares and executes the statement. A parameter marker is specified in the where clause. When the statement is executed, the variable P_DEPT is passed on the USING clause. If the procedure is called passing value D21 for the department, table DEPT_D21_T is created and the table is initialized with all the employees that are in department D21.
125
v Dynamic CALL statement where no CREATE PROCEDURE exists Notes: Dynamic here refers to: v A dynamically prepared and executed CALL statement. v A CALL statement issued in an interactive environment (for example, through STRSQL or Query Manager). v A CALL statement executed in an EXECUTE IMMEDIATE statement. Using the CALL statement where procedure definition exists: This type of CALL statement reads all the information about the procedure and the argument attributes from the CREATE PROCEDURE catalog definition. The following PL/I example shows a CALL statement that corresponds to the CREATE PROCEDURE statement shown.
DCL HV1 CHAR(10); DCL IND1 FIXED BIN(15); : EXEC SQL CREATE P1 PROCEDURE (INOUT PARM1 CHAR(10)) EXTERNAL NAME MYLIB.PROC1 LANGUAGE C GENERAL WITH NULLS; : EXEC SQL CALL P1 (:HV1 :IND1); :
When this CALL statement is issued, a call to program MYLIB/PROC1 is made and two arguments are passed. Because the language of the program is ILE C, the first argument is a C NUL-terminated string, 11 characters long, which contains the contents of host variable HV1. On a call to an ILE C procedure, SQL adds one character to the parameter declaration if the parameter is declared to be a character, graphic, date, time, or timestamp variable. The second argument is the indicator array. In this case, it is one short integer because there is only one parameter in the CREATE PROCEDURE statement. This argument contains the contents of indicator variable IND1 on entry to the procedure. Since the first parameter is declared as INOUT, SQL updates the host variable HV1 and the indicator variable IND1 with the values returned from MYLIB.PROC1 before returning to the user program. Notes: 1. The procedure names specified on the CREATE PROCEDURE and CALL statements must match EXACTLY in order for the link between the two to be made during the SQL precompile of the program. 2. For an embedded CALL statement where both a CREATE PROCEDURE and a DECLARE PROCEDURE statement exist, the DECLARE PROCEDURE statement will be used. Using the embedded CALL statement where no procedure definition exists: A static CALL statement without a corresponding CREATE PROCEDURE statement is processed with these rules. v All host variable arguments are treated as INOUT type parameters. v The CALL type is GENERAL (no indicator argument is passed). v The program to call is determined based on the procedure name specified on the CALL, and, if necessary, the naming convention.
126
v The language of the program to call is determined based on information retrieved from the system about the program. Example: Embedded CALL statement where no procedure definition exists The following PL/I example shows an embedded CALL statement where no procedure definition exists:
DCL HV2 CHAR(10); : EXEC SQL CALL P2 (:HV2); :
When the CALL statement is issued, SQL attempts to find the program based on standard SQL naming conventions. For the preceding example, assume that the naming option of *SYS (system naming) is used and that a DFTRDBCOL parameter is not specified on the Create SQL PL/I Program (CRTSQLPLI) command. In this case, the library list is searched for a program named P2. Because the call type is GENERAL, no additional argument is passed to the program for indicator variables. Note: If an indicator variable is specified on the CALL statement and its value is less than zero when the CALL statement is executed, an error results because there is no way to pass the indicator to the procedure. Assuming program P2 is found in the library list, the contents of host variable HV2 are passed in to the program on the CALL and the argument returned from P2 is mapped back to the host variable after P2 has completed execution. For numeric constants passed on a CALL statement, the following rules apply: v All integer constants are passed as fullword binary integers. v All decimal constants are passed as packed decimal values. Precision and scale are determined based on the constant value. For instance, a value of 123.45 is passed as a packed decimal(5,2). Likewise, a value of 001.01 is also passed with a precision of 5 and a scale of 2. v All floating point constants are passed as double-precision floating point. Special registers specified on a dynamic CALL statement are passed as follows: CURRENT DATE Passed as a 10-byte character string in ISO format. CURRENT DEGREE Passed as a 5-byte character string. CURRENT TIME Passed as an 8-byte character string in ISO format. CURRENT TIMEZONE Passed as a packed decimal number with a precision of 6 and a scale of 0. CURRENT TIMESTAMP Passed as a 26-byte character string in IBM SQL format. CURRENT SCHEMA Passed as an 128-byte varying length character string. CURRENT SERVER Passed as an 18-byte varying length character string. USER Passed as an 18-byte varying length character string. CURRENT PATH Passed as a 3483-byte varying length character string.
SQL programming
127
SESSION_USER Passed as a 128-byte varying length character string. SYSTEM_USER Passed as a 128-byte varying length character string. Using the embedded CALL statement with an SQLDA: In either type of embedded CALL statement (where a procedure definition might or might not exist), an SQLDA rather than a parameter list can be passed. The following C examples illustrates this. Assume that the stored procedure is expecting 2 parameters, the first of type SHORT INT and the second of type CHAR with a length of 4. Note: By using the code examples, you agree to the terms of the Code license and disclaimer information on page 300.
#define SQLDA_HV_ENTRIES 2 #define SHORTINT 500 #define NUL_TERM_CHAR 460 exec sql include sqlca; exec sql include sqlda; ... typedef struct sqlda Sqlda; typedef struct sqlda* Sqldap; ... main() { Sqldap dap; short col1; char col2[4]; int bc; dap = (Sqldap) malloc(bc=SQLDASIZE(SQLDA_HV_ENTRIES)); /* SQLDASIZE is a macro defined in the sqlda include */ col1 = 431; strcpy(col2,"abc"); strncpy(dap->sqldaid,"SQLDA ",8); dap->sqldabc = bc; /* bc set in the malloc statement above */ dap->sqln = SQLDA_HV_ENTRIES; dap->sqld = SQLDA_HV_ENTRIES; dap->sqlvar[0].sqltype = SHORTINT; dap->sqlvar[0].sqllen = 2; dap->sqlvar[0].sqldata = (char*) &col1; dap->sqlvar[0].sqlname.length = 0; dap->sqlvar[1].sqltype = NUL_TERM_CHAR; dap->sqlvar[1].sqllen = 4; dap->sqlvar[1].sqldata = col2; ... EXEC SQL CALL P1 USING DESCRIPTOR :*dap; ... }
The name of the called procedure may also be stored in a host variable and the host variable used in the CALL statement, instead of the hard-coded procedure name. For example:
... main() { char proc_name[15]; ... strcpy (proc_name, "MYLIB.P3");
128
In the above example, if MYLIB.P3 is expecting parameters, either a parameter list or an SQLDA passed with the USING DESCRIPTOR clause may be used, as shown in the previous example. When a host variable containing the procedure name is used in the CALL statement and a CREATE PROCEDURE catalog definition exists, it will be used. The procedure name cannot be specified as a parameter marker. Using the dynamic CALL statement where no CREATE PROCEDURE exists: These rules pertain to the processing of a dynamic CALL statement when there is no CREATE PROCEDURE definition. v All arguments are treated as IN type parameters. v The CALL type is GENERAL (no indicator argument is passed). v The program to call is determined based on the procedure name specified on the CALL and the naming convention. v The language of the program to call is determined based on information retrieved from the system about the program. Example: Dynamic CALL statement where no CREATE PROCEDURE exists The following C example shows a dynamic CALL statement:
char hv3[10],string[100]; : strcpy(string,"CALL MYLIB.P3 (P3 TEST)"); EXEC SQL EXECUTE IMMEDIATE :string; :
This example shows a dynamic CALL statement executed through an EXECUTE IMMEDIATE statement. The call is made to program MYLIB.P3 with one parameter passed as a character variable containing P3 TEST. When executing a CALL statement and passing a constant, as in the previous example, the length of the expected argument in the program must be kept in mind. If program MYLIB.P3 expected an argument of only 5 characters, the last 2 characters of the constant specified in the example is lost to the program. Note: For this reason, it is always safer to use host variables on the CALL statement so that the attributes of the procedure can be matched exactly and so that characters are not lost. For dynamic SQL, host variables can be specified for CALL statement arguments if the PREPARE and EXECUTE statements are used to process it. Examples: CALL statements: These examples show how the arguments of a CALL statement are passed to a procedure for several languages, and how the arguments are received into local variables in the procedure. Example 1: ILE C and PL/I procedures called from an ILE C program: This example shows an ILE C program that uses the CREATE PROCEDURE definitions to call the P1 and P2 procedures. Procedure P1 is written in ILE C and has 10 parameters. Procedure P2 is written in PL/I and also has 10 parameters.
SQL programming
129
Note: By using the code examples, you agree to the terms of the Code license and disclaimer information on page 300. Calling the P1 and P2 procedures
/**************************************************************/ /*********** START OF SQL C Application ***********************/ #include <stdio.h> #include <string.h> #include <decimal.h> main() { EXEC SQL INCLUDE SQLCA; char PARM1[10]; signed long int PARM2; signed short int PARM3; float PARM4; double PARM5; decimal(10,5) PARM6; struct { signed short int parm7l; char parm7c[10]; } PARM7; char PARM8[10]; /* FOR DATE */ char PARM9[8]; /* FOR TIME */ char PARM10[26]; /* FOR TIMESTAMP */ /*******************************************************/ /* Initialize variables for the call to the procedures */ /*******************************************************/ strcpy(PARM1,"PARM1"); PARM2 = 7000; PARM3 = -1; PARM4 = 1.2; PARM5 = 1.0; PARM6 = 10.555; PARM7.parm7l = 5; strcpy(PARM7.parm7c,"PARM7"); strncpy(PARM8,"1994-12-31",10); /* FOR DATE */ strncpy(PARM9,"12.00.00",8); /* FOR TIME */
130
strncpy(PARM10,"1994-12-31-12.00.00.000000",26); /* FOR TIMESTAMP */ /***********************************************/ /* Call the C procedure */ /* */ /* */ /***********************************************/ EXEC SQL CALL P1 (:PARM1, :PARM2, :PARM3, :PARM4, :PARM5, :PARM6, :PARM7, :PARM8, :PARM9, :PARM10 ); if (strncmp(SQLSTATE,"00000",5)) { /* Handle error or warning returned on CALL statement */ } /* Process return values from the CALL. : /***********************************************/ /* Call the PLI procedure */ /* */ /* */ /***********************************************/ /* Reset the host variables before making the CALL /* : EXEC SQL CALL P2 (:PARM1, :PARM2, :PARM3, :PARM4, :PARM5, :PARM6, :PARM7, :PARM8, :PARM9, :PARM10 ); if (strncmp(SQLSTATE,"00000",5)) { /* Handle error or warning returned on CALL statement } /* Process return values from the CALL. : } */
*/ */
*/ */
Procedure P1
/******** START OF C PROCEDURE P1 *******************************/ /* PROGRAM TEST12/CALLPROC2 */ /****************************************************************/ #include <stdio.h> #include <string.h> #include <decimal.h> main(argc,argv) int argc; char *argv[]; { char parm1[11]; long int parm2; short int parm3,i,j,*ind,ind1,ind2,ind3,ind4,ind5,ind6,ind7, ind8,ind9,ind10; float parm4; double parm5; decimal(10,5) parm6; char parm7[11]; char parm8[10]; char parm9[8]; char parm10[26];
SQL programming
131
/* /* /* /* /* /* /* /* /*
*********************************************************/ Receive the parameters into the local variables */ Character, date, time, and timestamp are passed as */ NUL terminated strings - cast the argument vector to */ the proper data type for each variable. Note that */ the argument vector can be used directly instead of */ copying the parameters into local variables - the copy */ is done here just to illustrate the method. */ *********************************************************/ */ */ */ */ */ */
/* Copy 10 byte character string into local variable strcpy(parm1,argv[1]); /* Copy 4 byte integer into local variable parm2 = *(int *) argv[2]; /* Copy 2 byte integer into local variable parm3 = *(short int *) argv[3]; /* Copy floating point number into local variable parm4 = *(float *) argv[4]; /* Copy double precision number into local variable parm5 = *(double *) argv[5]; /* Copy decimal number into local variable parm6 = *(decimal(10,5) *) argv[6]; /**********************************************************/ /* Copy NUL terminated string into local variable. */ /* Note that the parameter in the CREATE PROCEDURE was */ /* declared as varying length character. For C, varying */ /* length are passed as NUL terminated strings unless */ /* FOR BIT DATA is specified in the CREATE PROCEDURE */ /**********************************************************/ strcpy(parm7,argv[7]); /**********************************************************/ /* Copy date into local variable. */ /* Note that date and time variables are always passed in */ /* ISO format so that the lengths of the strings are */ /* known. strcpy works here just as well. */ /**********************************************************/ strncpy(parm8,argv[8],10); /* Copy time into local variable strncpy(parm9,argv[9],8); */
/**********************************************************/ /* Copy timestamp into local variable. */ /* IBM SQL timestamp format is always passed so the length*/ /* of the string is known. */ /**********************************************************/ strncpy(parm10,argv[10],26); /**********************************************************/ /* The indicator array is passed as an array of short */ /* integers. There is one entry for each parameter passed */ /* on the CREATE PROCEDURE (10 for this example). */ /* Below is one way to set each indicator into separate */ /* variables. */ /**********************************************************/ ind = (short int *) argv[11]; ind1 = *(ind++); ind2 = *(ind++); ind3 = *(ind++); ind4 = *(ind++); ind5 = *(ind++);
132
ind6 = *(ind++); ind7 = *(ind++); ind8 = *(ind++); ind9 = *(ind++); ind10 = *(ind++); : /* Perform any additional processing here */ : return; } /******** END OF C PROCEDURE P1 *******************************/
Procedure P2
/******** START OF PL/I PROCEDURE P2 **************************/ /******** PROGRAM TEST12/CALLPROC *****************************/ /**************************************************************/ CALLPROC :PROC( PARM1,PARM2,PARM3,PARM4,PARM5,PARM6,PARM7, PARM8,PARM9,PARM10,PARM11); DCL SYSPRINT FILE STREAM OUTPUT EXTERNAL; OPEN FILE(SYSPRINT); DCL PARM1 CHAR(10); DCL PARM2 FIXED BIN(31); DCL PARM3 FIXED BIN(15); DCL PARM4 BIN FLOAT(22); DCL PARM5 BIN FLOAT(53); DCL PARM6 FIXED DEC(10,5); DCL PARM7 CHARACTER(10) VARYING; DCL PARM8 CHAR(10); /* FOR DATE */ DCL PARM9 CHAR(8); /* FOR TIME */ DCL PARM10 CHAR(26); /* FOR TIMESTAMP */ DCL PARM11(10) FIXED BIN(15); /* Indicators */ /* PERFORM LOGIC - Variables can be set to other values for */ /* return to the calling program. */ : END CALLPROC;
Example 2: A REXX procedure called from an ILE C program: This example shows a REXX procedure called from an ILE C program. Defining the REXX procedure
EXEC SQL CREATE PROCEDURE REXXPROC (IN PARM1 CHARACTER(20), IN PARM2 INTEGER, IN PARM3 DECIMAL(10,5), IN PARM4 DOUBLE PRECISION, IN PARM5 VARCHAR(10), IN PARM6 GRAPHIC(4), IN PARM7 VARGRAPHIC(10), IN PARM8 DATE, IN PARM9 TIME, IN PARM10 TIMESTAMP) EXTERNAL NAME TEST.CALLSRC(CALLREXX) LANGUAGE REXX GENERAL WITH NULLS
Note: By using the code examples, you agree to the terms of the Code license and disclaimer information on page 300.
SQL programming
133
*/
134
/**********************************************************************/ /****** START OF REXX MEMBER TEST/CALLSRC CALLREXX ********************/ /**********************************************************************/ /* REXX source member TEST/CALLSRC CALLREXX */ /* Note the extra parameter being passed for the indicator*/ /* array. */ /* */ /* ACCEPT THE FOLLOWING INPUT VARIABLES SET TO THE */ /* SPECIFIED VALUES : */ /* AR1 CHAR(20) = TestingREXX */ /* AR2 INTEGER = 12345 */ /* AR3 DECIMAL(10,5) = 5.5 */ /* AR4 DOUBLE PRECISION = 3e3 */ /* AR5 VARCHAR(10) = parm6 */ /* AR6 GRAPHIC = GC1C1C2C2C3C3 */ /* AR7 VARGRAPHIC = */ /* GE2E2E3E3E4E4E5E5E6E6E7E7E8E8E9E9EAEA */ /* AR8 DATE = 1994-01-01 */ /* AR9 TIME = 13.01.00 */ /* AR10 TIMESTAMP = */ /* 1994-01-01-13.01.00.000000 */ /* AR11 INDICATOR ARRAY = +0+0+0+0+0+0+0+0+0+0 */ /**********************************************************/ /* Parse the arguments into individual parameters */ /**********************************************************/ parse arg ar1 ar2 ar3 ar4 ar5 ar6 ar7 ar8 ar9 ar10 ar11 /**********************************************************/ /* Verify that the values are as expected */ /**********************************************************/ if ar1<>"TestingREXX" then signal ar1tag if ar2<>12345 then signal ar2tag if ar3<>5.5 then signal ar3tag if ar4<>3e3 then signal ar4tag if ar5<>"parm6" then signal ar5tag if ar6 <>"GAABBCC" then signal ar6tag if ar7 <>"GSSTTUUVVWWXXYYZZAA" then , signal ar7tag if ar8 <> "1994-01-01" then signal ar8tag if ar9 <> "13.01.00" then signal ar9tag if ar10 <> "1994-01-01-13.01.00.000000" then signal ar10tag if ar11 <> "+0+0+0+0+0+0+0+0+0+0" then signal ar11tag /************************************************************/ /* Perform other processing as necessary .. */ /************************************************************/ : /************************************************************/ /* Indicate the call was successful by exiting with a */ /* return code of 0 */ /************************************************************/ exit(0) ar1tag: say "ar1 did not match" ar1 exit(1) ar2tag: say "ar2 did not match" ar2 exit(1) : : /************ END OF REXX MEMBER **********************************/
SQL programming
135
136
ODBC application Note: Some of the logic has been removed. Note: By using the code examples, you agree to the terms of the Code license and disclaimer information on page 300.
: strcpy(stmt,"call prod.resset()"); rc = SQLExecDirect(hstmt,stmt,SQL_NTS); if (rc == SQL_SUCCESS) { // CALL statement has executed successfully. Process the result set. // Get number of result columns for the result set. rc = SQLNumResultCols(hstmt, &wNum); if (rc == SQL_SUCCESS) // Get description of result columns in result set { rc = SQLDescribeCol(hstmt,); if (rc == SQL_SUCCESS) : { // Bind result columns based on attributes returned // rc = SQLBindCol(hstmt,); : // FETCH records until EOF is returned rc = SQLFetch(hstmt); while (rc == SQL_SUCCESS) { // process result returned on the SQLFetch : rc = SQLFetch(hstmt); } : } // Close the result set cursor when done with it. rc = SQLFreeStmt(hstmt,SQL_CLOSE); :
Example 2: Calling a stored procedure that returns a result set from a nested procedure: This example shows how a nested stored procedure can open and return a result set to the outermost procedure. To return a result set to the outermost procedure in an environment where there are nested stored procedures, the RETURN TO CLIENT returnability attribute should be used on the DECLARE CURSOR statement or on the SET RESULT SETS statement to indicate that the cursors are to be returned to the application which called the outermost procedure. Note that this nested procedure returns two result sets to the client; the first, an array result set, and the second a cursor result set. Both an ODBC and a JDBC client application are shown below along with the stored procedures. Defining the stored procedures
CREATE PROCEDURE prod.rtnnested () LANGUAGE CL DYNAMIC RESULT SET 2 EXTERNAL NAME prod.rtnnested GENERAL CREATE PROCEDURE prod.rtnclient () LANGUAGE RPGLE EXTERNAL NAME prod.rtnclient GENERAL
Note: By using the code examples, you agree to the terms of the Code license and disclaimer information on page 300.
SQL programming
137
LR
ODBC application
//******************************************************************* // // Module: // Examples.C // // Purpose: // Perform calls to stored procedures to get back result sets. // // ******************************************************************* #include "common.h" #include "stdio.h" // ******************************************************************* // // Local function prototypes. // // ******************************************************************* SWORD BOOL BOOL BOOL FAR FAR FAR FAR PASCAL PASCAL PASCAL PASCAL RetClient(lpSERVERINFO lpSI); Bind_Params(HSTMT); Bind_First_RS(HSTMT); Bind_Second_RS(HSTMT);
// ******************************************************************* // // Constant strings definitions for SQL statements used in // the auto test. // // ******************************************************************* // // Declarations of variables global to the auto test. // // ******************************************************************* #define ARRAYCOL_LEN 16 #define LSTNAM_LEN 8
138
char char
stmt[2048]; buf[2000];
UDWORD rowcnt; char arraycol[ARRAYCOL_LEN+1]; char lstnam[LSTNAM_LEN+1]; SDWORD cbcol1,cbcol2; lpSERVERINFO lpSI; /* Pointer to a SERVERINFO structure. */
// ******************************************************************** // // Define the auto test name and the number of test cases // for the current auto test. These informations will // be returned by AutoTestName(). // // ******************************************************************** LPSTR szAutoTestName = CREATE_NAME("Result Sets Examples"); UINT iNumOfTestCases = 1;
// ******************************************************************* // // Define the structure for test case names, descriptions, // and function names for the current auto test. // Test case names and descriptions will be returned by // AutoTestDesc(). Functions will be run by // AutoTestFunc() if the bits for the corresponding test cases // are set in the rglMask member of the SERVERINFO // structure. // // ******************************************************************* struct TestCase TestCasesInfo[] = { "Return to Client", "2 result sets ", RetClient }; // ******************************************************************* // // Sample return to Client: // Return to Client result sets. Call a CL program which in turn // calls an RPG program which returns 2 result sets. The first // result set is an array result set and the second is a cursor // result set. // // // ******************************************************************* SWORD FAR PASCAL RetClient(lpSERVERINFO lpSI) { SWORD sRC = SUCCESS; RETCODE returncode; HENV henv; HDBC hdbc; HSTMT hstmt;
139
goto ExitNoDisconnect; } // ******************************************************** // Call CL program PROD.RTNNESTED, which in turn calls RPG // program RTNCLIENT. // ******************************************************** strcpy(stmt,"CALL PROD.RTNNESTED()"); // ************************************************************** // Call the CL program prod.rtnnested. This program will in turn // call the RPG program proc.rtnclient, which will open 2 result // sets for return to this ODBC application. // ************************************************************* returncode = SQLExecDirect(hstmt,stmt,SQL_NTS); if (returncode != SQL_SUCCESS) { vWrite(lpSI, "CALL PROD.RTNNESTED is not Successful", TRUE); } else { vWrite(lpSI, "CALL PROC.RTNNESTED was Successful", TRUE); } // ************************************************************** // Bind the array result set output column. Note that the result // sets are returned to the application in the order that they // are specified on the SET RESULT SETS statement. // ************************************************************* if (Bind_First_RS(hstmt) == FALSE) { myRETCHECK(lpSI, henv, hdbc, hstmt, SQL_SUCCESS, returncode, "Bind_First_RS"); sRC = FAIL; goto ErrorRet; } else { vWrite(lpSI, "Bind_First_RS Complete...", TRUE); } // ************************************************************** // Fetch the rows from the array result set. After the last row // is read, a returncode of SQL_NO_DATA_FOUND will be returned to // the application on the SQLFetch request. // ************************************************************** returncode = SQLFetch(hstmt); while(returncode == SQL_SUCCESS) { wsprintf(stmt,"array column = %s",arraycol); vWrite(lpSI,stmt,TRUE); returncode = SQLFetch(hstmt); } if (returncode == SQL_NO_DATA_FOUND) ; else { myRETCHECK(lpSI, henv, hdbc, hstmt, SQL_SUCCESS_WITH_INFO, returncode, "SQLFetch"); sRC = FAIL; goto ErrorRet; } // ******************************************************** // Get any remaining result sets from the call. The next // result set corresponds to cursor C2 opened in the RPG // Program. // ******************************************************** returncode = SQLMoreResults(hstmt); if (returncode != SQL_SUCCESS) { myRETCHECK(lpSI, henv, hdbc, hstmt, SQL_SUCCESS, returncode, "SQLMoreResults"); sRC = FAIL; goto ErrorRet;
140
} // // // // //
************************************************************** Bind the cursor result set output column. Note that the result sets are returned to the application in the order that they are specified on the SET RESULT SETS statement. *************************************************************
if (Bind_Second_RS(hstmt) == FALSE) { myRETCHECK(lpSI, henv, hdbc, hstmt, SQL_SUCCESS, returncode, "Bind_Second_RS"); sRC = FAIL; goto ErrorRet; } else { vWrite(lpSI, "Bind_Second_RS Complete...", TRUE); } // ************************************************************** // Fetch the rows from the cursor result set. After the last row // is read, a returncode of SQL_NO_DATA_FOUND will be returned to // the application on the SQLFetch request. // ************************************************************** returncode = SQLFetch(hstmt); while(returncode == SQL_SUCCESS) { wsprintf(stmt,"lstnam = %s",lstnam); vWrite(lpSI,stmt,TRUE); returncode = SQLFetch(hstmt); } if (returncode == SQL_NO_DATA_FOUND) ; else { myRETCHECK(lpSI, henv, hdbc, hstmt, SQL_SUCCESS_WITH_INFO, returncode, "SQLFetch"); sRC = FAIL; goto ErrorRet; } returncode = SQLFreeStmt(hstmt,SQL_CLOSE); if (returncode != SQL_SUCCESS) { myRETCHECK(lpSI, henv, hdbc, hstmt, SQL_SUCCESS, returncode, "Close statement"); sRC = FAIL; goto ErrorRet; } else { vWrite(lpSI, "Close statement...", TRUE); }
ErrorRet: FullDisconnect(lpSI, henv, hdbc, hstmt); if (sRC == FAIL) { // a failure in an ODBC function that prevents completion of the // test - for example, connect to the server vWrite(lpSI, "\t\t *** Unrecoverable RTNClient Test FAILURE ***", } /* endif */ ExitNoDisconnect: return(sRC); } // RetClient
TRUE);
SQL programming
141
BOOL FAR PASCAL Bind_First_RS(HSTMT hstmt) { RETCODE rc = SQL_SUCCESS; rc = SQLBindCol(hstmt,1,SQL_C_CHAR,arraycol,ARRAYCOL_LEN+1, &cbcol1); if (rc != SQL_SUCCESS) return FALSE; return TRUE; } BOOL FAR PASCAL Bind_Second_RS(HSTMT hstmt) { RETCODE rc = SQL_SUCCESS; rc = SQLBindCol(hstmt,1,SQL_C_CHAR,lstnam,LSTNAM_LEN+1,&dbcol2); if (rc != SQL_SUCCESS) return FALSE; return TRUE; }
JDBC application
//----------------------------------------------------------// Call Nested procedures which return result sets to the // client, in this case a JDBC client. //----------------------------------------------------------import java.sql.*; public class callNested { public static void main (String argv[]) // Main entry point { try { Class.forName("com.ibm.db2.jdbc.app.DB2Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { Connection jdbcCon = DriverManager.getConnection("jdbc:db2:lp066ab","Userid","xxxxxxx"); jdbcCon.setAutoCommit(false); CallableStatement cs = jdbcCon.prepareCall("CALL PROD.RTNNESTED"); cs.execute(); ResultSet rs1 = cs.getResultSet(); int r = 0; while (rs1.next()) { r++; String s1 = rs1.getString(1); System.out.print("Result set 1 Row: " + r + ": "); System.out.print(s1 + " " ); System.out.println(); } cs.getMoreResults(); r = 0; ResultSet rs2 = cs.getResultSet(); while (rs2.next()) { r++; String s2 = rs2.getString(1); System.out.print("Result set 2 Row: " + r + ": "); System.out.print(s2 + " "); System.out.println(); } }
142
catch ( SQLException e ) { System.out.println( "SQLState: " + e.getSQLState() ); System.out.println( "Message : " + e.getMessage() ); e.printStackTrace(); } } // main }
SQL programming
143
Table 38. Data types of parameters (continued) SQL data type CLOB C and C++ CLOB structured form CL N/A COBOL and ILE COBOL CLOB structured form Note: Only supported for ILE COBOL. GRAPHIC(n) wchar_t ... [n+1] N/A PIC G(n) DISPLAY-1 or PIC N(n) Note: Only supported for ILE COBOL. VARGRAPHIC(n) VARGRAPHIC structured form N/A Varying-Length Graphic String Note: Only supported for ILE COBOL. DBCLOB DBCLOB structured form N/A DBCLOB structured form Note: Only supported for ILE COBOL. BINARY VARBINARY BLOB BINARY structured form VARBINARY structured form BLOB structured form N/A N/A N/A BINARY structured form VARBINARY structured form BLOB structured form Note: Only supported for ILE COBOL. DATE char ... [11] TYPE(*CHAR) LEN(10) PIC X(10) Note: For ILE COBOL only, FORMAT DATE. TIME char ... [9] TYPE(*CHAR) LEN(8) PIC X(8) Note: For ILE COBOL only, FORMAT TIME. TIMESTAMP char ... [27] TYPE(*CHAR) LEN(26) PIC X(26) Note: For ILE COBOL only, FORMAT TIMESTAMP. ROWID DataLink Indicator Variable ROWID structured form N/A short N/A N/A N/A ROWID structured form N/A PIC S9(4) BINARY
Table 39. Data types of parameters SQL data type SMALLINT INTEGER BIGINT DECIMAL(p,s) NUMERIC(p,s) REAL or FLOAT(p) Java parameter style JAVA short int long BigDecimal BigDecimal float Java parameter style DB2GENERAL short int long BigDecimal BigDecimal float PL/I FIXED BIN(15) FIXED BIN(31) N/A FIXED DEC(p,s) N/A FLOAT BIN(p)
144
Table 39. Data types of parameters (continued) SQL data type DOUBLE PRECISION or FLOAT or FLOAT(p) CHARACTER(n) VARCHAR(n) VARCHAR(n) FOR BIT DATA CLOB GRAPHIC(n) VARGRAPHIC(n) DBCLOB BINARY VARBINARY BLOB DATE TIME TIMESTAMP ROWID DataLink Indicator Variable Java parameter style JAVA double String String byte[ ] java.sql.Clob String String java.sql.Clob byte[ ] byte[ ] java.sql.Blob Date Time Timestamp byte[] N/A N/A Java parameter style DB2GENERAL double String String com.ibm.db2.app.Blob com.ibm.db2.app.Clob String String com.ibm.db2.app.Clob com.ibm.db2.app.Blob com.ibm.db2.app.Blob com.ibm.db2.app.Blob String String String com.ibm.db2.app.Blob N/A N/A PL/I FLOAT BIN(p) CHAR(n) CHAR(n) VAR CHAR(n) VAR CLOB structured form N/A N/A DBCLOB structured form BINARY structured form VARBINARY structured form BLOB structured form CHAR(10) CHAR(8) CHAR(26) ROWID structured form N/A FIXED BIN(15)
Table 40. Data types of parameters SQL data type SMALLINT REXX N/A RPG Data structure that contains a single sub-field. B in position 43, length must be 2, and 0 in position 52 of the sub-field specification. ILE RPG Data specification. B in position 40, length must be <= 4, and 00 in positions 41-42 of the sub-field specification. or Data specification. I in position 40, length must be 5, and 00 in positions 41-42 of the sub-field specification. INTEGER numeric string with Data structure that contains a no decimal (and an single sub-field. B in position 43, optional leading sign) length must be 4, and 0 in position 52 of the sub-field specification. Data specification. B in position 40, length must be <=09 and >=05, and 00 in positions 41-42 of the sub-field specification. or Data specification. I in position 40, length must be 10, and 00 in positions 41-42 of the sub-field specification.
SQL programming
145
Table 40. Data types of parameters (continued) SQL data type BIGINT REXX N/A RPG N/A ILE RPG Data specification. I in position 40, length must be 20, and 00 in positions 41-42 of the sub-field specification. Data specification. P in position 40 and 00 through 31 in positions 41-42 of the sub-field specification.
DECIMAL(p,s)
numeric string with a Data structure that contains a decimal (and an single sub-field. P in position 43 optional leading sign) and 0 through 9 in position 52 of the sub-field specification. or A numeric input field or calculation result field. N/A Data structure that contains a single sub-field. Blank in position 43 and 0 through 9 in position 52 of the sub-field specification. N/A
NUMERIC(p,s)
Data specification. S in position 40, or Blank in position 40 and 00 through 31 in position 41-42 of the sub-field specification. Data specification. F in position 40, length must be 4.
REAL or FLOAT(p)
DOUBLE PRECISION string with digits, or FLOAT or then an E, (then an FLOAT(p) optional sign), then digits CHARACTER(n) string with n characters within two apostrophes
N/A
Data structure field without sub-fields or data structure that contains a single sub-field. Blank in position 43 and 52 of the sub-field specification. or A character input field or calculation result field. N/A
Data specification. A in position 40, or Blank in position 40 and 41-42 of the sub-field specification.
VARCHAR(n)
Data specification. A in position 40, or Blank in position 40 and 41-42 of the sub-field specification and the keyword VARYING in positions 44-80. Data specification. A in position 40, or Blank in position 40 and 41-42 of the sub-field specification and the keyword VARYING in positions 44-80. CLOB structured form Data specification. G in position 40 of the sub-field specification. Data specification. G in position 40 of the sub-field specification and the keyword VARYING in positions 44-80. DBCLOB structured form BINARY structured form VARBINARY structured form BLOB structured form
N/A
CLOB GRAPHIC(n)
N/A
N/A
string starting with N/A G, then n double byte characters, then string starting with N/A G, then n double byte characters, then N/A N/A N/A N/A N/A N/A N/A N/A
VARGRAPHIC(n)
146
Table 40. Data types of parameters (continued) SQL data type DATE REXX string with 10 characters within two apostrophes RPG ILE RPG
Data structure field without Data specification. D in position sub-fields or data structure that 40 of the sub-field specification. contains a single sub-field. Blank DATFMT(*ISO) in position 44-80. in position 43 and 52 of the sub-field specification. Length is 10. or A character input field or calculation result field. Data structure field without Data specification. T in position sub-fields or data structure that 40 of the sub-field specification. contains a single sub-field. Blank TIMFMT(*ISO) in position 44-80. in position 43 and 52 of the sub-field specification. Length is 8. or A character input field or calculation result field. Data structure field without Data specification. Z in position sub-fields or data structure that 40 of the sub-field specification. contains a single sub-field. Blank in position 43 and 52 of the sub-field specification. Length is 26. or A character input field or calculation result field. N/A N/A ROWID structured form N/A Data specification. B in position 40, length must be <=4, and 00 in positions 41-42 of the sub-field specification.
TIME
TIMESTAMP
N/A N/A
numeric string with Data structure that contains a no decimal (and an single sub-field. B in position 43, optional leading sign). length must be 2, and 0 in position 52 of the sub-field specification.
147
The following example illustrates the handling of indicator variables in CALL statements. Notice that the logic checks the value of the indicator variable before using the associated variable. Also note the method that the indicator variables are passed into procedure PROC1 (as a third argument consisting of an array of two-byte values). Note: By using the code examples, you agree to the terms of the Code license and disclaimer information on page 300. Assume a procedure was defined as follows:
CREATE PROCEDURE PROC1 (INOUT DECIMALOUT DECIMAL(7,2), INOUT DECOUT2 DECIMAL(7,2)) EXTERNAL NAME LIB1.PROC1 LANGUAGE RPGLE GENERAL WITH NULLS)
148
C* : C* INOUTP DOES NOT CONTAIN MEANINGFUL DATA C* C ELSE C* : C* INOUTP CONTAINS MEANINGFUL DATA C* : C ENDIF C* PROCESS ALL REMAINING VARIABLES C* C* BEFORE RETURNING, SET OUTPUT VALUE FOR FIRST C* PARAMETER AND SET THE INDICATOR TO A NON-NEGATIV C* VALUE SO THAT THE DATA IS RETURNED TO THE CALLING C* PROGRAM C* C EVAL INOUTP2 = 20.5 C EVAL NULLARRAY(2) = 0 C* C* INDICATE THAT THE SECOND PARAMETER IS TO CONTAIN C* THE NULL VALUE UPON RETURN. THERE IS NO POINT C* IN SETTING THE VALUE IN INOUTP SINCE IT WONT BE C* PASSED BACK TO THE CALLER. C EVAL NULLARRAY(1) = -5 C RETURN ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ End of PROGRAM PROC1 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SQL programming
149
In certain cases, calling the UDF directly from the database engine instead of from your application can have a considerable performance advantage. You will notice this advantage in cases where the function may be used in the qualification of data for further processing. These cases occur when the function is used in row selection processing. Consider a simple scenario where you want to process some data. You can meet some selection criteria which can be expressed as a function SELECTION_CRITERIA(). Your application can issue the following select statement:
SELECT A, B, C FROM T
When it receives each row, it runs the programs SELECTION_CRITERIA function against the data to decide if it is interested in processing the data further. Here, every row of table T must be passed back to the application. But, if SELECTION_CRITERIA() is implemented as a UDF, your application can issue the following statement:
SELECT C FROM T WHERE SELECTION_CRITERIA(A,B)=1
In this case, only the rows and one column of interest are passed across the interface between the application and the database. Another case where a UDF can offer a performance benefit is when you deal with large objects (LOBs). Suppose that you have a function that extracts some information from a value of a LOB. You can perform this extraction right on the database server and pass only the extracted value back to the application. This is more efficient than passing the entire LOB value back to the application and then performing the extraction. The performance value of packaging this function as a UDF can be enormous, depending on the particular situation. Related concepts User-defined functions on page 10 A user-defined function is a program that can be called like any built-in functions.
UDF concepts
A user-defined function (UDF) is a function that is defined to the DB2 database system through the CREATE FUNCTION statement and that can be referenced in SQL statements. A UDF can be an external function or an SQL function.
Types of function
There are several types of functions: v Built-in. These are functions provided by and shipped with the database. SUBSTR() is an example. v System-generated. These are functions implicitly generated by the database engine when a DISTINCT TYPE is created. These functions provide casting operations between the DISTINCT TYPE and its base type. v User-defined. These are functions created by users and registered to the database. In addition, each function can be further classified as a scalar, column, or table function. A scalar function returns a single value answer each time it is called. For example, the built-in function SUBSTR() is a scalar function, as are many built-in functions. System-generated functions are always scalar functions. Scalar UDFs can either be external (coded in a programming language such as C), written in SQL, or sourced (using the implementation of an existing function). A column function receives a set of like values (a column of data) and returns a single value answer from this set of values. These are also called aggregating functions in DB2. Some built-in functions are column functions. An example of a column function is the built-in function AVG(). An external UDF cannot be defined as a column function. However, a sourced UDF is defined to be a column function if it is sourced
150
on one of the built-in column functions. The latter is useful for distinct types. For example, if a distinct type SHOESIZE exists that is defined with base type INTEGER, you can define a UDF, AVG(SHOESIZE), as a column function sourced on the existing built-in column function, AVG(INTEGER). A table function returns a table to the SQL statement that references it. It must be referenced in the FROM clause of a SELECT. A table function can be used to apply SQL language processing power to data that is not DB2 data, or to convert such data into a DB2 table. It can, for example, take a file and convert it to a table, sample data from the World Wide Web and tabularize it, or access a Lotus Notes database and return information about mail messages, such as the date, sender, and the text of the message. This information can be joined with other tables in the database. A table function can be defined as a external function or an SQL function; it cannot be defined as a sourced function.
However, you may also omit the <schema-name>., in which case, DB2 must determine the function to which you are referring. For example:
SNOWBLOWER_SIZE FOO SUBSTR FLOOR
Path
The concept of path is central to DB2s resolution of unqualified references that occur when schema-name is not specified. The path is an ordered list of schema names that is used for resolving unqualified references to UDFs and UDTs. In cases where a function reference matches a function in more than one schema in the path, the order of the schemas in the path is used to resolve this match. The path is established by means of the SQLPATH option on the precompile commands for static SQL. The path is set by the SET PATH statement for dynamic SQL. When the first SQL statement that runs in an activation group runs with SQL naming, the path has the following default value:
"QSYS","QSYS2","<ID>"
This applies to both static and dynamic SQL, where <ID> represents the current statement authorization ID. When the first SQL statement in an activation group runs with system naming, the default path is *LIBL.
Function resolution
It is the function resolution algorithm that takes into account the facts of overloading and function path to choose the best fit for every function reference, whether it is a qualified or an unqualified reference. All functions, even built-in functions, are processed through the function selection algorithm. The function resolution algorithm does not take into account the type of a function. So a table function may be
SQL programming
151
resolved to as the best fit function, even though the usage of the reference requires an scalar function, or vice versa.
152
This example shows a table function that returns data based on a date.
CREATE FUNCTION PROJFUNC(indate DATE) RETURNS TABLE (PROJNO CHAR(6), ACTNO SMALLINT, ACTSTAFF DECIMAL(5,2), ACSTDATE DATE, ACENDATE DATE) LANGUAGE SQL BEGIN RETURN SELECT * FROM PROJACT WHERE ACSTDATE<=indate; END
SQL table functions are required to have one and only one RETURN statement.
SQL programming
153
In this example, the RETURNS NULL ON NULL INPUT is specified since you want the result to be NULL if either argument is NULL. As there is no reason why EXPON cannot be parallel, the ALLOW PARALLEL value is specified. Example: String search: Suppose that you write a user-defined function (UDF) to look for a given string, passed as an argument, within a given character large object (CLOB) value that is also passed as an argument. The UDF returns the position of the string within the CLOB if it finds the string, or zero if it does not. The C program was written to return a FLOAT result. Suppose you know that when it is used in SQL, it should always return an INTEGER. You can create the following function:
CREATE FUNCTION FINDSTRING (CLOB(500K), VARCHAR(200)) RETURNS INTEGER CAST FROM FLOAT SPECIFIC FINDSTRING EXTERNAL NAME MYLIB/MYPGM(FINDSTR) LANGUAGE C PARAMETER STYLE DB2SQL NO SQL DETERMINISTIC NO EXTERNAL ACTION RETURNS NULL ON NULL INPUT
Note that a CAST FROM clause is used to specify that the UDF program really returns a FLOAT value, but you want to cast this to INTEGER before returning the value to the SQL statement which used the UDF. Also, you want to provide your own specific name for the function. Because the UDF was not written to handle NULL values, you use the RETURNS NULL ON NULL INPUT. Example: BLOB string search: Suppose that you want the FINDSTRING function to work on binary large objects (BLOBs) as well as on character large objects (CLOBs). To do this, you define another FINDSTRING function that takes BLOB as the first parameter.
CREATE FUNCTION FINDSTRING (BLOB(500K), VARCHAR(200)) RETURNS INTEGER CAST FROM FLOAT SPECIFIC FINDSTRING_BLOB EXTERNAL NAME MYLIB/MYPGM(FINDSTR) LANGUAGE C PARAMETER STYLE DB2SQL NO SQL DETERMINISTIC NO EXTERNAL ACTION RETURNS NULL ON NULL INPUT
This example illustrates overloading of the UDF name and shows that multiple UDFs can share the same program. Note that although a BLOB cannot be assigned to a CLOB, the same source code can be used. There is no programming problem in the above example as the interface for BLOB and CLOB between DB2 and the UDF program is the same: length followed by data. Example: String search over a user-defined type (UDT): Suppose that you are satisfied with the FINDSTRING function from the binary large object (BLOB) string search, but now you want to define a distinct type BOAT with source type BLOB. You also want FINDSTRING to operate on values having data type BOAT, so you create another FINDSTRING function. This function is sourced on the FINDSTRING which operates on BLOB values. Note the further overloading of FINDSTRING in this example:
154
CREATE FUNCTION FINDSTRING (BOAT, VARCHAR(200)) RETURNS INT SPECIFIC "slick_fboat" SOURCE SPECIFIC FINDSTRING_BLOB
Note that this FINDSTRING function has a different signature from the FINDSTRING functions in Example: BLOB string search on page 154, so there is no problem overloading the name. Because you are using the SOURCE clause, you cannot use the EXTERNAL NAME clause or any of the related keywords specifying function attributes. These attributes are taken from the source function. Finally, observe that in identifying the source function you are using the specific function name explicitly provided in Example: BLOB string search on page 154. Because this is an unqualified reference, the schema in which this source function resides must be in the function path, or the reference will not be resolved. Related reference Example: BLOB string search on page 154 Suppose that you want the FINDSTRING function to work on binary large objects (BLOBs) as well as on character large objects (CLOBs). To do this, you define another FINDSTRING function that takes BLOB as the first parameter. Example: AVG over a user-defined type (UDT): This example implements the AVG column function over the CANADIAN_DOLLAR distinct type. Strong typing prevents you from using the built-in AVG function on a distinct type. It turns out that the source type for CANADIAN_DOLLAR was DECIMAL, and so you implement the AVG by sourcing it on the AVG(DECIMAL) built-in function.
CREATE FUNCTION AVG (CANADIAN_DOLLAR) RETURNS CANADIAN_DOLLAR SOURCE "QSYS2".AVG(DECIMAL(9,2))
Note that in the SOURCE clause you have qualified the function name, just in case there might be some other AVG function lurking in your SQL path. Example: Counting: Your simple counting function returns a 1 the first time and increments the result by one each time it is called. This function takes no SQL arguments. By definition, it is a NOT DETERMINISTIC function because its answer varies from call to call. It uses the SCRATCHPAD to save the last value returned. Each time it is called, the function increments this value and returns it.
CREATE FUNCTION COUNTER () RETURNS INT EXTERNAL NAME MYLIB/MYFUNCS(CTR) LANGUAGE C PARAMETER STYLE DB2SQL NO SQL NOT DETERMINISTIC NOT FENCED SCRATCHPAD 4 DISALLOW PARALLEL
Note that no parameter definitions are provided, just empty parentheses. The above function specifies SCRATCHPAD and uses the default specification of NO FINAL CALL. In this case, the size of the scratchpad is set to only 4 bytes, which is sufficient for a counter. Since the COUNTER function requires that a single scratchpad be used to operate properly, DISALLOW PARALLEL is added to prevent DB2 from operating it in parallel.
SQL programming
155
Example: Table function returning document IDs: Suppose that you write a table function that returns a row consisting of a single document identifier column for each known document that matches a given subject area (the first parameter) and contains the given string (second parameter). This user-defined function (UDF) quickly identifies the documents:
CREATE FUNCTION DOCMATCH (VARCHAR(30), VARCHAR(255)) RETURNS TABLE (DOC_ID CHAR(16)) EXTERNAL NAME DOCFUNCS/UDFMATCH(udfmatch) LANGUAGE C PARAMETER STYLE DB2SQL NO SQL DETERMINISTIC NO EXTERNAL ACTION NOT FENCED SCRATCHPAD NO FINAL CALL DISALLOW PARALLEL CARDINALITY 20
Within the context of a single session it will always return the same table, and therefore it is defined as DETERMINISTIC. The RETURNS clause defines the output from DOCMATCH, including the column name DOC_ID. FINAL CALL does not need to be specified for this table function. The DISALLOW PARALLEL keyword is required since table functions cannot operate in parallel. Although the size of the output from DOCMATCH can be a large table, CARDINALITY 20 is a representative value, and is specified to help the optimizer make good decisions. Typically, this table function is used in a join with the table containing the document text, as follows:
SELECT T.AUTHOR, T.DOCTEXT FROM DOCS AS T, TABLE(DOCMATCH(MATHEMATICS, ZORNS LEMMA)) AS F WHERE T.DOCID = F.DOC_ID
Note the special syntax (TABLE keyword) for specifying a table function in a FROM clause. In this invocation, the DOCMATCH() table function returns a row containing the single column DOC_ID for each MATHEMATICS document referencing ZORNS LEMMA. These DOC_ID values are joined to the master document table, retrieving the authors name and document text. Passing arguments from DB2 to external functions: DB2 provides storage for all parameters that are passed to a user-defined function (UDF). Therefore, parameters are passed to an external function by address. This is the normal parameter passing method for programs. For service programs, ensure that the parameters are defined correctly in the function code. When defining and using the parameters in the UDF, care should be taken to ensure that no more storage is referenced for a given parameter than is defined for that parameter. The parameters are all stored in the same space and exceeding a given parameters storage space can overwrite another parameters value. This, in turn, can cause the function to see invalid input data or cause the value returned to the database to be invalid. There are several supported parameter styles available to external UDFs. For the most part, the styles differ in how many parameters are passed to the external program or service program. Parameter style SQL:
156
The SQL parameter style conforms to the industry standard SQL. This parameter style can be used only with scalar user-defined functions (UDFs). With parameter style SQL, the parameters are passed into the external program as follows (in the order specified):
SQL-result SQL-result-ind SQL-state
SQL-argument
SQL-argument-ind
SQL-argument This argument is set by DB2 before calling the UDF. This value repeats n times, where n is the number of arguments specified in the function reference. The value of each of these arguments is taken from the expression specified in the function invocation. It is expressed in the data type of the defined parameter in the create function statement. Note: These parameters are treated as input only; any changes to the parameter values made by the UDF are ignored by DB2. SQL-result This argument is set by the UDF before returning to DB2. The database provides the storage for the return value. Since the parameter is passed by address, the address is of the storage where the return value should be placed. The database provides as much storage as needed for the return value as defined on the CREATE FUNCTION statement. If the CAST FROM clause is used in the CREATE FUNCTION statement, DB2 assumes the UDF returns the value as defined in the CAST FROM clause, otherwise DB2 assumes the UDF returns the value as defined in the RETURNS clause. SQL-argument-ind This argument is set by DB2 before calling the UDF. It can be used by the UDF to determine if the corresponding SQL-argument is null or not. The nth SQL-argument-ind corresponds to the nth SQL-argument, described previously. Each indicator is defined as a two-byte signed integer. It is set to one of the following values: 0 -1 The argument is present and not null. The argument is null.
If the function is defined with RETURNS NULL ON NULL INPUT, the UDF does not need to check for a null value. However, if it is defined with CALLS ON NULL INPUT, any argument can be NULL and the UDF should check for null input. Note: these parameters are treated as input only; any changes to the parameter values made by the UDF are ignored by DB2. SQL-result-ind This argument is set by the UDF before returning to DB2. The database provides the storage for the return value. The argument is defined as a two-byte signed integer. If set to a negative value, the database interprets the result of the function as null. If set to zero or a positive value, the database uses the value returned in SQL-result. The database provides the storage for the return value indicator. Since the parameter is passed by address, the address is of the storage where the indicator value should be placed. SQL-state This argument is a CHAR(5) value that represents the SQLSTATE. This parameter is passed in from the database set to 00000 and can be set by the function as a result state for the function. While normally the SQLSTATE is not set by the function, it can be used to signal an error or warning to the database as follows:
SQL programming
157
01Hxx The function code detected a warning situation. This results in an SQL warning, Here xx may be one of several possible strings. 38xxx The function code detected an error situation. It results in a SQL error. Here xxx may be one of several possible strings.
function-name This argument is set by DB2 before calling the UDF. It is a VARCHAR(139) value that contains the name of the function on whose behalf the function code is being called. The form of the function name that is passed is:
<schema-name>.<function-name>
This parameter is useful when the function code is being used by multiple UDF definitions so that the code can distinguish which definition is being called. Note: This parameter is treated as input only; any changes to the parameter value made by the UDF are ignored by DB2. specific-name This argument is set by DB2 before calling the UDF. It is a VARCHAR(128) value that contains the specific name of the function on whose behalf the function code is being called. Like function-name, this parameter is useful when the function code is being used by multiple UDF definitions so that the code can distinguish which definition is being called. Note: This parameter is treated as input only; any changes to the parameter value made by the UDF are ignored by DB2. diagnostic-message This argument is set by DB2 before calling the UDF. It is a VARCHAR(70) value that can be used by the UDF to send message text back when an SQLSTATE warning or error is signaled by the UDF. It is initialized by the database on input to the UDF and may be set by the UDF with descriptive information. Message text is ignored by DB2 unless the SQL-state parameter is set by the UDF. Related reference SQL messages and codes Parameter style DB2SQL: With the DB2SQL parameter style, the same parameters and the same order of parameters are passed to an external program or a service program as with the SQL parameter style. However, DB2SQL allows additional optional parameters to be passed as well. If more than one of the optional parameters below is specified in the UDF definition, they are passed to the UDF in the order defined below. Refer to parameter style SQL for the common parameters. This parameter style can be used for both scalar and table UDFs. For scalar functions:
SQL-result SQL-result-ind SQL-state
SQL-argument
SQL-argument-ind
158
SQL-result
SQL-result-ind
SQL-state
SQL-argument
scratchpad This argument is set by DB2 before calling the UDF. It is only present if the CREATE FUNCTION statement for the UDF specified the SCRATCHPAD keyword. This argument is a structure with the following elements: v An INTEGER containing the length of the scratchpad. v The actual scratchpad, initialized to all binary 0s by DB2 before the first call to the UDF. The scratchpad can be used by the UDF either as working storage or as persistent storage, since it is maintained across UDF invocations. For table functions, the scratchpad is initialized as above before the FIRST call to the UDF if FINAL CALL is specified on the CREATE FUNCTION. After this call, the scratchpad content is totally under control of the table function. DB2 does not examine or change the content of the scratchpad thereafter. The scratchpad is passed to the function on each invocation. The function can be re-entrant, and DB2 preserves its state information in the scratchpad. If NO FINAL CALL was specified or defaulted for a table function, then the scratchpad is initialized as above for each OPEN call, and the scratchpad content is completely under control of the table function between OPEN calls. This can be very important for a table function used in a join or subquery. If it is necessary to maintain the content of the scratchpad across OPEN calls, then FINAL CALL must be specified in your CREATE FUNCTION statement. With FINAL CALL specified, in addition to the normal OPEN, FETCH, and CLOSE calls, the table function will also receive FIRST and FINAL calls, for the purpose of scratchpad maintenance and resource release. call-type This argument is set by DB2 before calling the UDF. For scalar functions, it is only present if the CREATE FUNCTION statement for the UDF specified the FINAL CALL keyword. However, for table functions it is always present. It follows the scratchpad argument; or the diagnostic-message argument if the scratchpad argument is not present. This argument takes the form of an INTEGER value. For scalar functions: -1 0 1 This is the first call to the UDF for this statement. A first call is a normal call in that all SQL argument values are passed. This is a normal call. (All the normal input argument values are passed). This is a final call. No SQL-argument or SQL-argument-ind values are passed. A UDF should not return any answer using the SQL-result, SQL-result-ind arguments, SQL-state, or diagnostic-message arguments. These arguments are ignored by the system when returned from the UDF.
For table functions: -2 This is the first call to the UDF for this statement. A first call is a normal call in that all SQL argument values are passed.
SQL programming
159
-1
This is the open call to the UDF for this statement. The scratchpad is initialized if NO FINAL CALL is specified, but not necessarily otherwise. All SQL argument values are passed. This is a fetch call. DB2 expects the table function to return either a row comprising the set of return values, or an end-of-table condition indicated by SQLSTATE value 02000. This is a close call. This call balances the OPEN call, and can be used to perform any external CLOSE processing and resource release. This is a final call. No SQL-argument or SQL-argument-ind values are passed. A UDF should not return any answer using the SQL-result, SQL-result-ind arguments, SQL-state, or diagnostic-message arguments. These arguments are ignored by the system when returned from the UDF.
0 1 2
dbinfo
This argument is set by DB2 before calling the UDF. It is only present if the CREATE FUNCTION statement for the UDF specifies the DBINFO keyword. The argument is a structure whose definition is contained in the sqludf include.
Parameter style GENERAL: With the GENERAL parameter style, the parameters are passed to an external service program just as they are specified in the CREATE FUNCTION statement. This parameter style can be used only with scalar user-defined functions (UDFs). The format is:
SQL-argument This argument is set by DB2 before calling the UDF. This value repeats n times, where n is the number of arguments specified in the function reference. The value of each of these arguments is taken from the expression specified in the function invocation. It is expressed in the data type of the defined parameter in the CREATE FUNCTION statement. Note: These parameters are treated as input only; any changes to the parameter values made by the UDF are ignored by DB2. SQL-result This value is returned by the UDF. DB2 copies the value into database storage. In order to return the value correctly, the function code must be a value-returning function. The database copies only as much of the value as defined for the return value as specified on the CREATE FUNCTION statement. If the CAST FROM clause is used in the CREATE FUNCTION statement, DB2 assumes the UDF returns the value as defined in the CAST FROM clause, otherwise DB2 assumes the UDF returns the value as defined in the RETURNS clause. Because of the requirement that the function code be a value-returning function, any function code used for parameter style GENERAL must be created into a service program. Parameter style GENERAL WITH NULLS: The GENERAL WITH NULLS parameter style can be used only with scalar user-defined functions (UDFs). With this parameter style, the parameters are passed into the service program as follows (in the order specified):
160
SQL-result-ind )
SQL-argument This argument is set by DB2 before calling the UDF. This value repeats n times, where n is the number of arguments specified in the function reference. The value of each of these arguments is taken from the expression specified in the function invocation. It is expressed in the data type of the defined parameter in the CREATE FUNCTION statement. Note: These parameters are treated as input only; any changes to the parameter values made by the UDF are ignored by DB2. SQL-argument-ind-array This argument is set by DB2 before calling the UDF. It can be used by the UDF to determine if one or more SQL-arguments are null or not. It is an array of two-byte signed integers (indicators). The nth array argument corresponds corresponds to the nth SQL-argument. Each array entry is set to one of the following values: 0 -1 The argument is present and not null. The argument is null.
The UDF should check for null input. Note: This parameter is treated as input only; any changes to the parameter value made by the UDF is ignored by DB2. SQL-result-ind This argument is set by the UDF before returning to DB2. The database provides the storage for the return value. The argument is defined as a two-byte signed integer. If set to a negative value, the database interprets the result of the function as null. If set to zero or a positive value, the database uses the value returned in SQL-result. The database provides the storage for the return value indicator. Since the parameter is passed by address, the address is of the storage where the indicator value should be placed. SQL-result This value is returned by the UDF. DB2 copies the value into database storage. In order to return the value correctly, the function code must be a value-returning function. The database copies only as much of the value as defined for the return value as specified on the CREATE FUNCTION statement. If the CAST FROM clause is used in the CREATE FUNCTION statement, DB2 assumes the UDF returns the value as defined in the CAST FROM clause, otherwise DB2 assumes the UDF returns the value as defined in the RETURNS clause. Because of the requirement that the function code be a value-returning function, any function code used for parameter style GENERAL WITH NULLS must be created into a service program. Notes: 1. The external name specified on the CREATE FUNCTION statement can be specified either with or without single quotation marks. If the name is not quoted, it is uppercased before it is stored; if it is quoted, it is stored as specified. This becomes important when naming the actual program, as the database searches for the program that has a name that exactly matches the name stored with the function definition. For example, if a function was created as:
CREATE FUNCTION X(INT) RETURNS INT LANGUAGE C EXTERNAL NAME MYLIB/MYPGM(MYENTRY)
SQL programming
161
the database will not find the entry because it is in lowercase myentry and the database was instructed to look for uppercase MYENTRY. 2. For service programs with C++ modules, make sure in the C++ source code to precede the program function definition with extern C. Otherwise, the C++ compiler will perform name mangling of the functions name and the database will not find it. Parameter style DB2GENERAL: The DB2GENERAL parameter style is used by Java user-defined functions (UDFs). Related concepts Java SQL routines Parameter style Java: The Java parameter style is the style specified by the SQLJ Part 1: SQL Routines standard. Related concepts Java SQL routines Table function considerations: An external table function is a user-defined function (UDF) that delivers a table to the SQL statement in which it is referenced. A table function reference is valid only in a FROM clause of a SELECT statement. When using table functions, observe the following: v Even though a table function delivers a table, the physical interface between DB2 and the UDF is one-row-at-a-time. There are five types of calls made to a table function: OPEN, FETCH, CLOSE, FIRST, and FINAL. The existence of FIRST and FINAL calls depends on how you define the UDF. The same call-type mechanism that can be used for scalar functions is used to distinguish these calls. v The standard interface used between DB2 and user-defined scalar functions is extended to accommodate table functions. The SQL-result argument repeats for table functions; each instance corresponding to a column to be returned as defined in the RETURNS TABLE clause of the CREATE FUNCTION statement. The SQL-result-ind argument likewise repeats, each instance related to the corresponding SQL-result instance. v Not every result column defined in the RETURNS clause of the CREATE FUNCTION statement for the table function has to be returned. The DBINFO keyword of CREATE FUNCTION, and corresponding dbinfo argument enable the optimization that only those columns needed for a particular table function reference need be returned. v The individual column values returned conform in format to the values returned by scalar functions. v The CREATE FUNCTION statement for a table function has a CARDINALITY n specification. This specification enables the definer to inform the DB2 optimizer of the approximate size of the result so that the optimizer can make better decisions when the function is referenced. Regardless of what has been specified as the CARDINALITY of a table function, exercise caution against writing a function with infinite cardinality; that is, a function that always returns a row on a FETCH call. DB2 expects the end-of-table condition, as a catalyst within its query processing. So a table function that never returns the end-of-table condition (SQL-state value 02000) will cause an infinite processing loop. Error processing for UDFs:
162
When an error occurs in processing a user-defined function (UDF), the system follows a specified model to handle the error. Table function error processing The error processing model for table function calls is as follows: 1. If FIRST call fails, no further calls are made. 2. If FIRST call succeeds, the nested OPEN, FETCH, and CLOSE calls are made, and the FINAL call is always made. 3. If OPEN call fails, no FETCH or CLOSE call is made. 4. If OPEN call succeeds, then FETCH and CLOSE calls are made. 5. If a FETCH call fails, no further FETCH calls are made, but the CLOSE call is made. Note: This model describes the ordinary error processing for table UDFs. In the event of a system failure or communication problem, a call indicated by the error processing model may not be made. Scalar function error processing The error processing model for scalar UDFs, which are defined with the FINAL CALL specification, is as follows: 1. If FIRST call fails, no further calls are made. 2. If FIRST call succeeds, then further NORMAL calls are made as warranted by the processing of the statement, and a FINAL call is always made. 3. If NORMAL call fails, no further NORMAL calls are made, but the FINAL call is made (if you have specified FINAL CALL). This means that if an error is returned on a FIRST call, the UDF must clean up before returning, because no FINAL call will be made. Note: This model describes the ordinary error processing for scalar UDFs. In the event of a system failure or communication problem, a call indicated by the error processing model may not be made. Threads considerations: A user-defined function (UDF) that is defined as FENCED runs in the same job as the SQL statement that calls the function. However, the UDF runs in a system thread, separate from the thread that is running the SQL statement. Because the UDF runs in the same job as the SQL statement, it shares much of the same environment as the SQL statement. However, because it runs under a separate thread, the following threads considerations apply: v The UDF will conflict with thread level resources held by the SQL statements thread. Primarily, these are the table resources discussed above. v UDFs do not inherit any program adopted authority that may have been active at the time the SQL statement was called. UDF authority comes from either the authority associated with the UDF program itself or the authority of the user running the SQL statement. v The UDF cannot perform any operation that is blocked from being run in a secondary thread. v The UDF program must be created such that it either runs under a named activation group or in the activation group of its caller (ACTGRP parameter). Programs that specify ACTGRP(*NEW) will not be allowed to run as UDFs. Related reference Multithreaded applications Fenced or unfenced considerations on page 164 When you create a user-defined function (UDF), consider whether to make the UDF an unfenced UDF.
SQL programming
163
Parallel processing: A user-defined function (UDF) can be defined to allow parallel processing. This means that the same UDF program can be running in multiple threads at the same time. Therefore, if ALLOW PARALLEL is specified for the UDF, ensure that it is thread safe. User-defined table functions cannot run in parallel; therefore, DISALLOW PARALLEL must be specified when creating the function Related reference Multithreaded applications Fenced or unfenced considerations: When you create a user-defined function (UDF), consider whether to make the UDF an unfenced UDF. By default, UDFs are created as fenced UDFs. Fenced indicates that the database should run the UDF in a separate thread. For complex UDFs, this separation is meaningful as it will avoid potential problems such as generating unique SQL cursor names. Not having to be concerned about resource conflicts is one reason to stick with the default and create the UDF as a fenced UDF. A UDF created with the NOT FENCED option indicates to the database that the user is requesting that the UDF can run within the same thread that initiated the UDF. Unfenced is a suggestion to the database, which can still decide to run the UDF in the same manner as a fenced UDF.
CREATE FUNCTION QGPL.FENCED (parameter1 INTEGER) RETURNS INTEGER LANGUAGE SQL BEGIN RETURN parameter1 * 3; END; CREATE FUNCTION QGPL.UNFENCED1 (parameter1 INTEGER) RETURNS INTEGER LANGUAGE SQL NOT FENCED -- Build the UDF to request faster execution via the NOT FENCED option BEGIN RETURN parameter1 * 3; END;
Related reference Threads considerations on page 163 A user-defined function (UDF) that is defined as FENCED runs in the same job as the SQL statement that calls the function. However, the UDF runs in a system thread, separate from the thread that is running the SQL statement. Save and restore considerations: When an external function associated with an ILE external program or service program is created, an attempt is made to save the attributes of the function in the associated program or service program object. If the *PGM or *SRVPGM object is saved and then restored to this or another system, the catalogs are automatically updated with those attributes. If the functions attribute cannot be saved, then the catalogs will not be automatically updated and the user must create the external function on the new system. The attributes can be saved for external functions subject to the following restrictions: v The external program library must not be QSYS or QSYS2. v The external program must exist when the CREATE FUNCTION statement is issued. v The external program must be an ILE *PGM or *SRVPGM object. v The external program or service program must contain at least one SQL statement.
164
If the program object cannot be updated, the function will still be created.
Note: By using the code examples, you agree to the terms of the Code license and disclaimer information on page 300. The following examples show how to define the UDF in several different ways. Using an SQL function The CREATE FUNCTION statement:
CREATE FUNCTION SQUARE( inval INT) RETURNS INT LANGUAGE SQL SET OPTION DBGVIEW=*SOURCE BEGIN RETURN(inval*inval); END
This creates an SQL function that you can debug. Using an external function, parameter style SQL The CREATE FUNCTION statement:
CREATE FUNCTION SQUARE(INT) RETURNS INT CAST FROM FLOAT LANGUAGE C EXTERNAL NAME MYLIB/MATH(SQUARE) DETERMINISTIC NO SQL NO EXTERNAL ACTION PARAMETER STYLE SQL ALLOW PARALLEL
The code:
void SQUARE(int *inval, double *outval, short *inind, short *outind, char *sqlstate, char *funcname, char *specname, char *msgtext) { if (*inind<0) *outind=-1; else { *outval=*inval; *outval=(*outval)*(*outval);
SQL programming
165
*outind=0; } return; }
Using an external function, parameter style GENERAL The CREATE FUNCTION statement:
CREATE FUNCTION SQUARE(INT) RETURNS INT CAST FROM FLOAT LANGUAGE C EXTERNAL NAME MYLIB/MATH(SQUARE) DETERMINISTIC NO SQL NO EXTERNAL ACTION PARAMETER STYLE GENERAL ALLOW PARALLEL
The code:
double SQUARE(int *inval) { double outval; outval=*inval; outval=outval*outval; return(outval); }
Example: Counter: Suppose that you want to number the rows in a SELECT statement. So you write a user-defined function (UDF) that increments and returns a counter. Note: By using the code examples, you agree to the terms of the Code license and disclaimer information on page 300. This example uses an external function with DB2 SQL parameter style and a scratchpad.
CREATE FUNCTION COUNTER() RETURNS INT SCRATCHPAD NOT DETERMINISTIC NO SQL NO EXTERNAL ACTION LANGUAGE C PARAMETER STYLE DB2SQL EXTERNAL NAME MYLIB/MATH(ctr) DISALLOW PARALLEL /* structure scr defines the passed scratchpad for the function "ctr" */ struct scr { long len; long countr;
166
char not_used[92]; }; void ctr ( long *out, short *outnull, char *sqlstate, char *funcname, char *specname, char *mesgtext, struct scr *scratchptr) { *out = ++scratchptr->countr; *outnull = 0; return; } /* end of UDF : ctr */ /* /* /* /* /* /* /* output answer (counter) */ output NULL indicator */ SQL STATE */ function name */ specific function name */ message text insert */ scratch pad */
For this UDF, observe that: v It has no input SQL arguments defined, but returns a value. v It appends the scratchpad input argument after the four standard trailing arguments, namely SQL-state, function-name, specific-name, and message-text. v It includes a structure definition to map the scratchpad which is passed. v No input parameters are defined. This agrees with the code. v SCRATCHPAD is coded, causing DB2 to allocate, properly initialize and pass the scratchpad argument. v You have specified it to be NOT DETERMINISTIC, because it depends on more than the SQL input arguments, (none in this case). v You have correctly specified DISALLOW PARALLEL, because correct functioning of the UDF depends on a single scratchpad. Example: Weather table function: Suppose that you write a table function that returns weather information for various cities in the United States. Note: By using the code examples, you agree to the terms of the Code license and disclaimer information on page 300. The weather date for these cities is read in from an external file, as indicated in the comments contained in the example program. The data includes the name of a city followed by its weather information. This pattern is repeated for the other cities.
#include #include #include #include #define #define #define #define #define <stdlib.h> <string.h> <stdio.h> <sqludf.h> /* for use in compiling User Defined Function */ SQL_NOTNULL 0 /* Nulls Allowed - Value is not Null */ SQL_ISNULL -1 /* Nulls Allowed - Value is Null */ SQL_TYP_VARCHAR 448 SQL_TYP_INTEGER 496 SQL_TYP_FLOAT 480
/* Short and long city name structure */ typedef struct { char * city_short ; char * city_long ; } city_area ; /* Scratchpad data */ (See note 1)
SQL programming
167
/* Preserve information from one function call to the next call */ typedef struct { /* FILE * file_ptr; if you use weather data text file */ int file_pos ; /* if you use a weather data buffer */ } scratch_area ; /* Field descriptor structure */ typedef struct { char fld_field[31] ; /* Field int fld_ind ; /* Field null indicator int fld_type ; /* Field int fld_length ; /* Field length in the weather int fld_offset ; /* Field offset in the weather } fld_desc ; /* Short and long city name data */ city_area cities[] = { { "alb", "Albany, NY" }, { "atl", "Atlanta, GA" }, . . . { "wbc", "Washington DC, DC" }, /* You may want to add more cities here */ /* Do not forget a null termination */ { ( char * ) 0, ( char * ) 0 } } ; /* Field descriptor data */ fld_desc fields[] = { { "", SQL_ISNULL, SQL_TYP_VARCHAR, 30, 0 }, /* city { "", SQL_ISNULL, SQL_TYP_INTEGER, 3, 2 }, /* temp_in_f { "", SQL_ISNULL, SQL_TYP_INTEGER, 3, 7 }, /* humidity { "", SQL_ISNULL, SQL_TYP_VARCHAR, 5, 13 }, /* wind { "", SQL_ISNULL, SQL_TYP_INTEGER, 3, 19 }, /* wind_velocity { "", SQL_ISNULL, SQL_TYP_FLOAT, 5, 24 }, /* barometer { "", SQL_ISNULL, SQL_TYP_VARCHAR, 25, 30 }, /* forecast /* You may want to add more fields here */ /* Do not forget a null termination */ { ( char ) 0, 0, 0, 0, 0 } } ; /* Following is the weather data buffer for this example. You /* may want to keep the weather data in a separate text file. /* Uncomment the following fopen() statement. Note that you /* need to specify the full path name for this file. char * weather_data[] = { "alb.forecast", " 34 28% wnw 3 30.53 clear", "atl.forecast", " 46 89% east 11 30.03 fog", . . . "wbc.forecast", " 38 96% ene 16 30.31 light rain", /* You may want to add more weather data here */ /* Do not forget a null termination */ ( char * ) 0 } ; #ifdef __cplusplus extern "C" #endif */ */ */ */
*/ */ */ */ */
*/ */ */ */ */ */ */
168
/* This is a subroutine. */ /* Find a full city name using a short name */ int get_name( char * short_name, char * long_name ) { int name_pos = 0 ; while ( cities[name_pos].city_short != ( char * ) 0 ) { if (strcmp(short_name, cities[name_pos].city_short) == 0) { strcpy( long_name, cities[name_pos].city_long ) ; /* A full city name found */ return( 0 ) ; } name_pos++ ; } /* can not find such city in the city data */ strcpy( long_name, "Unknown City" ) ; return( -1 ) ; } #ifdef __cplusplus extern "C" #endif /* This is a subroutine. */ /* Clean all field data and field null indicator data */ int clean_fields( int field_pos ) { while (fields[field_pos].fld_length !=0 ) { memset( fields[field_pos].fld_field, \0, 31 ) ; fields[field_pos].fld_ind = SQL_ISNULL ; field_pos++ ; } return( 0 ) ; } #ifdef __cplusplus extern "C" #endif /* This is a subroutine. */ /* Fills all field data and field null indicator data ... */ /* ... from text weather data */ int get_value( char * value, int field_pos ) { fld_desc * field ; char field_buf[31] ; double * double_ptr ; int * int_ptr, buf_pos ; while ( fields[field_pos].fld_length != 0 ) { field = &fields[field_pos] ; memset( field_buf, \0, 31 ) ; memcpy( field_buf, ( value + field->fld_offset ), field->fld_length ) ; buf_pos = field->fld_length ; while ( ( buf_pos > 0 ) && ( field_buf[buf_pos] == ) ) field_buf[buf_pos--] = \0 ; buf_pos = 0 ; while ( ( buf_pos < field->fld_length ) && ( field_buf[buf_pos] == ) ) buf_pos++ ; if ( strlen( ( char * ) ( field_buf + buf_pos ) ) > 0 || strcmp( ( char * ) ( field_buf + buf_pos ), "n/a") != 0 ) { field->fld_ind = SQL_NOTNULL ;
SQL programming
169
/* Text to SQL type conversion */ switch( field->fld_type ) { case SQL_TYP_VARCHAR: strcpy( field->fld_field, ( char * ) ( field_buf + buf_pos ) ) ; break ; case SQL_TYP_INTEGER: int_ptr = ( int * ) field->fld_field ; *int_ptr = atoi( ( char * ) ( field_buf + buf_pos ) ) ; break ; case SQL_TYP_FLOAT: double_ptr = ( double * ) field->fld_field ; *double_ptr = atof( ( char * ) ( field_buf + buf_pos ) ) ; break ; /* You may want to add more text to SQL type conversion here */ } } field_pos++ ; } return( 0 ) ; } #ifdef __cplusplus extern "C" #endif void SQL_API_FN weather( /* Return row fields */ SQLUDF_VARCHAR * city, SQLUDF_INTEGER * temp_in_f, SQLUDF_INTEGER * humidity, SQLUDF_VARCHAR * wind, SQLUDF_INTEGER * wind_velocity, SQLUDF_DOUBLE * barometer, SQLUDF_VARCHAR * forecast, /* You may want to add more fields here */ /* Return row field null indicators */ SQLUDF_NULLIND * city_ind, SQLUDF_NULLIND * temp_in_f_ind, SQLUDF_NULLIND * humidity_ind, SQLUDF_NULLIND * wind_ind, SQLUDF_NULLIND * wind_velocity_ind, SQLUDF_NULLIND * barometer_ind, SQLUDF_NULLIND * forecast_ind, /* You may want to add more field indicators here */ /* UDF always-present (trailing) input arguments */ SQLUDF_TRAIL_ARGS_ALL ) { scratch_area * save_area ; char line_buf[81] ; int line_buf_pos ; /* SQLUDF_SCRAT is part of SQLUDF_TRAIL_ARGS_ALL */ /* Preserve information from one function call to the next call */ save_area = ( scratch_area * ) ( SQLUDF_SCRAT->data ) ; /* SQLUDF_CALLT is part of SQLUDF_TRAIL_ARGS_ALL */ switch( SQLUDF_CALLT ) { /* First call UDF: Open table and fetch first row */ case SQL_TF_OPEN: /* If you use a weather data text file specify full path */ /* save_area->file_ptr = fopen("tblsrv.dat","r"); */ save_area->file_pos = 0 ;
170
break ; /* Normal call UDF: Fetch next row */ (See note 2) case SQL_TF_FETCH: /* If you use a weather data text file */ /* memset(line_buf, \0, 81); */ /* if (fgets(line_buf, 80, save_area->file_ptr) == NULL) { */ if ( weather_data[save_area->file_pos] == ( char * ) 0 ) { /* SQLUDF_STATE is part of SQLUDF_TRAIL_ARGS_ALL */ strcpy( SQLUDF_STATE, "02000" ) ; break ; } memset( line_buf, \0, 81 ) ; strcpy( line_buf, weather_data[save_area->file_pos] ) ; line_buf[3] = \0 ; /* Clean all field data and field null indicator data */ clean_fields( 0 ) ; /* Fills city field null indicator data */ fields[0].fld_ind = SQL_NOTNULL ; /* Find a full city name using a short name */ /* Fills city field data */ if ( get_name( line_buf, fields[0].fld_field ) == 0 ) { save_area->file_pos++ ; /* If you use a weather data text file */ /* memset(line_buf, \0, 81); */ /* if (fgets(line_buf, 80, save_area->file_ptr) == NULL) { */ if ( weather_data[save_area->file_pos] == ( char * ) 0 ) { /* SQLUDF_STATE is part of SQLUDF_TRAIL_ARGS_ALL */ strcpy( SQLUDF_STATE, "02000" ) ; break ; } memset( line_buf, \0, 81 ) ; strcpy( line_buf, weather_data[save_area->file_pos] ) ; line_buf_pos = strlen( line_buf ) ; while ( line_buf_pos > 0 ) { if ( line_buf[line_buf_pos] >= ) line_buf_pos = 0 ; else { line_buf[line_buf_pos] = \0 ; line_buf_pos-- ; } } } /* Fills field data and field null indicator data ... */ /* ... for selected city from text weather data */ get_value( line_buf, 1 ) ; /* Skips city field */ /* Builds return row fields */ strcpy( city, fields[0].fld_field ) ; memcpy( (void *) temp_in_f, fields[1].fld_field, sizeof( SQLUDF_INTEGER ) ) ; memcpy( (void *) humidity, fields[2].fld_field, sizeof( SQLUDF_INTEGER ) ) ; strcpy( wind, fields[3].fld_field ) ; memcpy( (void *) wind_velocity, fields[4].fld_field, sizeof( SQLUDF_INTEGER ) ) ; memcpy( (void *) barometer, fields[5].fld_field,
SQL programming
171
sizeof( SQLUDF_DOUBLE ) ) ; strcpy( forecast, fields[6].fld_field ) ; /* Builds return row field null indicators */ memcpy( (void *) city_ind, &(fields[0].fld_ind), sizeof( SQLUDF_NULLIND ) ) ; memcpy( (void *) temp_in_f_ind, &(fields[1].fld_ind), sizeof( SQLUDF_NULLIND ) ) ; memcpy( (void *) humidity_ind, &(fields[2].fld_ind), sizeof( SQLUDF_NULLIND ) ) ; memcpy( (void *) wind_ind, &(fields[3].fld_ind), sizeof( SQLUDF_NULLIND ) ) ; memcpy( (void *) wind_velocity_ind, &(fields[4].fld_ind), sizeof( SQLUDF_NULLIND ) ) ; memcpy( (void *) barometer_ind, &(fields[5].fld_ind), sizeof( SQLUDF_NULLIND ) ) ; memcpy( (void *) forecast_ind, &(fields[6].fld_ind), sizeof( SQLUDF_NULLIND ) ) ; /* Next city weather data */ save_area->file_pos++ ; break ; /* Special last call UDF for clean up (no real args!): Close table */ (See note 3) case SQL_TF_CLOSE: /* If you use a weather data text file */ /* fclose(save_area->file_ptr); */ /* save_area->file_ptr = NULL; */ save_area->file_pos = 0 ; break ; } }
Referring to the embedded notes in this UDF code, observe that: 1. The scratchpad is defined. The row variable is initialized on the OPEN call, and the iptr array and nbr_rows variable are filled in by the mystery function at open time. 2. FETCH traverses the iptr array, using row as an index, and moves the values of interest from the current element of iptr to the location pointed to by out_c1, out_c2, and out_c3 result value pointers. 3. Finally, CLOSE frees the storage acquired by OPEN and anchored in the scratchpad. Following is the CREATE FUNCTION statement for this UDF:
CREATE FUNCTION tfweather_u() RETURNS TABLE (CITY VARCHAR(25), TEMP_IN_F INTEGER, HUMIDITY INTEGER, WIND VARCHAR(5), WIND_VELOCITY INTEGER, BAROMETER FLOAT, FORECAST VARCHAR(25)) SPECIFIC tfweather_u DISALLOW PARALLEL NOT FENCED DETERMINISTIC
172
NO SQL NO EXTERNAL ACTION SCRATCHPAD NO FINAL CALL LANGUAGE C PARAMETER STYLE DB2SQL EXTERNAL NAME LIB1/WEATHER(weather);
Referring to the embedded numbered notes, observe that: v It does not take any input, and returns 7 output columns. v SCRATCHPAD is specified, so DB2 allocates, properly initializes and passes the scratchpad argument. v NO FINAL CALL is specified. v The function is specified as NOT DETERMINISTIC, because it depends on more than the SQL input arguments. That is, it depends on the mystery function and we assume that the content can vary from execution to execution. v DISALLOW PARALLEL is required for table functions. v CARDINALITY 100 is an estimate of the expected number of rows returned, provided to the DB2 optimizer. v DBINFO is not used, and the optimization to only return the columns needed by the particular statement referencing the function is not implemented. v NOT NULL CALL is specified, so the UDF will not be called if any of its input SQL arguments are NULL, and does not need to check for this condition. To select all of the rows generated by this table function, use the following query:
SELECT * FROM TABLE (tfweather_u())x
or
BLOOP(NULL)
You can use the CAST specification to provide a data type for the parameter marker or NULL value that function resolution can use:
BLOOP(CAST(? AS INTEGER))
or
SQL programming
173
BLOOP(CAST(NULL AS INTEGER))
Using qualified function references: If you use a qualified function reference, you restrict the search for a matching function to the specified schema. For example, you have the following statement:
SELECT PABLO.BLOOP(COLUMN1) FROM T
Only the BLOOP functions in schema PABLO are considered. It does not matter that user SERGE has defined a BLOOP function, or whether there is a built-in BLOOP function. Now suppose that user PABLO has defined two BLOOP functions in his schema:
CREATE FUNCTION BLOOP (INTEGER) RETURNS ... CREATE FUNCTION BLOOP (DOUBLE) RETURNS ...
BLOOP is thus overloaded within the PABLO schema, and the function selection algorithm chooses the best BLOOP, depending on the data type of the argument, COLUMN1. In this case, both of the PABLO.BLOOPs take numeric arguments, and if COLUMN1 is not one of the numeric types, the statement will fail. On the other hand, if COLUMN1 is either SMALLINT or INTEGER, function selection will resolve to the first BLOOP, while if COLUMN1 is DECIMAL or DOUBLE, the second BLOOP will be chosen. Several points about this example: 1. It illustrates argument promotion. The first BLOOP is defined with an INTEGER parameter, yet you can pass it a SMALLINT argument. The function selection algorithm supports promotions among the built-in data types and DB2 performs the appropriate data value conversions. 2. If for some reason you want to call the second BLOOP with a SMALLINT or INTEGER argument, you need to take an explicit action in your statement as follows:
SELECT PABLO.BLOOP(DOUBLE(COLUMN1)) FROM T
3. If you want to call the first BLOOP with a DECIMAL or DOUBLE argument, you have your choice of explicit actions, depending on your intent:
SELECT PABLO.BLOOP(INTEGER(COLUMN1)) FROM T SELECT PABLO.BLOOP(FLOOR(COLUMN1)) FROM T
Related reference Using unqualified function references You can use an unqualified function reference instead of a qualified function reference. When searching for a matching function, DB2 normally uses the function path to qualify the reference. Defining a UDT on page 202 You define a user-defined type (UDT) using the CREATE DISTINCT TYPE statement. Using unqualified function references: You can use an unqualified function reference instead of a qualified function reference. When searching for a matching function, DB2 normally uses the function path to qualify the reference. In the case of the DROP FUNCTION and COMMENT ON FUNCTION statements, the reference is qualified using the current authorization ID, if they are unqualified for *SQL naming, or *LIBL for *SYS naming. Thus, it is important that you know what your function path is, and what, if any, conflicting functions exist in the schemas of your current function path. For example, suppose that you are PABLO and your static SQL statement is as follows, where COLUMN1 is data type INTEGER:
SELECT BLOOP(COLUMN1) FROM T
174
You have created the two BLOOP functions in the section Using qualified function reference, and you want and expect one of them to be chosen. If the following default function path is used, the first BLOOP is chosen (since COLUMN1 is INTEGER), if there is no conflicting BLOOP in QSYS or QSYS2:
"QSYS","QSYS2","PABLO"
However, suppose you have forgotten that you are using a script for precompiling and binding which you previously wrote for another purpose. In this script, you explicitly coded your SQLPATH parameter to specify the following function path for another reason that does not apply to your current work:
"KATHY","QSYS","QSYS2","PABLO"
If there is a BLOOP function in schema KATHY, the function selection can very well resolve to that function, and your statement executes without error. You are not notified because DB2 assumes that you know what you are doing. It is your responsibility to identify the incorrect output from your statement and make the required correction. Related reference Using qualified function references on page 174 If you use a qualified function reference, you restrict the search for a matching function to the specified schema. Summary of function references: For both qualified and unqualified function references, the function selection algorithm looks at all the applicable functions, both built-in and user-defined functions, that have the given name, the same number of defined parameters as arguments, and each parameter identical to or promotable from the type of the corresponding argument. Applicable functions means functions in the named schema for a qualified reference, or functions in the schemas of the function path for an unqualified reference. The algorithm looks for an exact match, or failing that, a best match among these functions. The current function path is used, in the case of an unqualified reference only, as the deciding factor if two identically good matches are found in different schemas. An interesting feature is the fact that function references can be nested, even references to the same function. This is generally true for built-in functions as well as UDFs. However, there are some limitations when column functions are involved. Refining an earlier example:
CREATE FUNCTION BLOOP (INTEGER) RETURNS INTEGER ... CREATE FUNCTION BLOOP (DOUBLE) RETURNS INTEGER ...
If COLUMN1 is a DECIMAL or DOUBLE column, the inner BLOOP reference resolves to the second BLOOP defined above. Because this BLOOP returns an INTEGER, the outer BLOOP resolves to the first BLOOP. Alternatively, if COLUMN1 is a SMALLINT or INTEGER column, the inner BLOOP reference resolves to the first BLOOP defined above. Because this BLOOP returns an INTEGER, the outer BLOOP also resolves to the first BLOOP. In this case, you are seeing nested references to the same function. A few additional points important for function references are: v You can define a function with the name of one of the SQL operators. For example, suppose you can attach some meaning to the "+" operator for values which have distinct type BOAT. You can define the following UDF:
SQL programming
175
You are not permitted to overload the built-in conditional operators such as >, =, LIKE, IN, and so on, in this way. v The function selection algorithm does not consider the context of the reference in resolving to a particular function. Look at these BLOOP functions, modified a bit from before:
CREATE FUNCTION BLOOP (INTEGER) RETURNS INTEGER ... CREATE FUNCTION BLOOP (DOUBLE) RETURNS CHAR(10)...
Because the best match, resolved using the SMALLINT argument, is the first BLOOP defined above, the second operand of the CONCAT resolves to data type INTEGER. The statement might not return the expected result since the returned integer will be cast as a VARCHAR before the CONCAT is performed. If the first BLOOP was not present, the other BLOOP is chosen and the statement execution is successful. v UDFs can be defined with parameters or results having any of the LOB types: BLOB, CLOB, or DBCLOB. The system will materialize the entire LOB value in storage before calling such a function, even if the source of the value is a LOB locator host variable. For example, consider the following fragment of a C language application:
EXEC SQL BEGIN DECLARE SECTION; SQL TYPE IS CLOB(150K) clob150K ; /* LOB host var */ SQL TYPE IS CLOB_LOCATOR clob_locator1; /* LOB locator host var */ char string[40]; /* string host var */ EXEC SQL END DECLARE SECTION;
Either host variable :clob150K or :clob_locator1 is valid as an argument for a function whose corresponding parameter is defined as CLOB(500K). Referring to the FINDSTRING defined in Example: String search on page 154 both of the following are valid in the program:
... SELECT FINDSTRING (:clob150K, :string) FROM ... ... SELECT FINDSTRING (:clob_locator1, :string) FROM ...
v External UDF parameters or results which have one of the LOB types can be created with the AS LOCATOR modifier. In this case, the entire LOB value is not materialized before invocation. Instead, a LOB LOCATOR is passed to the UDF. You can also use this capability on UDF parameters or results which have a distinct type that is based on a LOB. This capability is limited to external UDFs. Note that the argument to such a function can be any LOB value of the defined type; it does not need to be a host variable defined as one of the LOCATOR types. The use of host variable locators as arguments is completely unrelated to the use of AS LOCATOR in UDF parameters and result definitions. v UDFs can be defined with distinct types as parameters or as the result. DB2 will pass the value to the UDF in the format of the source data type of the distinct type. Distinct type values that originate in a host variable and which are used as arguments to a UDF which has its corresponding parameter defined as a distinct type must be explicitly cast to the distinct type by the user. There is no host language type for distinct types. DB2s strong typing necessitates this. Otherwise your results may be ambiguous. So, consider the BOAT distinct type that is defined over a BLOB that takes an object of type BOAT as its argument. In the following fragment of a C language application, the host variable :ship holds the BLOB value that is to passed to the BOAT_COST function:
EXEC SQL BEGIN DECLARE SECTION; SQL TYPE IS BLOB(150K) ship; EXEC SQL END DECLARE SECTION;
Both of the following statements correctly resolve to the BOAT_COST function, because both cast the :ship host variable to type BOAT:
176
... SELECT BOAT_COST (BOAT(:ship)) FROM ... ... SELECT BOAT_COST (CAST(:ship AS BOAT)) FROM ...
If there are multiple BOAT distinct types in the database, or BOAT UDFs in other schema, you must be careful with your function path. Otherwise your results may be unpredictable.
Triggers
A trigger is a set of actions that runs automatically when a specified change operation is performed on a specified table or view. The change operation can be an SQL INSERT, UPDATE, or DELETE statement, or an insert, an update, or a delete high-level language statement in an application program. Triggers are useful for tasks such as enforcing business rules, validating input data, and keeping an audit trail. Triggers can be defined as SQL or external. For an external trigger, the CRTPFTRG CL command is used. The program containing the set of trigger actions can be defined in any supported high level language. External triggers can be insert, update, delete, or read triggers. For an SQL trigger, the CREATE TRIGGER statement is used. The trigger program is defined entirely using SQL. SQL triggers can be insert, update, or delete triggers. | | | | | | | Once a trigger is associated with a table or view, the trigger support calls the trigger program whenever a change operation is initiated against the table or view, or any logical file or view created over the table or view. SQL triggers and external triggers can be defined for the same table. Only SQL triggers can be defined for a view. Up to 200 triggers can be defined for a single table or view. Each change operation for a table can call a trigger before or after the change operation occurs. Additionally, you can add a read trigger that is called every time the table is accessed. Thus, a table can be associated with many types of triggers. v v v v v v v | | | | | Before delete trigger Before insert trigger Before update trigger After delete trigger After insert trigger After update trigger Read-only trigger (external trigger only)
Each change operation for a view can call an instead of trigger which will perform some set of actions instead of the insert, update, or delete. A view can be associated with an: v Instead of delete trigger v Instead of insert trigger v Instead of update trigger Related tasks Triggering automatic events in your database
SQL triggers
| | | The SQL CREATE TRIGGER statement provides a way for the database management system to actively control, monitor, and manage a group of tables and views whenever an insert, an update, or a delete operation is performed.
SQL programming
177
The statements specified in the SQL trigger are executed each time an SQL insert, update, or delete operation is performed. An SQL trigger may call stored procedures or user-defined functions to perform additional processing when the trigger is executed. | | | | | | | | | | | Unlike stored procedures, an SQL trigger cannot be directly called from an application. Instead, an SQL trigger is invoked by the database management system on the execution of a triggering insert, update, or delete operation. The definition of the SQL trigger is stored in the database management system and is invoked by the database management system when the SQL table or view that the trigger is defined on, is modified. An SQL trigger can be created by specifying the CREATE TRIGGER SQL statement. All objects referred to in the CREATE TRIGGER statement (such as tables and functions) must exist; otherwise, the trigger will not be created. The statements in the routine-body of the SQL trigger are transformed by SQL into a program (*PGM) object. The program is created in the schema specified by the trigger name qualifier. The specified trigger is registered in the SYSTRIGGERS, SYSTRIGDEP, SYSTRIGCOL, and SYSTRIGUPD SQL catalogs. Related concepts Debugging an SQL routine on page 188 By specifying SET OPTION DBGVIEW = *SOURCE in the CREATE PROCEDURE, CREATE FUNCTION, or CREATE TRIGGER statement, you can debug the generated program or module at the SQL statement level. Related reference SQL control statements CREATE TRIGGER BEFORE SQL triggers: BEFORE triggers cannot change tables, but they can be used to verify input column values and to change column values that are inserted or updated in a table. In the following example, the trigger is used to set the fiscal quarter for the corporation before inserting the row into the target table.
CREATE TABLE TransactionTable (DateOfTransaction DATE, FiscalQuarter SMALLINT) CREATE TRIGGER TransactionBeforeTrigger BEFORE INSERT ON TransactionTable REFERENCING NEW AS new_row FOR EACH ROW MODE DB2ROW BEGIN DECLARE newmonth SMALLINT; SET newmonth = MONTH(new_row.DateOfTransaction); IF newmonth < 4 THEN SET new_row.FiscalQuarter=3; ELSEIF newmonth < 7 THEN SET new_row.FiscalQuarter=4; ELSEIF newmonth < 10 THEN SET new_row.FiscalQuarter=1; ELSE SET new_row.FiscalQuarter=2; END IF; END
For the SQL insert statement below, the FiscalQuarter column is set to 2, if the current date is November 14, 2000.
INSERT INTO TransactionTable(DateOfTransaction) VALUES(CURRENT DATE)
178
SQL triggers have access to and can use user-defined types (UDTs) and stored procedures. In the following example, the SQL trigger calls a stored procedure to execute some predefined business logic, in this case, to set a column to a predefined value for the business.
CREATE DISTINCT TYPE enginesize AS DECIMAL(5,2) WITH COMPARISONS CREATE DISTINCT TYPE engineclass AS VARCHAR(25) WITH COMPARISONS CREATE PROCEDURE SetEngineClass(IN SizeInLiters enginesize, OUT CLASS engineclass) LANGUAGE SQL CONTAINS SQL BEGIN IF SizeInLiters<2.0 THEN SET CLASS = Mouse; ELSEIF SizeInLiters<3.1 THEN SET CLASS =Economy Class; ELSEIF SizeInLiters<4.0 THEN SET CLASS =Most Common Class; ELSEIF SizeInLiters<4.6 THEN SET CLASS = Getting Expensive; ELSE SET CLASS =Stop Often for Fillups; END IF; END CREATE TABLE EngineRatings (VariousSizes enginesize, ClassRating engineclass) CREATE TRIGGER SetEngineClassTrigger BEFORE INSERT ON EngineRatings REFERENCING NEW AS new_row FOR EACH ROW MODE DB2ROW CALL SetEngineClass(new_row.VariousSizes, new_row.ClassRating)
For the SQL insert statement below, the ClassRating column is set to Economy Class, if the VariousSizes column has the value of 3.0.
INSERT INTO EngineRatings(VariousSizes) VALUES(3.0)
SQL requires all tables, user-defined functions, procedures and user-defined types to exist before creating an SQL trigger. In the examples above, all of the tables, stored procedures, and user-defined types are defined before the trigger is created. AFTER SQL triggers: An after trigger runs after the corresponding insert, update, or delete changes are applied to the table. The WHEN condition can be used in an SQL trigger to specify a condition. If the condition evaluates to true, the SQL statements in the SQL trigger routine body are run. If the condition evaluates to false, the SQL statements in the SQL trigger routine body are not run, and control is returned to the database system. In the following example, a query is evaluated to determine if the statements in the trigger routine body should be run when the trigger is activated.
CREATE TABLE TodaysRecords(TodaysMaxBarometricPressure FLOAT, TodaysMinBarometricPressure FLOAT) CREATE TABLE OurCitysRecords(RecordMaxBarometricPressure FLOAT, RecordMinBarometricPressure FLOAT) CREATE TRIGGER UpdateMaxPressureTrigger AFTER UPDATE OF TodaysMaxBarometricPressure ON TodaysRecords REFERENCING NEW AS new_row FOR EACH ROW MODE DB2ROW WHEN (new_row.TodaysMaxBarometricPressure> (SELECT MAX(RecordMaxBarometricPressure) FROM
SQL programming
179
OurCitysRecords)) UPDATE OurCitysRecords SET RecordMaxBarometricPressure = new_row.TodaysMaxBarometricPressure CREATE TRIGGER UpdateMinPressureTrigger AFTER UPDATE OF TodaysMinBarometricPressure ON TodaysRecords REFERENCING NEW AS new_row FOR EACH ROW MODE DB2ROW WHEN(new_row.TodaysMinBarometricPressure< (SELECT MIN(RecordMinBarometricPressure) FROM OurCitysRecords)) UPDATE OurCitysRecords SET RecordMinBarometricPressure = new_row.TodaysMinBarometricPressure
For the SQL update statement below, the RecordMaxBarometricPressure in OurCitysRecords is updated by the UpdateMaxPressureTrigger.
UPDATE TodaysRecords SET TodaysMaxBarometricPressure = 29.95
But tomorrow, if the TodaysMaxBarometricPressure is only 29.91, then the RecordMaxBarometricPressure is not updated.
UPDATE TodaysRecords SET TodaysMaxBarometricPressure = 29.91
SQL allows the definition of multiple triggers for a single triggering action. In the previous example, there are two AFTER UPDATE triggers: UpdateMaxPressureTrigger and UpdateMinPressureTrigger. These triggers are activated only when specific columns of the table TodaysRecords are updated. AFTER triggers may modify tables. In the example above, an UPDATE operation is applied to a second table. Note that recursive insert and update operations should be avoided. The database management system terminates the operation if the maximum trigger nesting level is reached. You can avoid recursion by adding conditional logic so that the insert or update operation is exited before the maximum nesting level is reached. The same situation needs to be avoided in a network of triggers that recursively cascade | through the network of triggers. | INSTEAD OF SQL triggers: | An INSTEAD OF trigger is an SQL trigger that is processed instead of an SQL UPDATE, DELETE or | INSERT statement. Unlike SQL BEFORE and AFTER triggers, an INSTEAD OF trigger can be defined | only on a view, not a table. | An INSTEAD OF trigger allows a view, which is not inherently insertable, updatable, or deletable, to be | inserted into, updated, or deleted from. See CREATE VIEW for more information about deleteable, | updatable, and insertable views. | After an SQL INSTEAD OF trigger is added to a view, the view which previously could only be read | from can be used as the target of an insert, update, or delete operation. The INSTEAD OF trigger defines | the operations which need to be performed to maintain the view. | A view can be used to control access to tables. INSTEAD OF triggers can simplify the maintenance of | access control to tables.
180
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
Using an INSTEAD OF trigger The definition of the following view V1 is updatable, deletable, and insertable:
CREATE TABLE T1 (C1 VARCHAR(10), C2 INT) CREATE VIEW V1(X1) AS SELECT C1 FROM T1 WHERE C2 > 10
For the following insert statement, C1 in table T1 will be assigned a value of A. C2 will be assigned the NULL value. The NULL value would cause the new row to not match the selection criteria C2 > 10 for the view V1.
INSERT INTO V1 VALUES(A)
Adding the INSTEAD OF trigger IOT1 can provide a different value for the row that will be selected by the view:
CREATE TRIGGER IOT1 INSTEAD OF INSERT ON V1 REFERENCING NEW AS NEW_ROW FOR EACH ROW MODE DB2SQL INSERT INTO T1 VALUES(NEW_ROW.X1, 15)
Making a view deletable The definition of the following join view V3 is not updatable, deletable, or insertable:
CREATE TABLE A (A1 VARCHAR(10), A2 INT) CREATE VIEW V1(X1) AS SELECT A1 FROM A CREATE TABLE B (B1 VARCHAR(10), B2 INT) CREATE VIEW V2(Y1) AS SELECT B1 FROM B CREATE VIEW V3(Z1, Z2) AS SELECT V1.X1, V2.Y1 FROM V1, V2 WHERE V1.X1 = A AND V2.Y1 > B
With this trigger, the following DELETE statement is allowed. It deletes all rows from table A having an A1 value of A, and all rows from table B having a B1 value of X.
DELETE FROM V3 WHERE Z1 = A AND Z2 = X
INSTEAD OF triggers with views defined on views The following definition of view V2 defined on V1 is not inherently insertable, updatable, or deletable:
CREATE TABLE T1 (C1 VARCHAR(10), C2 INT) CREATE TABLE T2 (D1 VARCHAR(10), D2 INT) CREATE VIEW V1(X1, X2) AS SELECT C1, C2 FROM T1 UNION SELECT D1, D2 FROM T2 CREATE VIEW V2(Y1, Y2) AS SELECT X1, X2 FROM V1
SQL programming
181
| C1 = OLD_ROW.X1 AND C2 = OLD_ROW.X2; UPDATE T2 SET D1 = NEW_ROW.X1, D2 = NEW_ROW.D2 WHERE | D1 = OLD_ROW.X1 AND D2 = OLD_ROW.X2; | | END | View V2 remains not updatable since the original definition of view V2 remains not updatable. | Using INSTEAD OF triggers with BEFORE and AFTER triggers | | | | | | | | | | | | | | | | The addition of an INSTEAD OF trigger to a view does not cause any conflicts with BEFORE and AFTER triggers defined on the base tables:
CREATE TABLE T1 (C1 VARCHAR(10), C2 DATE) CREATE TABLE T2 (D1 VARCHAR(10)) CREATE TRIGGER AFTER1 AFTER DELETE ON T1 REFERENCING OLD AS OLD_ROW FOR EACH ROW MODE DB2SQL DELETE FROM T2 WHERE D1 = OLD_ROW.C1 CREATE VIEW V1(X1, X2) AS SELECT SUBSTR(T1.C1, 1, 1), DAYOFWEEK_ISO(T1.C2) FROM T1 CREATE TRIGGER IOT1 INSTEAD OF DELETE ON V1 REFERENCING OLD AS OLD_ROW FOR EACH ROW MODE DB2SQL DELETE FROM T1 WHERE C1 LIKE (OLD_ROW.X1 CONCAT %)
| Any delete operations for view V1 result in the AFTER DELETE trigger AFTER1 being activated also | because trigger IOT1 performs a delete on table T1. The delete for table T1 causes the AFTER1 trigger to | be activated. | Dependent views and INSTEAD OF triggers | | | | When adding an INSTEAD OF trigger to a view, if the view definition references views that also have INSTEAD OF triggers defined, you should define INSTEAD OF triggers for all three operations, UPDATE, DELETE, and INSERT, to avoid confusion on what capabilities the view being defined contains versus what the capabilities of any dependent views have. Handlers in SQL triggers: A handler in an SQL trigger gives the SQL trigger the ability to recover from an error or log information about an error that has occurred while processing the SQL statements in the trigger routine body. In the following example, there are two handlers defined: one to handle the overflow condition and a second handler to handle SQL exceptions.
CREATE TABLE ExcessInventory(Description VARCHAR(50), ItemWeight SMALLINT) CREATE TABLE YearToDateTotals(TotalWeight SMALLINT) CREATE TABLE FailureLog(Item VARCHAR(50), ErrorMessage VARCHAR(50), ErrorCode INT) CREATE TRIGGER InventoryDeleteTrigger AFTER DELETE ON ExcessInventory REFERENCING OLD AS old_row FOR EACH ROW MODE DB2ROW BEGIN DECLARE sqlcode INT; DECLARE invalid_number condition FOR 22003; DECLARE exit handler FOR invalid_number INSERT INTO FailureLog VALUES(old_row.Description, Overflow occurred in YearToDateTotals, sqlcode); DECLARE exit handler FOR sqlexception
182
INSERT INTO FailureLog VALUES(old_row.Description, SQL Error occurred in InventoryDeleteTrigger, sqlcode); UPDATE YearToDateTotals SET TotalWeight=TotalWeight + old_row.itemWeight; END
When the first SQL delete statement below is executed, the ItemWeight for the item Desks is added to the column total for TotalWeight in the table YearToDateTotals. When the second SQL delete statement is executed, an overflow occurs when the ItemWeight for the item Chairs is added to the column total for TotalWeight, as the column only handles values up to 32767. When the overflow occurs, the invalid_number exit handler is executed and a row is written to the FailureLog table. The sqlexception exit handler runs, for example, if the YearToDateTotals table was deleted by accident. In this example, the handlers are used to write a log so that the problem can be diagnosed at a later time.
DELETE FROM ExcessInventory WHERE Description=Desks DELETE FROM ExcessInventory WHERE Description=Chairs
SQL trigger transition tables: An SQL trigger might need to refer to all of the affected rows for an SQL insert, update, or delete operation. For example, a trigger needs to apply aggregate functions, such as MIN or MAX, to a specific column of the affected rows. The OLD_TABLE and NEW_TABLE transition tables can be used for this purpose. In the following example, the trigger applies the aggregate function MAX to all of the affected rows of the table StudentProfiles.
CREATE TABLE StudentProfiles(StudentsName VARCHAR(125), StudentsYearInSchool SMALLINT, StudentsGPA DECIMAL(5,2)) CREATE TABLE CollegeBoundStudentsProfile (YearInSchoolMin SMALLINT, YearInSchoolMax SMALLINT, StudentGPAMin DECIMAL(5,2), StudentGPAMax DECIMAL(5,2)) CREATE TRIGGER UpdateCollegeBoundStudentsProfileTrigger AFTER UPDATE ON StudentProfiles REFERENCING NEW_TABLE AS ntable FOR EACH STATEMENT MODE DB2SQL BEGIN DECLARE maxStudentYearInSchool SMALLINT; SET maxStudentYearInSchool = (SELECT MAX(StudentsYearInSchool) FROM ntable); IF maxStudentYearInSchool > (SELECT MAX (YearInSchoolMax) FROM CollegeBoundStudentsProfile) THEN UPDATE CollegeBoundStudentsProfile SET YearInSchoolMax = maxStudentYearInSchool; END IF; END
In the preceding example, the trigger is processed a single time following the processing of a triggering update statement because it is defined as a FOR EACH STATEMENT trigger. You will need to consider the processing overhead required by the database management system for populating the transition tables when you define a trigger that references transition tables.
SQL programming
183
External triggers
For an external trigger, the program that contains the set of trigger actions can be defined in any supported high-level language that creates a *PGM object. The trigger program can have SQL embedded in it. To define an external trigger, you must create a trigger program and add it to a table using the ADDPFTRG CL command or you can add it using iSeries Navigator. To add a trigger to a table, you must: v Identify the table v Identify the kind of operation v Identify the program that performs the actions that you want. Related tasks Triggering automatic events in your database Example: External triggers: This example shows an external trigger program that is written in ILE C with embedded SQL. Note: By using the code examples, you agree to the terms of the Code license and disclaimer information on page 300.
#include "string.h" #include "stdlib.h" #include "stdio.h" #include <recio.h> #include <xxcvt.h> #include "qsysinc/h/trgbuf" /* Trigger input parameter */ #include "lib1/csrc/msghand1" /* User defined message handler */ /*********************************************************************/ /* This is a trigger program which is called whenever there is an */ /* update to the EMPLOYEE table. If the employees commission is */ /* greater than the maximum commission, this trigger program will */ /* increase the employees salary by 1.04 percent and insert into */ /* the RAISE table. */ /* */ /* The EMPLOYEE record information is passed from the input parameter*/ /* to this trigger program. */ /*********************************************************************/ Qdb_Trigger_Buffer_t *hstruct; char *datapt; /*******************************************************/ /* Structure of the EMPLOYEE record which is used to */ /* store the old or the new record that is passed to */ /* this trigger program. */ /* */ /* Note : You must ensure that all the numeric fields */ /* are aligned at 4 byte boundary in C. */ /* Used either Packed struct or filler to reach */ /* the byte boundary alignment. */ /*******************************************************/ _Packed struct rec{ char empn[6]; _Packed struct { short fstlen ; char fstnam[12]; } fstname; char minit[1]; _Packed struct { short lstlen; char lstnam[15]; } lstname; char dept[3];
184
char phone[4]; char hdate[10]; char jobn[8]; short edclvl; char sex1[1]; char bdate[10]; decimal(9,2) salary1; decimal(9,2) bonus1; decimal(9,2) comm1; } oldbuf, newbuf; EXEC SQL INCLUDE SQLCA; main(int argc, char **argv) { int i; int obufoff; /* int nuloff; /* int nbufoff; /* int nul2off; /* short work_days = 253; /* decimal(9,2) commission = 2000.00; /* decimal(9,2) percentage = 1.04; /* char raise_date[12] = "1982-06-01";/* struct { char empno[6]; char name[30]; decimal(9,2) salary; decimal(9,2) new_salary; } rpt1; /*******************************************************/ /* Start to monitor any exception. */ /*******************************************************/ _FEEDBACK fc; _HDLR_ENTRY hdlr = main_handler; /****************************************/ /* Make the exception handler active. */ /****************************************/ CEEHDLR(&hdlr, NULL, &fc); /****************************************/ /* Ensure exception handler OK */ /****************************************/ if (fc.MsgNo != CEE0000) { printf("Failed to register exception handler.\n"); exit(99); }; /*******************************************************/ /* Move the data from the trigger buffer to the local */ /* structure for reference. */ /*******************************************************/ hstruct = (Qdb_Trigger_Buffer_t *)argv[1]; datapt = (char *) hstruct; obufoff = hstruct ->Old_Record_Offset; /* old buffer memcpy(&oldbuf,datapt+obufoff,; hstruct->Old_Record_Len); nbufoff = hstruct ->New_Record_Offset; /* new buffer memcpy(&newbuf,datapt+nbufoff,; hstruct->New_Record_Len); EXEC SQL WHENEVER SQLERROR GO TO ERR_EXIT; */ */
old buffer offset old null byte map offset new buffer offset new null byte map offset work days during in one year cutoff to qualify for raised salary as percentage effective raise date
*/ */ */ */ */ */ */ */
/*******************************************************/
SQL programming
185
/* Set the transaction isolation level to the same as */ /* the application based on the input parameter in the */ /* trigger buffer. */ /*******************************************************/ if(strcmp(hstruct->Commit_Lock_Level,"0") == 0) EXEC SQL SET TRANSACTION ISOLATION LEVEL NONE; else{ if(strcmp(hstruct->Commit_Lock_Level,"1") == 0) EXEC SQL SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED, READ WRITE; else { if(strcmp(hstruct->Commit_Lock_Level,"2") == 0) EXEC SQL SET TRANSACTION ISOLATION LEVEL READ COMMITTED; else if(strcmp(hstruct->Commit_Lock_Level,"3") == 0) EXEC SQL SET TRANSACTION ISOLATION LEVEL ALL; } } /********************************************************/ /* If the employees commission is greater than maximum */ /* commission, then increase the employees salary */ /* by 1.04 percent and insert into the RAISE table. */ /********************************************************/ if (newbuf.comm1 >= commission) { EXEC SQL SELECT EMPNO, EMPNAME, SALARY INTO :rpt1.empno, :rpt1.name, :rpt1.salary FROM TRGPERF/EMP_ACT WHERE EMP_ACT.EMPNO=:newbuf.empn ; if (sqlca.sqlcode == 0) then { rpt1.new_salary = salary * percentage; EXEC SQL INSERT INTO TRGPERF/RAISE VALUES(:rpt1); } goto finished; } err_exit: exit(1); /* All done */ finished: return; } /* end of main line */ /******************************************************************/ /* INCLUDE NAME : MSGHAND1 */ /* */ /* DESCRIPTION : Message handler to signal an exception to */ /* the application to inform that an */ /* error occured in the trigger program. */ /* */ /* NOTE : This message handler is a user defined routine. */ /* */ /******************************************************************/ #include <stdio.h> #include <stdlib.h> #include <recio.h> #include <leawi.h> #pragma linkage (QMHSNDPM, OS) void QMHSNDPM(char *, void *, void *, int, /* /* /* /* Message identifier Qualified message file name Message data or text Length of message data or text */ */ */ */
186
Message type */ Call message queue */ Call stack counter */ Message key */ Error code */ Optionals: length of call message queue name Call stack entry qualification display external messages screen wait time */ /*********************************************************************/ /******** This is the start of the exception handler function. */ /*********************************************************************/ void main_handler(_FEEDBACK *cond, _POINTER *token, _INT4 *rc, _FEEDBACK *new) { /****************************************/ /* Initialize variables for call to */ /* QMHSNDPM. */ /* User must create a message file and */ /* define a message ID to match the */ /* following data. */ /****************************************/ char message_id[7] = "TRG9999"; char message_file[20] = "MSGF LIB1 "; char message_data[50] = "Trigger error " ; int message_len = 30; char message_type[10] = "*ESCAPE "; char message_q[10] = "_C_pep "; int pgm_stack_cnt = 1; char message_key[4]; /****************************************/ /* Declare error code structure for */ /* QMHSNDPM. */ /****************************************/ struct error_code { int bytes_provided; int bytes_available; char message_id[7]; } error_code; error_code.bytes_provided = 15; /****************************************/ /* Set the error handler to resume and */ /* mark the last escape message as */ /* handled. */ /****************************************/ *rc = CEE_HDLR_RESUME; /****************************************/ /* Send my own *ESCAPE message. */ /****************************************/ QMHSNDPM(message_id, &message_file, &message_data, message_len, message_type, message_q, pgm_stack_cnt, &message_key, &error_code ); /****************************************/ /* Check that the call to QMHSNDPM */ /* finished correctly. */ /****************************************/ if (error_code.bytes_available != 0)
/* /* /* /* /* /*
SQL programming
187
188
The following tips are especially important for functions because a function tends to be called multiple times from many different procedures: v Use the NOT FENCED option so UDFs run in the same thread as the caller. v Use the DETERMINISTIC option on procedures and UDFs that return the same results for identical inputs. This allows the optimizer to cache the results of a function call or order where the function is called in the execution stream to reduce the run time. v Use the NO EXTERNAL ACTION option on UDFs that do not take an action outside the scope of the function. An example of an external action is a function that initiates a different process to fulfill a transaction request. Coding techniques used for the SQL routine body can have a major impact on the runtime performance of the generated C program. By writing your routine to allow greater use of C code for assignments and comparisons, the overhead of an equivalent SQL statement is avoided. The following tips should help your routine generate more C code and fewer SQL statements. v Declare host variables as NOT NULL when possible. This saves the generated code from having to check and set the null value flags. Do not automatically set all variables to NOT NULL. When you specify NOT NULL, you need to also give a default value. If a variable is always used in the routine, a default value might help. However, if a variable is not always used, having a default value set may cause additional initialization overhead that is not needed. A default value is best for numeric values, where an additional database call to process the assignment of the default value is not needed. v Avoid character and date data types when possible. An example of this is a variable used as a flag with a value of 0, 1, 2, or 3. If this value is declared as a single character variable instead of an integer, it causes calls to the database engine that can be avoided. v Use integer instead of decimal with zero scale, especially when the variable is used as a counter. v Do not use temporary variables. Look at the following example:
IF M_days<=30 THEN SET I = M_days-7; SET J = 23 RETURN decimal(M_week_1 + ((M_month_1 - M_week_1)*I)/J,16,7); END IF
v Combine sequences of complex SET statements into one statement. This applies to statements where C code only cannot be generated because of CCSIDS or data types.
SET var1 = function1(var2); SET var2 = function2();
v Use IF () ELSE IF () ... ELSE ... constructs instead of IF (x AND y) to avoid unnecessary comparisons. v Do as much in SELECT statements as possible:
SELECT A INTO Y FROM B; SET Y=Y||X;
v Avoid doing character or date comparisons inside of loops when not necessary. In some cases the loop can be rewritten to move a comparison to precede the loop and have the comparison set an integer variable that is used within the loop. This causes the complex expression to be evaluated only one time. An integer comparison within the loop is more efficient since it can be done with generated C code.
SQL programming
189
v Avoid setting variables that might not be used. For example, if a variable is set outside of the an IF statement, be sure that the variable will actually be used in all instances of the IF statement. If not, then set the variable only in the portion of the IF statement that is it actually used. v Replace sections of code with a single SELECT statement when possible. Look at the following code snippet:
SET vnb_decimal = 4; cdecimal: FOR vdec AS cdec CURSOR FOR SELECT nb_decimal FROM K$FX_RULES WHERE first_currency=Pi_curl AND second_currency=P1_cur2 DO SET vnb_decimal=SMALLINT(cdecimal.nb_decimal); END FOR cdecimal; IF vnb_decimal IS NULL THEN SET vnb_decimal=4; END IF; SET vrate=ROUND(vrate1/vrate2,vnb_decimal); RETURN vrate;
This code snippet can be more efficient if rewritten in the following way:
RETURN( SELECT CASE WHEN MIN(nb_decimal) IS NULL THEN ROUND(Vrate1/Vrate2,4) ELSE ROUND(Vrate1/Vrate2,SMALLINT(MIN(nb_decimal))) END FROM K$FX_RULES WHERE first_currency=Pi_curl AND second_currency=Pi_cur2);
v C code can only be used for assignments and comparisons of character data if the CCSIDs of both operands are the same, if one of the CCSIDs is 65535, if the CCSID is not UTF8, and if truncation of character data is not possible. If the CCSID of the variable is not specified, the CCSID is not determined until the procedure is called. In this case, code must be generated to determine and compare the CCSID at runtime. If an alternate collating sequence is specified or if *JOBRUN is specified, C code cannot be generated for character comparisons. v Use the same data type, length and scale for numeric variables that are used together in assignments. C code can only be generated if truncation is not possible.
DECLARE v1, v2 INT; SET v1 = 100; SET v1 = v2;
190
You should also use nested compound statements to localize exception handling and cursors. If several specific handlers are specified, code is generated to check to see if the error occurred after each statement. Code is also generated to close cursors and process savepoints if an error occurs in a compound statement. In routines with a single compound statement with multiple handlers and multiple cursors, code is generated to process each handler and cursor after every SQL statement. If you scope the handlers and cursors to a nested compound statement, the handlers and cursors are only checked within the nested compound statement. In the following routine, code to check the SQLSTATE 22H11 error will only be generated for the statements within the lab2 compound statement. Specific checking for this error will not be done for any statements in the routine outside of the lab2 block. Code to check the SQLEXCEPTION error will be generated for all statements in both the lab1 and lab2 blocks. Likewise, error handling for closing cursor c1 will be limited to the statements in the lab2 block.
Lab1: BEGIN DECLARE var1 INT; DECLARE EXIT HANDLER FOR SQLEXCEPTION RETURN -3; lab2: BEGIN DECLARE EXIT HANDLER FOR SQLSTATE 22H11 RETURN -1; DECLARE c1 CURSOR FOR SELECT col1 FROM table1; OPEN c1; CLOSE c1; END lab2; END Lab1
Because redesigning a whole routine takes a lot of effort, examine routines that are showing up as key performance bottlenecks rather than looking at the application as a whole. More important than redesigning existing performance bottlenecks is to spend time during the design of the application thinking about the performance impacts of the design. Focusing on areas of the application that are expected to be high use areas and making sure that they are designed with performance in mind saves you from having to do a redesign of those areas later.
Large objects
A large object (LOB) is a string data type with a size ranging from 0 bytes to 2 GB (GB equals 1 073 741 824 bytes). The VARCHAR, VARGRAPHIC, and VARBINARY data types have a limit of 32 KB (where KB equals 1024 bytes) of storage. While this might be sufficient for small to medium-sized text data, applications often need to store large text documents. They might also need to store a wide variety of additional data types, such as audio, video, drawings, mixed text and graphics, and images. Some data types can store these data objects as strings of up to 2 GB. These data types are binary large objects (BLOBs), single-byte character large objects (CLOBs), and double-byte character large objects (DBCLOBs). Each table can have a large amount of associated LOB data. Although a single row that contains one or more LOB values cannot exceed 3.5 GB, a table can contain nearly 256 GB of LOB data. You can refer to and manipulate LOBs using host variables as you do any other data type. However, host variables use the programs storage that might not be large enough to hold LOB values, so you might need to manipulate large values in other ways. Locators are useful for identifying and manipulating a
SQL programming
191
large object value at the database server and for extracting pieces of the LOB value. File reference variables are useful for physically moving a large object value (or a large part of it) to and from the client.
192
Example: Using a locator to work with a CLOB value Suppose that you want your application program to retrieve a locator for a large object (LOB) value and then use the locator to extract data from the LOB value.
int main(int argc, char *argv[]) { #ifdef DB2MAC char * bufptr; #endif EXEC SQL BEGIN DECLARE SECTION; 1 char number[7]; long deptInfoBeginLoc; long deptInfoEndLoc; SQL TYPE IS CLOB_LOCATOR resume; SQL TYPE IS CLOB_LOCATOR deptBuffer; short lobind; char buffer[1000]=""; char userid[9]; char passwd[19];
SQL programming
193
EXEC SQL END DECLARE SECTION; printf( "Sample C program: LOBLOC\n" ); if (argc == 1) { EXEC SQL CONNECT TO sample; CHECKERR ("CONNECT TO SAMPLE"); } else if (argc == 3) { strcpy (userid, argv[1]); strcpy (passwd, argv[2]); EXEC SQL CONNECT TO sample USER :userid USING :passwd; CHECKERR ("CONNECT TO SAMPLE"); } else { printf ("\nUSAGE: lobloc [userid passwd]\n\n"); return 1; } /* endif */ /* Employee A10030 is not included in the following select, because the lobeval program manipulates the record for A10030 so that it is not compatible with lobloc */ EXEC SQL DECLARE c1 CURSOR FOR SELECT empno, resume FROM emp_resume WHERE resume_format=ascii AND empno <> A00130; EXEC SQL OPEN c1; CHECKERR ("OPEN CURSOR"); do { EXEC SQL FETCH c1 INTO :number, :resume :lobind; 2 if (SQLCODE != 0) break; if (lobind < 0) { printf ("NULL LOB indicated\n"); } else { /* EVALUATE the LOB LOCATOR */ /* Locate the beginning of "Department Information" section */ EXEC SQL VALUES (POSSTR(:resume, Department Information)) INTO :deptInfoBeginLoc; CHECKERR ("VALUES1"); /* Locate the beginning of "Education" section (end of "Dept.Info" */ EXEC SQL VALUES (POSSTR(:resume, Education)) INTO :deptInfoEndLoc; CHECKERR ("VALUES2"); /* Obtain ONLY the "Department Information" section by using SUBSTR */ EXEC SQL VALUES(SUBSTR(:resume, :deptInfoBeginLoc, :deptInfoEndLoc - :deptInfoBeginLoc)) INTO :deptBuffer; CHECKERR ("VALUES3"); /* Append the "Department Information" section to the :buffer var. */ EXEC SQL VALUES(:buffer || :deptBuffer) INTO :buffer; CHECKERR ("VALUES4"); } /* endif */ } while ( 1 ); #ifdef DB2MAC /* Need to convert the newline character for the Mac */ bufptr = &(buffer[0]); while ( *bufptr != \0 ) { if ( *bufptr == 0x0A ) *bufptr = 0x0D; bufptr++; } #endif
194
printf ("%s\n",buffer); EXEC SQL FREE LOCATOR :resume, :deptBuffer; 3 CHECKERR ("FREE LOCATOR"); EXEC SQL CLOSE c1; CHECKERR ("CLOSE CURSOR"); EXEC SQL CONNECT RESET; CHECKERR ("CONNECT RESET"); return 0; } /* end of program : LOBLOC.SQC */
Example: LOBLOC.SQB in COBOL: This example program written in COBOL uses a locator to retrieve a LOB value. Note: By using the code examples, you agree to the terms of the Code license and disclaimer information on page 300.
Identification Division. Program-ID. "lobloc". Data Division. Working-Storage Section. copy "sqlenv.cbl". copy "sql.cbl". copy "sqlca.cbl". EXEC SQL BEGIN DECLARE SECTION END-EXEC. 1 01 userid pic x(8). 01 passwd. 49 passwd-length pic s9(4) comp-5 value 0. 49 passwd-name pic x(18). 01 empnum pic x(6). 01 di-begin-loc pic s9(9) comp-5. 01 di-end-loc pic s9(9) comp-5. 01 resume USAGE IS SQL TYPE IS CLOB-LOCATOR. 01 di-buffer USAGE IS SQL TYPE IS CLOB-LOCATOR. 01 lobind pic s9(4) comp-5. 01 buffer USAGE IS SQL TYPE IS CLOB(1K). EXEC SQL END DECLARE SECTION END-EXEC. 77 errloc pic x(80).
Procedure Division. Main Section. display "Sample COBOL program: LOBLOC". * Get database connection information. display "Enter your user id (default none): " with no advancing. accept userid. if userid = spaces EXEC SQL CONNECT TO sample END-EXEC else display "Enter your password : " with no advancing accept passwd-name. * Passwords in a CONNECT * format with the length inspect passwd-name before initial " statement must be entered in a VARCHAR of the input string. tallying passwd-length for characters ".
SQL programming
195
EXEC SQL CONNECT TO sample USER :userid USING :passwd END-EXEC. move "CONNECT TO" to errloc. call "checkerr" using SQLCA errloc. * Employee A10030 is not included in the following select, because * the lobeval program manipulates the record for A10030 so that it is * not compatible with lobloc EXEC SQL DECLARE c1 CURSOR FOR SELECT empno, resume FROM emp_resume WHERE resume_format = ascii AND empno <> A00130 END-EXEC. EXEC SQL OPEN c1 END-EXEC. move "OPEN CURSOR" to errloc. call "checkerr" using SQLCA errloc. Move 0 to buffer-length. perform Fetch-Loop thru End-Fetch-Loop until SQLCODE not equal 0. * display contents of the buffer. display buffer-data(1:buffer-length). EXEC SQL FREE LOCATOR :resume, :di-buffer END-EXEC. 3 move "FREE LOCATOR" to errloc. call "checkerr" using SQLCA errloc. EXEC SQL CLOSE c1 END-EXEC. move "CLOSE CURSOR" to errloc. call "checkerr" using SQLCA errloc. EXEC SQL CONNECT RESET END-EXEC. move "CONNECT RESET" to errloc. call "checkerr" using SQLCA errloc. End-Main. go to End-Prog. Fetch-Loop Section. EXEC SQL FETCH c1 INTO :empnum, :resume :lobind 2 END-EXEC. if SQLCODE not equal 0 go to End-Fetch-Loop. * check to see if the host variable indicator returns NULL. if lobind less than 0 go to NULL-lob-indicated. * Value exists. Evaluate the LOB locator. * Locate the beginning of "Department Information" section. EXEC SQL VALUES (POSSTR(:resume, Department Information)) INTO :di-begin-loc END-EXEC. move "VALUES1" to errloc. call "checkerr" using SQLCA errloc. * Locate the beginning of "Education" section (end of Dept.Info) EXEC SQL VALUES (POSSTR(:resume, Education)) INTO :di-end-loc END-EXEC. move "VALUES2" to errloc. call "checkerr" using SQLCA errloc. subtract di-begin-loc from di-end-loc. * Obtain ONLY the "Department Information" section by using SUBSTR EXEC SQL VALUES (SUBSTR(:resume, :di-begin-loc,
196
:di-end-loc)) INTO :di-buffer END-EXEC. move "VALUES3" to errloc. call "checkerr" using SQLCA errloc. * Append the "Department Information" section to the :buffer var EXEC SQL VALUES (:buffer || :di-buffer) INTO :buffer END-EXEC. move "VALUES4" to errloc. call "checkerr" using SQLCA errloc. go to End-Fetch-Loop. NULL-lob-indicated. display "NULL LOB indicated". End-Fetch-Loop. exit. End-Prog. stop run.
197
v SQL_FILE_READ (Regular file) This option has a value of 2. This is a file that can be open, read, and closed. DB2 determines the length of the data in the file (in bytes) when opening the file. DB2 then returns the length through the data_length field of the file reference variable structure. The value for COBOL is SQL-FILE-READ. Values and options when using output file reference variables are as follows: v SQL_FILE_CREATE (New file) This option has a value of 8. This option creates a new file. Should the file already exist, an error message is returned. The value for COBOL is SQL-FILE-CREATE. v SQL_FILE_OVERWRITE (Overwrite file) This option has a value of 16. This option creates a new file if none already exists. If the file already exists, the new data overwrites the data in the file. The value for COBOL is SQL-FILE-OVERWRITE. v SQL_FILE_APPEND (Append file) This option has a value of 32. This option has the output appended to the file, if it exists. Otherwise, it creates a new file. The value for COBOL is SQL-FILE-APPEND. Note: If a LOB file reference variable is used in an OPEN statement, do not delete the file associated with the LOB file reference variable until the cursor is closed. Related concepts Large object locators on page 192 A large object (LOB) locator is a small, easily managed value that is used to refer to a much larger value. Integrated file system
COBOL CHECKERR is an external program named checkerr.cbl. Example: LOBFILE.SQC in C: This example program, written in C, extracts CLOB data from a table to an external file. Note: By using the code examples, you agree to the terms of the Code license and disclaimer information on page 300.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sql.h>
198
#include "util.h" EXEC SQL INCLUDE SQLCA; #define CHECKERR(CE_STR) if (check_error (CE_STR, &sqlca) != 0) return 1;
int main(int argc, char *argv[]) { EXEC SQL BEGIN DECLARE SECTION; 1 SQL TYPE IS CLOB_FILE resume; short lobind; char userid[9]; char passwd[19]; EXEC SQL END DECLARE SECTION; printf( "Sample C program: LOBFILE\n" ); if (argc == 1) { EXEC SQL CONNECT TO sample; CHECKERR ("CONNECT TO SAMPLE"); } else if (argc == 3) { strcpy (userid, argv[1]); strcpy (passwd, argv[2]); EXEC SQL CONNECT TO sample USER :userid USING :passwd; CHECKERR ("CONNECT TO SAMPLE"); } else { printf ("\nUSAGE: lobfile [userid passwd]\n\n"); return 1; } /* endif */ strcpy (resume.name, "RESUME.TXT"); 2 resume.name_length = strlen("RESUME.TXT"); resume.file_options = SQL_FILE_OVERWRITE; EXEC SQL SELECT resume INTO :resume :lobind FROM emp_resume WHERE resume_format=ascii AND empno=000130; 3
if (lobind < 0) { printf ("NULL LOB indicated \n"); } else { printf ("Resume for EMPNO 000130 is in file : RESUME.TXT\n"); } /* endif */ EXEC SQL CONNECT RESET; CHECKERR ("CONNECT RESET"); return 0; } /* end of program : LOBFILE.SQC */
Example: LOBFILE.SQB in COBOL: This example program, written in COBOL, extracts CLOB data from a table to an external file. Note: By using the code examples, you agree to the terms of the Code license and disclaimer information on page 300.
Identification Division. Program-ID. "lobfile". Data Division. Working-Storage Section. copy "sqlenv.cbl". copy "sql.cbl".
SQL programming
199
copy "sqlca.cbl". EXEC SQL BEGIN DECLARE SECTION END-EXEC. 1 01 userid pic x(8). 01 passwd. 49 passwd-length pic s9(4) comp-5 value 0. 49 passwd-name pic x(18). 01 resume USAGE IS SQL TYPE IS CLOB-FILE. 01 lobind pic s9(4) comp-5. EXEC SQL END DECLARE SECTION END-EXEC. 77 errloc pic x(80).
Procedure Division. Main Section. display "Sample COBOL program: LOBFILE". * Get database connection information. display "Enter your user id (default none): " with no advancing. accept userid. if userid = spaces EXEC SQL CONNECT TO sample END-EXEC else display "Enter your password : " with no advancing accept passwd-name. * Passwords in a CONNECT * format with the length inspect passwd-name before initial " statement must be entered in a VARCHAR of the input string. tallying passwd-length for characters ".
EXEC SQL CONNECT TO sample USER :userid USING :passwd END-EXEC. move "CONNECT TO" to errloc. call "checkerr" using SQLCA errloc. move "RESUME.TXT" to resume-NAME. move 10 to resume-NAME-LENGTH. move SQL-FILE-OVERWRITE to resume-FILE-OPTIONS. EXEC SQL SELECT resume INTO :resume :lobind 3 FROM emp_resume WHERE resume_format = ascii AND empno = 000130 END-EXEC. if lobind less than 0 go to NULL-LOB-indicated. display "Resume for EMPNO 000130 is in file : RESUME.TXT". go to End-Main. NULL-LOB-indicated. display "NULL LOB indicated". End-Main. EXEC SQL CONNECT RESET END-EXEC. move "CONNECT RESET" to errloc. call "checkerr" using SQLCA errloc. End-Prog. stop run. 2
200
v v v v
userid represents the directory for one of your users. dirname represents a subdirectory name of userid. filnam.1 can become the name of one of your documents that you want to insert into the table. clobtab is the name of the table with the CLOB data type.
strcpy(hv_text_file.name, "/home/userid/dirname/filnam.1"); hv_text_file.name_length = strlen("/home/userid/dirname/filnam.1"); hv_text_file.file_options = SQL_FILE_READ; /* this is a regular file */ EXEC SQL INSERT INTO CLOBTAB VALUES(:hv_text_file);
SQL programming
201
The first part of this layout is intended to 16 byte boundary align the pointer to the LOB data. The number of bytes in this area depends on the length of the columns that proceed the LOB column. Refer to the section above on the Display Layout of LOB Columns for an example of how the length of this first part is calculated.
Defining a UDT
You define a user-defined type (UDT) using the CREATE DISTINCT TYPE statement. For the CREATE DISTINCT TYPE statement, note that: 1. The name of the new UDT can be a qualified or an unqualified name. 2. The source type of the UDT is the type used by the system to internally represent the UDT. For this reason, it must be a built-in data type. Previously defined UDTs cannot be used as source types of other UDTs. As part of a UDT definition, the system always generates cast functions to: v Cast from the UDT to the source type, using the standard name of the source type. For example, if you create a distinct type based on FLOAT, the cast function called DOUBLE is created.
202
v Cast from the source type to the UDT. These functions are important for the manipulation of UDTs in queries. The function path is used to resolve any references to an unqualified type name or function, except if the type name or function is the main object of a CREATE, DROP, or COMMENT ON statement. Related reference Using qualified function references on page 174 If you use a qualified function reference, you restrict the search for a matching function to the specified schema. CREATE DISTINCT TYPE Example: Money: Suppose that you are writing applications that handle different currencies and want to ensure that DB2 does not allow these currencies to be compared or manipulated directly with one another in queries. Remember that conversions are necessary whenever you want to compare values of different currencies. So you define as many UDTs as you need; one for each currency that you may need to represent:
CREATE DISTINCT TYPE US_DOLLAR AS DECIMAL (9,2) CREATE DISTINCT TYPE CANADIAN_DOLLAR AS DECIMAL (9,2) CREATE DISTINCT TYPE EURO AS DECIMAL (9,2)
Example: Resum: Suppose that you want to keep the application forms that are filled out by applicants to your company in a table, and that you are going to use functions to extract information from these forms. Because these functions cannot be applied to regular character strings (because they are certainly not able to find the information they are supposed to return), you define a UDT to represent the filled forms:
CREATE DISTINCT TYPE PERSONAL.APPLICATION_FORM AS CLOB(32K)
203
The UDTs in the preceding examples are created with the same CREATE DISTINCT TYPE statements in Example: Money on page 203. Note that the preceding examples use check constraints. Example: Application forms: Suppose that you need to define a table to keep the forms that are filled out by applicants. Create the table as follows:
CREATE TABLE APPLICATIONS (ID INTEGER, NAME VARCHAR (30), APPLICATION_DATE DATE, FORM PERSONAL.APPLICATION_FORM)
You have fully qualified the UDT name because its qualifier is not the same as your authorization ID and you have not changed the default function path. Remember that whenever type and function names are not fully qualified, DB2 searches through the schemas listed in the current function path and looks for a type or function name matching the given unqualified name.
Manipulating UDTs
Strong typing is an important concept associated with user-defined types (UDTs). Strong typing guarantees that only functions and operators defined on a UDT can be applied to its instances. Strong typing is important to ensure that the instances of your UDTs are correct. For example, if you have defined a function to convert US dollars to Canadian dollars according to the current exchange rate, you do not want this same function to be used to convert Euros to Canadian dollars because it will certainly return the wrong amount. As a consequence of strong typing, DB2 does not allow you to write queries that compare, for example, UDT instances with instances of the UDT source type. For the same reason, DB2 will not let you apply functions defined on other types to UDTs. If you want to compare instances of UDTs with instances of another type, you need to cast the instances of one or the other type. In the same sense, you need to cast the UDT instance to the type of the parameter of a function that is not defined on a UDT if you want to apply this function to a UDT instance.
Because you cannot compare U.S. dollars with instances of the source type of U.S. dollars (that is, DECIMAL) directly, you have used the cast function provided by DB2 to cast from DECIMAL to U.S. dollars. You can also use the other cast function provided by DB2 (that is, the one to cast from U.S. dollars to DECIMAL) and cast the column total to DECIMAL. Either way you decide to cast, from or to the UDT, you can use the cast specification notation to perform the casting, or the functional notation. You might have written the above query as:
204
SELECT PRODUCT_ITEM FROM US_SALES WHERE TOTAL > CAST (100000 AS us_dollar) AND MONTH = 7 AND YEAR = 1998
Example: Casting between UDTs: Suppose that you want to define a user-defined function (UDF) that converts Canadian dollars to U.S. dollars. You can obtain the current exchange rate from a file managed outside of DB2. Then define a UDF that obtains a value in Canadian dollars, accesses the exchange rate file, and returns the corresponding amount in U.S. dollars. At first glance, such a UDF may appear easy to write. However, not all C compilers support DECIMAL values. The UDTs representing different currencies have been defined as DECIMAL. Your UDF will need to receive and return DOUBLE values, since this is the only data type provided by C that allows the representation of a DECIMAL value without losing the decimal precision. Your UDF should be defined as follows:
CREATE FUNCTION CDN_TO_US_DOUBLE(DOUBLE) RETURNS DOUBLE EXTERNAL NAME MYLIB/CURRENCIES(C_CDN_US) LANGUAGE C PARAMETER STYLE DB2SQL NO SQL NOT DETERMINISTIC
The exchange rate between Canadian and U.S. dollars may change between two invocations of the UDF, so you declare it as NOT DETERMINISTIC. The question now is, how do you pass Canadian dollars to this UDF and get U.S. dollars from it? The Canadian dollars must be cast to DECIMAL values. The DECIMAL values must be cast to DOUBLE. You also need to have the returned DOUBLE value cast to DECIMAL and the DECIMAL value cast to U.S. dollars. Such casts are performed automatically by DB2 anytime you define sourced UDFs, whose parameter and return type do not exactly match the parameter and return type of the source function. Therefore, you need to define two sourced UDFs. The first brings the DOUBLE values to a DECIMAL representation. The second brings the DECIMAL values to the UDT. Define the following:
CREATE FUNCTION CDN_TO_US_DEC (DECIMAL(9,2)) RETURNS DECIMAL(9,2) SOURCE CDN_TO_US_DOUBLE (DOUBLE) CREATE FUNCTION US_DOLLAR (CANADIAN_DOLLAR) RETURNS US_DOLLAR SOURCE CDN_TO_US_DEC (DECIMAL())
Note that an invocation of the US_DOLLAR function as in US_DOLLAR(C1), where C1 is a column whose type is Canadian dollars, has the same effect as invoking:
US_DOLLAR (DECIMAL(CDN_TO_US_DOUBLE (DOUBLE (DECIMAL (C1)))))
That is, C1 (in Canadian dollars) is cast to decimal which in turn is cast to a double value that is passed to the CDN_TO_US_DOUBLE function. This function accesses the exchange rate file and returns a double value (representing the amount in U.S. dollars) that is cast to decimal, and then to U.S. dollars. A function to convert Euros to U.S. dollars is similar to the example above:
CREATE FUNCTION EURO_TO_US_DOUBLE(DOUBLE) RETURNS DOUBLE EXTERNAL NAME MYLIB/CURRENCIES(C_EURO_US) LANGUAGE C
SQL programming
205
PARAMETER STYLE DB2SQL NO SQL NOT DETERMINISTIC CREATE FUNCTION EURO_TO_US_DEC (DECIMAL(9,2)) RETURNS DECIMAL(9,2) SOURCE EURO_TO_US_DOUBLE(DOUBLE) CREATE FUNCTION US_DOLLAR(EURO) RETURNS US_DOLLAR SOURCE EURO_TO_US_DEC (DECIMAL())
Example: Comparisons involving UDTs: Suppose that you want to know which products had higher sales in the U.S. than in Canada and Germany for the month of March 2003. Issue the following SELECT statement:
SELECT US.PRODUCT_ITEM, US.TOTAL FROM US_SALES AS US, CANADIAN_SALES AS CDN, GERMAN_SALES AS GERMAN WHERE US.PRODUCT_ITEM = CDN.PRODUCT_ITEM AND US.PRODUCT_ITEM = GERMAN.PRODUCT_ITEM AND US.TOTAL > US_DOLLAR (CDN.TOTAL) AND US.TOTAL > US_DOLLAR (GERMAN.TOTAL) AND US.MONTH = 3 AND US.YEAR = 2003 AND CDN.MONTH = 3 AND CDN.YEAR = 2003 AND GERMAN.MONTH = 3 AND GERMAN.YEAR = 2003
Because you cannot directly compare U.S. dollars with Canadian dollars or Euros, you use the UDF to cast the amount in Canadian dollars to U.S. dollars, and the UDF to cast the amount in Euros to U.S. dollars. You cannot cast them all to DECIMAL and compare the converted DECIMAL values because the amounts are not monetarily comparable as they are not in the same currency. Example: Sourced UDFs involving UDTs: Suppose that you have defined a sourced user-defined function (UDF) on the built-in SUM function to support SUM on Euros. The function statement is as follows:
CREATE FUNCTION SUM (EURO) RETURNS EURO SOURCE SYSIBM.SUM (DECIMAL())
You want to know the total of sales in Germany for each product in the year of 2004. You want to obtain the total sales in U.S. dollars:
SELECT PRODUCT_ITEM, US_DOLLAR (SUM (TOTAL)) FROM GERMAN_SALES WHERE YEAR = 2004 GROUP BY PRODUCT_ITEM
You cannot write SUM (US_DOLLAR (TOTAL)), unless you had defined a SUM function on U.S. dollar in a manner similar to the above. Related reference Example: Assignments involving different UDTs on page 207 Suppose that you have defined these sourced user-defined functions (UDFs) on the built-in SUM function to support SUM on U.S. and Canadian dollars.
206
Example: Assignments involving UDTs: Suppose that you want to store the form that is filled out by a new applicant into the database. You have defined a host variable containing the character string value used to represent the filled form:
EXEC SQL BEGIN DECLARE SECTION; SQL TYPE IS CLOB(32K) hv_form; EXEC SQL END DECLARE SECTION; /* Code to fill hv_form */ INSERT INTO APPLICATIONS VALUES (134523, Peter Holland, CURRENT DATE, :hv_form)
You do not explicitly invoke the cast function to convert the character string to the UDT personal.application_form. This is because DB2 allows you to assign instances of the source type of a UDT to targets having that UDT. Related reference Example: Assignments in dynamic SQL If you want to store the application form using dynamic SQL, you can use parameter markers. Example: Assignments in dynamic SQL: If you want to store the application form using dynamic SQL, you can use parameter markers. The statement is as follows:
EXEC SQL BEGIN DECLARE SECTION; long id; char name[30]; SQL TYPE IS CLOB(32K) form; char command[80]; EXEC SQL END DECLARE SECTION; /* Code to fill host variables */ strcpy(command,"INSERT INTO APPLICATIONS VALUES"); strcat(command,"(?, ?, CURRENT DATE, ?)"); EXEC SQL PREPARE APP_INSERT FROM :command; EXEC SQL EXECUTE APP_INSERT USING :id, :name, :form;
You made use of DB2s cast specification to tell DB2 that the type of the parameter marker is CLOB(32K), a type that is assignable to the UDT column. Remember that you cannot declare a host variable of a UDT type, since host languages do not support UDTs. Therefore, you cannot specify that the type of a parameter marker is a UDT. Related reference Example: Assignments involving UDTs Suppose that you want to store the form that is filled out by a new applicant into the database. Example: Assignments involving different UDTs: Suppose that you have defined these sourced user-defined functions (UDFs) on the built-in SUM function to support SUM on U.S. and Canadian dollars.
CREATE FUNCTION SUM (CANADIAN_DOLLAR) RETURNS CANADIAN_DOLLAR SOURCE SYSIBM.SUM (DECIMAL())
SQL programming
207
Now suppose your supervisor requests that you maintain the annual total sales in U.S. dollars of each product and in each country, in separate tables:
CREATE TABLE US_SALES_04 (PRODUCT_ITEM INTEGER, TOTAL US_DOLLAR) CREATE TABLE GERMAN_SALES_04 (PRODUCT_ITEM INTEGER, TOTAL US_DOLLAR) CREATE TABLE CANADIAN_SALES_04 (PRODUCT_ITEM INTEGER, TOTAL US_DOLLAR) INSERT INTO US_SALES_04 SELECT PRODUCT_ITEM, SUM (TOTAL) FROM US_SALES WHERE YEAR = 2004 GROUP BY PRODUCT_ITEM INSERT INTO GERMAN_SALES_04 SELECT PRODUCT_ITEM, US_DOLLAR (SUM (TOTAL)) FROM GERMAN_SALES WHERE YEAR = 2004 GROUP BY PRODUCT_ITEM INSERT INTO CANADIAN_SALES_04 SELECT PRODUCT_ITEM, US_DOLLAR (SUM (TOTAL)) FROM CANADIAN_SALES WHERE YEAR = 2004 GROUP BY PRODUCT_ITEM
You explicitly cast the amounts in Canadian dollars and Euros to U.S. dollars since different UDTs are not directly assignable to each other. You cannot use the cast specification syntax because UDTs can only be cast to their own source type. Related reference Example: Sourced UDFs involving UDTs on page 206 Suppose that you have defined a sourced user-defined function (UDF) on the built-in SUM function to support SUM on Euros. Example: Using UDTs in UNION: Suppose that you want to provide your U.S. users with a query to show the sales of every product of your company. The SELECT statement is as follows:
SELECT PRODUCT_ITEM, MONTH, YEAR, TOTAL FROM US_SALES UNION SELECT PRODUCT_ITEM, MONTH, YEAR, US_DOLLAR (TOTAL) FROM CANADIAN_SALES UNION SELECT PRODUCT_ITEM, MONTH, YEAR, US_DOLLAR (TOTAL) FROM GERMAN_SALES
208
You cast Canadian dollars to U.S. dollars and Euros to U.S. dollars because UDTs are union compatible only with the same UDT. You must use the functional notation to cast between UDTs since the cast specification only allows you to cast between UDTs and their source types.
209
NO EXTERNAL ACTION CREATE FUNCTION CONTAINS (E_MAIL, VARCHAR (200)) RETURNS INTEGER EXTERNAL NAME LIB/PGM(CONTAINS) LANGUAGE C PARAMETER STYLE DB2SQL NO SQL DETERMINISTIC NO EXTERNAL ACTION CREATE TABLE ELECTRONIC_MAIL (ARRIVAL_TIMESTAMP TIMESTAMP, MESSAGE E_MAIL)
All the function provided by DB2 LOB support is applicable to UDTs whose source type are LOBs. Therefore, you have used LOB file reference variables to assign the contents of the file into the UDT column. You have not used the cast function to convert values of BLOB type into your e-mail type. This is because DB2 allows you to assign values of the source type of a distinct type to targets of the distinct type.
You have used the UDFs defined on the UDT in this SQL query since they are the only means to manipulate the UDT. In this sense, your UDT e-mail is completely encapsulated. Its internal representation and structure are hidden and can only be manipulated by the defined UDFs. These UDFs know how to interpret the data without the need to expose its representation. Suppose you need to know the details of all the e-mail your company received in 1994 that had to do with the performance of your products in the marketplace.
SELECT SENDER (MESSAGE), SENDING_DATE (MESSAGE), SUBJECT (MESSAGE) FROM ELECTRONIC_MAIL WHERE CONTAINS (MESSAGE, "performance" AND "products" AND "marketplace") = 1
210
You have used the contains UDF that is capable of analyzing the contents of the message searching for relevant keywords or synonyms.
Because your host variable is of type BLOB locator (the source type of the UDT), you have explicitly converted the BLOB locator to your UDT, whenever it was used as an argument of a UDF defined on the UDT.
Using DataLinks
The DataLink data type is one of the basic building blocks for extending the types of data that can be stored in database files. The idea of a DataLink is that the actual data stored in the column is only a pointer to the object. This object can be anything, an image file, a voice recording, a text file, and so on. The method used for resolving to the object is to store a Uniform Resource Locator (URL). This means that a row in a table can be used to contain information about the object in traditional data types, and the object itself can be referenced using the DataLink data type. The user can use SQL scalar functions to get back the path to the object and the server on which the object is stored (see Built-in functions in the SQL Reference). With the DataLink data type, there is a fairly loose relationship between the row and the object. For instance, deleting a row will sever the relationship to the object referenced by the DataLink, but the object itself might not be deleted. A table created with a DataLink column can be used to hold information about an object, without actually containing the object itself. This concept gives the user much more flexibility in the types of data that can be managed using a table. If, for instance, the user has thousands of video clips stored in the integrated file system of their server, they may want to use an SQL table to contain information about these video clips. But since the user already has the objects stored in a directory, they only want the SQL table to contain references to the objects, not the actual bytes of storage. A good solution is to use DataLinks. The SQL table uses traditional SQL data types to contain information about each clip, such as title, length, date, and so on. But the clip itself is referenced using a DataLink column. Each row in the
SQL programming
211
table stores a URL for the object and an optional comment. Then an application that is working with the clips can retrieve the URL using SQL interfaces, and then use a browser or other playback software to work with the URL and display the video clip. There are several advantages of using this technique: v The integrated file system can store any type of stream file. v The integrated file system can store extremely large objects, that does not fit into a character column, or perhaps even a LOB column. v The hierarchical nature of the integrated file system is well-suited to organizing and working with the stream file objects. v By leaving the bytes of the object outside the database and in the integrated file system, applications can achieve better performance by allowing the SQL runtime engine to handle queries and reports, and allowing the file system to handle streaming of video, displaying images, text, and so on. Using DataLinks also gives control over the objects while they are in linked status. A DataLink column can be created such that the referenced object cannot be deleted, moved, or renamed while there is a row in the SQL table that references that object. This object are considered linked. Once the row containing that reference is deleted, the object is unlinked. To understand this concept fully, one should know the levels of control that can be specified when creating a DataLink column. Related reference Data types
212
The ownership of the object is changed to a special system-supplied user profile. During the time that the object is linked, the only access to the object is by obtaining the URL from the SQL table that has the object linked. This is handled by using a special access token that is appended to the URL returned by SQL. Without the access token, all attempts to access the object will fail with an authority violation. If the URL with the access token is retrieved from the SQL table by normal means (FETCH, SELECT INTO, and so on.) the file system filter will validate the access token and allow the access to the object. This option provides the control of preventing updates to the linked object for users trying to access the object by direct means. Since the only access to the object is by obtaining the access token from an SQL operation, an administrator can effectively control access to the linked objects by using the database permissions to the SQL table that contains the DataLink column.
213
help text and syntax diagrams. For the most commonly used functions, CL commands have also been provided. Using the CL commands, most or all of the DLFM configuration can be accomplished without using the script interface. Depending on your preferences, you can choose to use either the script commands from the QSH command entry screen or the CL commands from the CL command entry screen. Since these functions are meant for a system administrator or a database administrator, they all require the *IOSYSCFG special authority.
Adding a prefix
A prefix is a path or directory that will contain objects to be linked. When setting up the Data Links File Manager (DLFM) on a system, the administrator must add any prefixes that will be used for DataLinks. The script command dfmadmin -add_prefix is used to add prefixes. The CL command to add prefixes is Add Prefix to DataLink File Manager (ADDPFXDLFM) command. For instance, on server TESTSYS1, there is a directory called /mydir/datalinks/ that contains the objects that will be linked. The administrator uses the command ADDPFXDLFM PREFIX(/mydir/datalinks/) to add the prefix. The following links for URLs are valid because their paths have valid prefixes:
http://TESTSYS1/mydir/datalinks/videos/file1.mpg or file://TESTSYS1/mydir/datalinks/text/story1.txt
It is also possible to remove a prefix using the script command dfmadmin -del_prefix. This is not a commonly used function since it can only be run if there are no linked objects anywhere in the directory structure contained within the prefix name. Notes: 1. The following directories, or any of their subdirectories, should not be used as prefixes for DataLinks: v /QIBM v /QReclaim v /QSR v /QFPNWSSTG 2. Additionally, common base directories such as the following should not be used unless the prefix is a subdirectory within one of the base directories: v /home v /dev v /bin v /etc v /tmp v /usr v /lib
214
-add_db and the CL command is Add Host Database to DataLink File Manager (ADDHDBDLFM) command. This function also requires that the libraries containing the SQL tables also be registered. For instance, on server TESTSYS1 where you have already added the /mydir/datalinks/ prefix, you want SQL tables on the local system in either TESTDB or PRODDB library to be allowed to link objects on this server. Use the following:
ADDHDBDLFM HOSTDBLIB((TESTDB) (PRODDB)) HOSTDB(TESTSYS1)
Once the DLFM has been started, and the prefixes and host database names have been registered, you can begin linking objects in the file system.
Using a cursor
When SQL runs a SELECT statement, the resulting rows comprise the result table. A cursor provides a way to access a result table. It is used within an SQL program to maintain a position in the result table. SQL uses a cursor to work with the rows in the result table and to make them available to your program. Your program can have several cursors, although each must have a unique name. Statements related to using a cursor include the following: v A DECLARE CURSOR statement to define and name the cursor and specify the rows to be retrieved with the embedded select statement. v OPEN and CLOSE statements to open and close the cursor for use within the program. The cursor must be opened before any rows can be retrieved. v A FETCH statement to retrieve rows from the cursors result table or to position the cursor on another row. v An UPDATE ... WHERE CURRENT OF statement to update the current row of a cursor. v A DELETE ... WHERE CURRENT OF statement to delete the current row of a cursor. Related reference Updating data as it is retrieved from a table on page 86 You can update rows of data as you retrieve them by using a cursor. CLOSE DECLARE CURSOR DELETE FETCH UPDATE
Types of cursors
SQL supports serial and scrollable cursors. The type of cursor determines the positioning methods that can be used with the cursor.
Serial cursor
A serial cursor is one defined without the SCROLL keyword. For a serial cursor, each row of the result table can be fetched only once per OPEN of the cursor. When the cursor is opened, it is positioned before the first row in the result table. When a FETCH is issued, the
SQL programming
215
cursor is moved to the next row in the result table. That row is then the current row. If host variables are specified (with the INTO clause on the FETCH statement), SQL moves the current rows contents into your programs host variables. This sequence is repeated each time a FETCH statement is issued until the end-of-data (SQLCODE = 100) is reached. When you reach the end-of-data, close the cursor. You cannot access any rows in the result table after you reach the end-of-data. To use a serial cursor again, you must first close the cursor and then re-issue the OPEN statement. You can never back up using a serial cursor.
Scrollable cursor
For a scrollable cursor, the rows of the result table can be fetched many times. The cursor is moved through the result table based on the position option specified on the FETCH statement. When the cursor is opened, it is positioned before the first row in the result table. When a FETCH is issued, the cursor is positioned to the row in the result table that is specified by the position option. That row is then the current row. If host variables are specified (with the INTO clause on the FETCH statement), SQL moves the current rows contents into your programs host variables. Host variables cannot be specified for the BEFORE and AFTER position options. This sequence is repeated each time a FETCH statement is issued. The cursor does not need to be closed when an end-of-data or beginning-of-data condition occurs. The position options enable the program to continue fetching rows from the table. The following scroll options are used to position the cursor when issuing a FETCH statement. These positions are relative to the current cursor location in the result table.
NEXT PRIOR FIRST LAST BEFORE AFTER CURRENT RELATIVE n Positions the cursor on the next row. This is the default if no position is specified. Positions the cursor on the previous row. Positions the cursor on the first row. Positions the cursor on the last row. Positions the cursor before the first row. Positions the cursor after the last row. Does not change the cursor position. Evaluates a host variable or integer n in relationship to the cursors current position. For example, if n is -1, the cursor is positioned on the previous row of the result table. If n is +3, the cursor is positioned three rows after the current row.
For a scrollable cursor, the end of the table can be determined by the following:
FETCH AFTER FROM C1
Once the cursor is positioned at the end of the table, the program can use the PRIOR or RELATIVE scroll options to position and fetch data starting from the end of the table.
216
For the serial cursor example, the program processes all of the rows from the table, updating the job for all members of department D11 and deleting the records of employees from the other departments.
Table 41. A serial cursor example Serial cursor SQL statement EXEC SQL DECLARE THISEMP CURSOR FOR SELECT EMPNO, LASTNAME, WORKDEPT, JOB FROM CORPDATA.EMPLOYEE FOR UPDATE OF JOB END-EXEC. Step 2: Opening the cursor on page 219. EXEC SQL OPEN THISEMP END-EXEC. EXEC SQL WHENEVER NOT FOUND GO TO CLOSE-THISEMP END-EXEC. EXEC SQL FETCH THISEMP INTO :EMP-NUM, :NAME2, :DEPT, :JOB-CODE END-EXEC. ... for all employees in department D11, update the JOB value: EXEC SQL UPDATE CORPDATA.EMPLOYEE SET JOB = :NEW-CODE WHERE CURRENT OF THISEMP END-EXEC. ... then print the row. ... for other employees, delete the row: EXEC SQL DELETE FROM CORPDATA.EMPLOYEE WHERE CURRENT OF THISEMP END-EXEC. Branch back to fetch and process the next row. Step 6: Closing the cursor on page 222. CLOSE-THISEMP. EXEC SQL CLOSE THISEMP END-EXEC. Step 5b: Deleting the current row on page 221. Step 3: Specifying what to do when the end of data is reached on page 220. Described in section Step 1: Defining the cursor on page 218.
SQL programming
217
For the scrollable cursor example, the program uses the RELATIVE position option to obtain a representative sample of salaries from department D11.
Table 42. A scrollable cursor example Scrollable cursor SQL statement EXEC SQL DECLARE THISEMP DYNAMIC SCROLL CURSOR FOR SELECT EMPNO, LASTNAME, SALARY FROM CORPDATA.EMPLOYEE WHERE WORKDEPT = D11 END-EXEC. Step 2: Opening the cursor on page 219. EXEC SQL OPEN THISEMP END-EXEC. EXEC SQL WHENEVER NOT FOUND GO TO CLOSE-THISEMP END-EXEC. ...initialize program summation salary variable EXEC SQL FETCH RELATIVE 3 FROM THISEMP INTO :EMP-NUM, :NAME2, :JOB-CODE END-EXEC. ...add the current salary to program summation salary ...branch back to fetch and process the next row. ...calculate the average salary Step 6: Closing the cursor on page 222. CLOSE-THISEMP. EXEC SQL CLOSE THISEMP END-EXEC. Step 3: Specifying what to do when the end of data is reached on page 220. Described in section Step 1: Defining the cursor.
Step 1: Defining the cursor: To define a cursor to access the result table, use the DECLARE CURSOR statement. The DECLARE CURSOR statement names a cursor and specifies a select-statement. The select-statement defines a set of rows that, conceptually, make up the result table. For a serial cursor, the statement looks like this (the FOR UPDATE OF clause is optional):
EXEC SQL DECLARE cursor-name CURSOR FOR SELECT column-1, column-2 ,... FROM table-name , ... FOR UPDATE OF column-2 ,... END-EXEC.
218
For a scrollable cursor, the statement looks like this (the WHERE clause is optional):
EXEC SQL DECLARE cursor-name SCROLL CURSOR FOR SELECT column-1, column-2 ,... FROM table-name ,... WHERE column-1 = expression ... END-EXEC.
The select-statements shown here are rather simple. However, you can code several other types of clauses in a select-statement within a DECLARE CURSOR statement for a serial and a scrollable cursor. If you intend to update any columns in any or all of the rows of the identified table (the table named in the FROM clause), include the FOR UPDATE OF clause. It names each column you intend to update. If you do not specify the names of columns, and you specify either the ORDER BY clause or FOR READ ONLY clause, a negative SQLCODE is returned if an update is attempted. If you do not specify the FOR UPDATE OF clause, the FOR READ ONLY clause, the ORDER BY clause, and the result table is not read-only and the cursor is not scrollable, you can update any of the columns of the specified table. You can update a column of the identified table even though it is not part of the result table. In this case, you do not need to name the column in the SELECT statement. When the cursor retrieves a row (using FETCH) that contains a column value you want to update, you can use UPDATE ... WHERE CURRENT OF to update the row. For example, assume that each row of the result table includes the EMPNO, LASTNAME, and WORKDEPT columns from the CORPDATA.EMPLOYEE table. If you want to update the JOB column (one of the columns in each row of the CORPDATA.EMPLOYEE table), the DECLARE CURSOR statement should include FOR UPDATE OF JOB ... even though JOB is omitted from the SELECT statement. The result table and cursor are read-only if any of the following are true: v The first FROM clause identifies more than one table or view. v v v v v v v | v v v v v v v The first FROM clause identifies a read-only view. The first FROM clause identifies a user-defined table function. The first SELECT clause specifies the keyword DISTINCT. The outer subselect contains a GROUP BY clause. The outer subselect contains a HAVING clause. The first SELECT clause contains a column function. The select-statement contains a subquery such that the base object of the outer subselect and of the subquery is the same table. The select-statement contains a UNION, UNION ALL, EXCEPT, or INTERSECT operator. The select-statement contains an ORDER BY clause, and the SENSITIVE keyword and FOR UPDATE OF clause are not specified. The select-statement includes a FOR READ ONLY clause. The SCROLL keyword is specified, a FOR UPDATE OF clause is not specified, and the SENSITIVE keyword is not specified. The select-list includes a DataLink column and a FOR UPDATE OF clause is not specified. The first subselect requires a temporary result table. The select-statement includes a FETCH FIRST n ROWS ONLY.
Step 2: Opening the cursor: To begin processing the rows of the result table, issue the OPEN statement.
SQL programming
219
When your program issues the OPEN statement, SQL processes the select-statement within the DECLARE CURSOR statement to identify a set of rows, called a result table, using the current value of any host variables specified in the select-statement. A result table can contain zero, one, or many rows, depending on the extent to which the search condition is satisfied. The OPEN statement looks like this:
EXEC SQL OPEN cursor-name END-EXEC.
Step 3: Specifying what to do when the end of data is reached: The end-of-data condition occurs when the FETCH statement has retrieved the last row in the result table and your program issues a subsequent FETCH statement. To find out when the end of the result table is reached, test the SQLCODE field for a value of 100 or test the SQLSTATE field for a value of 02000 (that is, end of data). For example:
... IF SQLCODE =100 GO TO DATA-NOT-FOUND. or IF SQLSTATE =02000 GO TO DATA-NOT-FOUND.
An alternative to this technique is to code the WHENEVER statement. Using WHENEVER NOT FOUND can result in a branch to another part of your program, where a CLOSE statement is issued. The WHENEVER statement looks like this:
EXEC SQL WHENEVER NOT FOUND GO TO symbolic-address END-EXEC.
Your program should anticipate an end-of-data condition whenever a cursor is used to fetch a row, and should be prepared to handle this situation when it occurs. When you are using a serial cursor and the end of data is reached, every subsequent FETCH statement returns the end-of-data condition. You cannot position the cursor on rows that are already processed. The CLOSE statement is the only operation that can be performed on the cursor. When you are using a scrollable cursor and the end of data is reached, the result table can still process more data. You can position the cursor anywhere in the result table using a combination of the position options. You do not need to close the cursor when the end of data is reached. Step 4: Retrieving a row using a cursor: To move the contents of a selected row into the host variables of your program, use the FETCH statement. The SELECT statement within the DECLARE CURSOR statement identifies rows that contain the column values your program wants. However, SQL does not retrieve any data for your application program until the FETCH statement is issued. When your program issues the FETCH statement, SQL uses the current cursor position as a starting point to locate the requested row in the result table. This changes that row to the current row. If an INTO clause was specified, SQL moves the current rows contents into your programs host variables. This sequence is repeated each time the FETCH statement is issued.
220
SQL maintains the position of the current row (that is, the cursor points to the current row) until the next FETCH statement for the cursor is issued. The UPDATE statement does not change the position of the current row within the result table, although the DELETE statement does. The serial cursor FETCH statement looks like this:
EXEC SQL FETCH cursor-name INTO :host variable-1[, :host variable-2] ... END-EXEC.
Step 5a: Updating the current row: When your program has positioned the cursor on a row, you can update the row by using the UPDATE statement with the WHERE CURRENT OF clause. The WHERE CURRENT OF clause specifies a cursor that points to the row that you want to update. The UPDATE ... WHERE CURRENT OF statement looks like this:
EXEC SQL UPDATE table-name SET column-1 = value [, column-2 = value] ... WHERE CURRENT OF cursor-name END-EXEC.
When used with a cursor, the UPDATE statement: v Updates only one rowthe current row v Identifies a cursor that points to the row to be updated v Requires that the columns updated be named previously in the FOR UPDATE OF clause of the DECLARE CURSOR statement, if an ORDER BY clause was also specified After you update a row, the cursors position remains on that row (that is, the current row of the cursor does not change) until you issue a FETCH statement for the next row. Step 5b: Deleting the current row: When your program has retrieved the current row, you can delete the row by using the DELETE statement with the WHERE CURRENT OF clause. The WHERE CURRENT OF clause specifies a cursor that points to the row that you want to delete. The DELETE ... WHERE CURRENT OF statement looks like this:
EXEC SQL DELETE FROM table-name WHERE CURRENT OF cursor-name END-EXEC.
When used with a cursor, the DELETE statement: v Deletes only one rowthe current row v Uses the WHERE CURRENT OF clause to identify a cursor that points to the row to be deleted After you delete a row, you cannot update or delete another row using that cursor until you issue a FETCH statement to position the cursor.
SQL programming
221
You can use the DELETE statement to delete all rows that meet a specific search condition. You can also use the FETCH and DELETE ... WHERE CURRENT OF statements when you want to obtain a copy of the row, examine it, and then delete it. Step 6: Closing the cursor: If you have processed the rows of a result table using a serial cursor, and you want to use the cursor again, issue a CLOSE statement to close the cursor before opening it again. The statement looks like this:
EXEC SQL CLOSE cursor-name END-EXEC.
If you processed the rows of a result table and you do not want to use the cursor again, you can let the system close the cursor. The system automatically closes the cursor when: v A COMMIT without HOLD statement is issued and the cursor is not declared using the WITH HOLD clause. v A ROLLBACK without HOLD statement is issued. v The job ends. v The activation group ends and CLOSQLCSR(*ENDACTGRP) was specified on the precompile. v The first SQL program in the call stack ends and neither CLOSQLCSR(*ENDJOB) or CLOSQLCSR(*ENDACTGRP) was specified when the program was precompiled. v The connection to the application server is ended using the DISCONNECT statement. v The connection to the application server was released and a successful COMMIT occurred. v An *RUW CONNECT occurred. Because an open cursor still holds locks on referred-to-tables or views, you should explicitly close any open cursors as soon as they are no longer needed.
222
v SQLERRD5 contains an indication that the last row in the table was fetched. It can be used to detect the end-of-data condition in the table being fetched when the cursor does not have immediate sensitivity to updates. Cursors which do have immediate sensitivity to updates should continue fetching until an SQLCODE +100 is received to detect an end-of-data condition. Related concepts Embedded SQL programming Multiple-row FETCH using a host structure array: To use the multiple-row FETCH statement with the host structure array, the application must define a host structure array that can be used by SQL. Each language has its own conventions and rules for defining a host structure array. Host structure arrays can be defined by using variable declarations or by using compiler directives to retrieve External File Descriptions (such as the COBOL COPY directive). The host structure array consists of an array of structures. Each structure corresponds to one row of the result table. The first structure in the array corresponds to the first row, the second structure in the array corresponds to the second row, and so on. SQL determines the attributes of elementary items in the host structure array based on the declaration of the host structure array. To maximize performance, the attributes of the items that make up the host structure array should match the attributes of the columns being retrieved. Consider the following COBOL example: Note: By using the code examples, you agree to the terms of the Code license and disclaimer information on page 300.
EXEC SQL INCLUDE SQLCA END-EXEC. ... 01 TABLE-1. 02 DEPT OCCURS 10 TIMES. 05 EMPNO PIC X(6). 05 LASTNAME. 49 LASTNAME-LEN PIC S9(4) BINARY. 49 LASTNAME-TEXT PIC X(15). 05 WORKDEPT PIC X(3). 05 JOB PIC X(8). 01 TABLE-2. 02 IND-ARRAY OCCURS 10 TIMES. 05 INDS PIC S9(4) BINARY OCCURS 4 TIMES. ... EXEC SQL DECLARE D11 CURSOR FOR SELECT EMPNO, LASTNAME, WORKDEPT, JOB FROM CORPDATA.EMPLOYEE WHERE WORKDEPT = "D11" END-EXEC. ... EXEC SQL OPEN D11 END-EXEC. PERFORM FETCH-PARA UNTIL SQLCODE NOT EQUAL TO ZERO. ALL-DONE. EXEC SQL CLOSE D11 END-EXEC.
SQL programming
223
... FETCH-PARA. EXEC SQL WHENEVER NOT FOUND GO TO ALL-DONE END-EXEC. EXEC SQL FETCH D11 FOR 10 ROWS INTO :DEPT :IND-ARRAY END-EXEC. ...
In this example, a cursor was defined for the CORPDATA.EMPLOYEE table to select all rows where the WORKDEPT column equals D11. The result table contains eight rows. The DECLARE CURSOR and OPEN statements do not have any special syntax when they are used with a multiple-row FETCH statement. Another FETCH statement that returns a single row against the same cursor can be coded elsewhere in the program. The multiple-row FETCH statement is used to retrieve all of the rows in the result table. Following the FETCH, the cursor position remains on the last row retrieved. The host structure array DEPT and the associated indicator array IND-ARRAY are defined in the application. Both arrays have a dimension of ten. The indicator array has an entry for each column in the result table. The attributes of type and length of the DEPT host structure array elementary items match the columns that are being retrieved. When the multiple-row FETCH statement has successfully completed, the host structure array contains the data for all eight rows. The indicator array, IND_ARRAY, contains zeros for every column in every row because no NULL values were returned. The SQLCA that is returned to the application contains the following information: v SQLCODE contains 0 v v v v SQLSTATE contains 00000 SQLERRD3 contains 8, the number of rows fetched SQLERRD4 contains 34, the length of each row SQLERRD5 contains +100, indicating the last row in the result table is in the block Related reference SQLCA (SQL communication area)
Multiple-row FETCH using a row storage area: Before using a multiple-row FETCH statement with the row storage area, the application must define a row storage area and an associated description area. The row storage area is a host variable defined in the application. The row storage area contains the results of the multiple-row FETCH statement. A row storage area can be a character variable with enough bytes to hold all of the rows that are requested on the multiple-row FETCH statement. An SQLDA that contains the SQLTYPE and SQLLEN for each returned column is defined by the associated descriptor used on the row storage area form of the multiple-row FETCH. The information provided in the descriptor determines the data mapping from the database to the row storage area. To maximize performance, the attribute information in the descriptor should match the attributes of the columns retrieved. Consider the following PL/I example: Note: By using the code examples, you agree to the terms of the Code license and disclaimer information on page 300.
224
*....+....1....+....2....+....3....+....4....+....5....+....6....+....7...* EXEC SQL INCLUDE SQLCA; EXEC SQL INCLUDE SQLDA; ... DCL DEPTPTR PTR; DCL 1 DEPT(20) BASED(DEPTPTR), 3 EMPNO CHAR(6), 3 LASTNAME CHAR(15) VARYING, 3 WORKDEPT CHAR(3), 3 JOB CHAR(8); DCL I BIN(31) FIXED; DEC J BIN(31) FIXED; DCL ROWAREA CHAR(2000); ... ALLOCATE SQLDA SET(SQLDAPTR); EXEC SQL DECLARE D11 CURSOR FOR SELECT EMPNO, LASTNAME, WORKDEPT, JOB FROM CORPDATA.EMPLOYEE WHERE WORKDEPT = D11; ... EXEC SQL OPEN D11; /* SET UP THE DESCRIPTOR FOR THE MULTIPLE-ROW FETCH */ /* 4 COLUMNS ARE BEING FETCHED */ SQLD = 4; SQLN = 4; SQLDABC = 366; SQLTYPE(1) = 452; /* FIXED LENGTH CHARACTER - */ /* NOT NULLABLE */ SQLLEN(1) = 6; SQLTYPE(2) = 456; /*VARYING LENGTH CHARACTER */ /* NOT NULLABLE */ SQLLEN(2) = 15; SQLTYPE(3) = 452; /* FIXED LENGTH CHARACTER - */ SQLLEN(3) = 3; SQLTYPE(4) = 452; /* FIXED LENGTH CHARACTER - */ /* NOT NULLABLE */ SQLLEN(4) = 8; /*ISSUE THE MULTIPLE-ROW FETCH STATEMENT TO RETRIEVE*/ /*THE DATA INTO THE DEPT ROW STORAGE AREA */ /*USE A HOST VARIABLE TO CONTAIN THE COUNT OF */ /*ROWS TO BE RETURNED ON THE MULTIPLE-ROW FETCH */ J = 20; /*REQUESTS 20 ROWS ON THE FETCH ... EXEC SQL WHENEVER NOT FOUND GOTO FINISHED; EXEC SQL WHENEVER SQLERROR GOTO FINISHED; EXEC SQL FETCH D11 FOR :J ROWS USING DESCRIPTOR :SQLDA INTO :ROWAREA; /* ADDRESS THE ROWS RETURNED DEPTPTR = ADDR(ROWAREA); /*PROCESS EACH ROW RETURNED IN THE ROW STORAGE /*AREA BASED ON THE COUNT OF RECORDS RETURNED /*IN SQLERRD3. DO I = 1 TO SQLERRD(3); IF EMPNO(I) = 000170 THEN */
*/ */ */ */
SQL programming
225
DO; : END; END; IF SQLERRD(5) = 100 THEN DO; /* PROCESS END OF FILE END; FINISHED:
*/
In this example, a cursor has been defined for the CORPDATA.EMPLOYEE table to select all rows where the WORKDEPT column equal D11. The sample EMPLOYEE table in the Sample Tables shows the result table contains multiple rows. The DECLARE CURSOR and OPEN statements do not have special syntax when they are used with a multiple-row FETCH statement. Another FETCH statement that returns a single row against the same cursor can be coded elsewhere in the program. The multiple-row FETCH statement is used to retrieve all rows in the result table. Following the FETCH, the cursor position remains on the final row in the block. The row area, ROWAREA, is defined as a character array. The data from the result table is placed in the host variable. In this example, a pointer variable is assigned to the address of ROWAREA. Each item in the rows that are returned is examined and used with the based structure DEPT. The attributes (type and length) of the items in the descriptor match the columns that are retrieved. In this case, no indicator area is provided. After the FETCH statement is completed, the ROWAREA contains all of the rows that equal D11, in this case 11 rows. The SQLCA that is returned to the application contains the following: v SQLCODE contains 0 v SQLSTATE contains 00000 v SQLERRD3 contains 11, the number of rows returned v SQLERRD4 contains 34, for the length of the row fetched v SQLERRD5 contains +100, indicating the last row in the result table was fetched In this example, the application has taken advantage of the fact that SQLERRD5 contains an indication of the end of the file being reached. As a result, the application does not need to call SQL again to attempt to retrieve more rows. If the cursor has immediate sensitivity to inserts, you should call SQL in case any records were added. Cursors have immediate sensitivity when the commitment control level is something other than *RR. Related reference DB2 Universal Database for iSeries sample tables on page 281 These sample tables are referred to and used in the SQL programming and the SQL reference topic collections. SQLDA (SQL descriptor area)
226
position is maintained. On a ROLLBACK statement, the cursor position is restored to just after the last row retrieved from the previous unit of work. All record locks are still released. After issuing a COMMIT or ROLLBACK statement without HOLD, all locks are released and all cursors are closed. You can open the cursor again, but you will begin processing at the first row of the result table. Note: Specification of the ALWBLK(*ALLREAD) parameter of the Create SQL (CRTSQLxxx) commands can change the restoration of the cursor position for read-only cursors. For information about the use of the ALWBLK parameter and other performance-related options on the CRTSQLxxx commands, see Dynamic SQL applications. Related concepts Commitment control
227
Related reference Unit of work and open cursors on page 226 When your program completes a unit of work, it should commit or roll back the changes that you have made. Actions allowed on SQL statements Process Extended Dynamic SQL (QSQPRCED) API
Related concepts Using interactive SQL on page 244 Interactive SQL allows a programmer or a database administrator to quickly and easily define, update, delete, or look at data for testing, problem analysis, and database maintenance. Using the PREPARE and EXECUTE statements:
228
If the non-SELECT statement does not contain parameter markers, you can run it dynamically using the EXECUTE IMMEDIATE statement. However, if the non-SELECT statement contains parameter markers, you must run it using the PREPARE and EXECUTE statements. The PREPARE statement prepares the non-SELECT statement (for example, the DELETE statement) and gives it a statement name you choose. If DLYPRP (*YES) is specified on the CRTSQLxxx command, the preparation is delayed until the first time the statement is used in an EXECUTE or DESCRIBE statement, unless the USING clause is specified on the PREPARE statement. After the statement has been prepared, it can be run many times within the same program, using different values for the parameter markers. The following example is of a prepared statement being run multiple times:
DSTRING = DELETE FROM CORPDATA.EMPLOYEE WHERE EMPNO = ?; /*The ? is a parameter marker which denotes that this value is a host variable that is to be substituted each time the statement is run.*/ EXEC SQL PREPARE S1 FROM :DSTRING; /*DSTRING is the delete statement that the PREPARE statement is naming S1.*/ DO UNTIL (EMP =0); /*The application program reads a value for EMP from the display station.*/ EXEC SQL EXECUTE S1 USING :EMP; END;
In routines similar to the example above, you must know the number of parameter markers and their data types, because the host variables that provide the input data are declared when the program is being written. Note: All prepared statements that are associated with an application server are destroyed whenever the connection to the application server ends. Connections are ended by a CONNECT (Type 1) statement, a DISCONNECT statement, or a RELEASE followed by a successful COMMIT.
229
To run fixed-list SELECT statements dynamically, your application must: 1. Place the input SQL statement into a host variable. 2. Issue a PREPARE statement to validate the dynamic SQL statement and put it into a form that can be run. If DLYPRP (*YES) is specified on the CRTSQLxxx command, the preparation is delayed until the first time the statement is used in an EXECUTE or DESCRIBE statement, unless the USING clause is specified on the PREPARE statement. 3. Declare a cursor for the statement name. 4. Open the cursor. 5. FETCH a row into a fixed list of variables (rather than into a descriptor area, as if you were using a varying-list SELECT statement. 6. When end of data occurs, close the cursor. 7. Handle any SQL return codes that result. For example:
MOVE SELECT EMPNO, LASTNAME FROM CORPDATA.EMPLOYEE WHERE EMPNO>? TO DSTRING. EXEC SQL PREPARE S2 FROM :DSTRING END-EXEC. EXEC SQL DECLARE C2 CURSOR FOR S2 END-EXEC. EXEC SQL OPEN C2 USING :EMP END-EXEC. PERFORM FETCH-ROW UNTIL SQLCODE NOT=0. EXEC SQL CLOSE C2 END-EXEC. STOP-RUN. FETCH-ROW. EXEC SQL FETCH C2 INTO :EMP, :EMPNAME END-EXEC.
| Note: Remember that because the SELECT statement, in this case, always returns the same number and type of data items as previously run fixed-list SELECT statements, you do not need to use an SQL | descriptor area. | Varying-list SELECT statements: In dynamic SQL, a varying-list SELECT statement is used when the number and format of the result columns are not predictable; that is, you do not know the data types or the number of variables that you need. Therefore, you cannot define host variables in advance to accommodate the result columns returned. | Note: In REXX, steps 5.b, 6, and 7 are not applicable. REXX only supports SQL descriptors defined using the SQLDA structure; it does not support allocated SQL descriptors. | If your application accepts varying-list SELECT statements, your program has to: 1. Place the input SQL statement into a host variable. 2. Issue a PREPARE statement to validate the dynamic SQL statement and put it into a form that can be run. If DLYPRP (*YES) is specified on the CRTSQLxxx command, the preparation is delayed until the first time the statement is used in an EXECUTE or DESCRIBE statement, unless the USING clause is specified on the PREPARE statement. 3. Declare a cursor for the statement name.
230
| |
4. Open the cursor (declared in step 3) that includes the name of the dynamic SELECT statement. 5. For an allocated SQL descriptor, run an ALLOCATE DESCRIPTOR statement to define the descriptor you intend to use. 6. Issue a DESCRIBE statement to request information from SQL about the type and size of each column of the result table. Notes: a. You can also code the PREPARE statement with an INTO clause to perform the functions of PREPARE and DESCRIBE with a single statement. b. If using an SQLDA and the SQLDA is not large enough to contain column descriptions for each retrieved column, the program must determine how much space is needed, get storage for that amount of space, build a new SQLDA, and reissue the DESCRIBE statement. If using an allocated SQL descriptor and the descriptor is not large enough, deallocate the descriptor, allocate it with a larger number of entries, and reissue the DESCRIBE statement. 7. For an SQLDA descriptor, allocate the amount of storage needed to contain a row of retrieved data. 8. For an SQLDA descriptor, put storage addresses into the SQLDA to tell SQL where to put each item of retrieved data. 9. FETCH a row. 10. Process the data returned in the SQL descriptor. 11. Handle any SQL return codes that might result. 12. When end of data occurs, close the cursor. 13. For an allocated SQL descriptor, run a DEALLOCATE DESCRIPTOR statement to delete the descriptor. Related reference Example: A SELECT statement for allocating storage for SQLDA on page 234 Suppose that your application needs to handle a dynamic SELECT statement that changes from one use to the next. This statement can be read from a display, passed in from another application, or built dynamically by your application. SQL descriptor areas: Dynamic SQL uses an SQL descriptor area (SQLDA) to pass information about an SQL statement between SQL and your application. A descriptor is required for running the DESCRIBE, DESCRIBE INPUT and DESCRIBE TABLE statements, and can also be used on the PREPARE, OPEN, FETCH, CALL, and EXECUTE statements. The meaning of the information in an SQLDA depends on its use. In PREPARE and DESCRIBE, an SQLDA provides information to an application program about a prepared statement. In DESCRIBE INPUT, the SQL descriptor area provides information to an application program about parameter markers in a prepared statement. In DESCRIBE TABLE, the SQLDA provides information to an application program about the columns in a table or view. In OPEN, EXECUTE, CALL, and FETCH, an SQLDA provides information about host variables. For example, you can read values into the SQLDA using a DESCRIBE statement, change the data values in the descriptor to use the host variables, and then reuse the same descriptor in a FETCH statement. If your application allows you to have several cursors open at the same time, you can code several SQLDAs, one for each dynamic SELECT statement.
| | |
| |
| |
There are two types of SQLDAs. One is defined with the ALLOCATE DESCRIPTOR statement. The other is defined with the SQLDA structure.
SQL programming
231
| | | |
ALLOCATE DESCRIPTOR is not supported in REXX. SQLDAs can be used in C, C++, COBOL, PL/I, REXX, and RPG. Because RPG/400 does not provide a way to set pointers, the SQLDA must be set outside the RPG/400 program by a PL/I, C, C++, COBOL, or an ILE RPG program. That program must then call the RPG/400 program. Related reference SQLCA (SQL communication area) SQLDA (SQL descriptor area) SQLDA format: An SQL descriptor area (SQLDA) consists of four variables followed by an arbitrary number of occurrences of a sequence of six variables collectively named SQLVAR. Note: The SQLDA in REXX is different. When an SQLDA is used in OPEN, FETCH, CALL, and EXECUTE, each occurrence of SQLVAR describes a host variable. The fields of the SQLDA are as follows: SQLDAID SQLDAID is as used an eyecatcher for storage dumps. It is a string of 8 characters that have the value SQLDA after the SQLDA is used in a PREPARE or DESCRIBE statement. This variable is not used for FETCH, OPEN, CALL, or EXECUTE. Byte 7 can be used to determine if more than one SQLVAR entry is needed for each column. Multiple SQLVAR entries may be needed if there are any LOB or distinct type columns. This flag is set to a blank if there are not any LOBs or distinct types. SQLDAID is not applicable in REXX. SQLDABC SQLDABC indicates the length of the SQLDA. It is a 4-byte integer that has the value SQLN*LENGTH(SQLVAR) + 16 after the SQLDA is used in a PREPARE or DESCRIBE statement. SQLDABC must have a value equal to or greater than SQLN*LENGTH(SQLVAR) + 16 before it is used by FETCH, OPEN, CALL, or EXECUTE. SQLABC is not applicable in REXX. SQLN SQLN is a 2-byte integer that specifies the total number of occurrences of SQLVAR. It must be set before being used by any SQL statement to a value greater than or equal to 0. SQLN is not applicable in REXX. SQLD SQLD is a 2-byte integer that specifies the number of occurrences of SQLVAR, in other words, the number of host variables or columns described by the SQLDA. This field is set by SQL on a DESCRIBE or PREPARE statement. In other statements, this field must be set before being used to a value greater than or equal to 0 and less than or equal to SQLN. SQLVAR This group of values are repeated once for each host variable or column. These variables are set by SQL on a DESCRIBE or PREPARE statement. In other statements, they must be set before being used. These variables are defined as follows: SQLTYPE SQLTYPE is a 2-byte integer that specifies the data type of the host variable or column. See SQLTYPE and SQLLEN for a table of the valid values. Odd values for SQLTYPE show that the host variable has an associated indicator variable addressed by SQLIND.
232
SQLLEN SQLLEN is a 2-byte integer variable that specifies the length attribute of the host variable or column. SQLRES SQLRES is a 12-byte reserved area for boundary alignment purposes. Note that, in i5/OS, pointers must be on a quad-word boundary. SQLRES is not applicable in REXX. SQLDATA SQLDATA is a 16-byte pointer variable that specifies the address of the host variables when the SQLDA is used on OPEN, FETCH, CALL, and EXECUTE. When the SQLDA is used on PREPARE and DESCRIBE, this area is overlaid with the following information: The CCSID of a character or graphic field is stored in the third and fourth bytes of SQLDATA. For BIT data, the CCSID is 65535. In REXX, the CCSID is returned in the variable SQLCCSID. SQLIND SQLIND is a 16-byte pointer that specifies the address of a small integer host variable that is used as an indication of null or not null when the SQLDA is used on OPEN, FETCH, CALL, and EXECUTE. A negative value indicates null and a non-negative indicates not null. This pointer is only used if SQLTYPE contains an odd value. When the SQLDA is used on PREPARE and DESCRIBE, this area is reserved for future use. SQLNAME SQLNAME is a variable-length character variable with a maximum length of 30. After a PREPARE or DESCRIBE, this variable contains the name of selected column, label, or system column name. In OPEN, FETCH, EXECUTE, or CALL, this variable can be used to pass the CCSID of character strings. CCSIDs can be passed for character and graphic host variables. The SQLNAME field in an SQLVAR array entry of an input SQLDA can be set to specify the CCSID. See CCSID values in SQLDATA or SQLNAME for the layout of the CCSID data in this field. Note: It is important to remember that the SQLNAME field is only for overriding the CCSID. Applications that use the defaults do not need to pass CCSID information. If a CCSID is not passed, the default CCSID for the job is used. The default for graphic host variables is the associated double-byte CCSID for the job CCSID. If an associated double-byte CCSID does not exist, 65535 is used. SQLVAR2 This is the Extended SQLVAR structure that contains 3 fields. Extended SQLVARs are needed for all columns of the result if the result includes any distinct type or LOB columns. For distinct types, they contain the distinct type name. For LOBs, they contain the length attribute of the host variable and a pointer to the buffer that contains the actual length. If locators are used to represent LOBs, these entries are not necessary. The number of Extended SQLVAR occurrences needed depends on the statement that the SQLDA was provided for and the data types of the columns or parameters being described. Byte 7 of SQLDAID is always set to the number of sets of SQLVARs necessary. If SQLD is not set to a sufficient number of SQLVAR occurrences: v SQLD is set to the total number of SQLVAR occurrences needed for all sets.
SQL programming
233
v A +237 warning is returned in the SQLCODE field of the SQLCA if at least enough were specified for the Base SQLVAR Entries. The Base SQLVAR entries are returned, but no Extended SQLVARs are returned. v A +239 warning is returned in the SQLCODE field of the SQLCA if enough SQLVARs were not specified for even the Base SQLVAR Entries. No SQLVAR entries are returned. SQLLONGLEN SQLLONGLEN is a 4-byte integer variable that specifies the length attribute of a LOB (BLOB, CLOB, or DBCLOB) host variable or column. SQLDATALEN SQLDATALEN is a 16-byte pointer variable that specifies the address of the length of the host variable. This variable is used for LOB (BLOB, CLOB, and DBCLOB) host variables only. It is not used for DESCRIBE or PREPARE. If this field is NULL, then the actual length of the data is stored in the 4 bytes immediately before the start of the data, and SQLDATA points to the first byte of the field length. The length indicates the number of bytes for a BLOB or CLOB, and the number of characters for a DBCLOB. If this field is not NULL, it contains a pointer to a 4-byte long buffer that contains the actual length in bytes (even for DBCLOB) of the data in the buffer pointed to by the SQLDATA field in the matching base SQLVAR. SQLDATATYPE_NAME SQLDATATYPE_NAME is a variable-length character variable with a maximum length of 30. It is only used for DESCRIBE or PREPARE. This variable is set to one of the following: v For a distinct type column, the database manager sets this to the fully qualified distinct type name. If the qualified name is longer than 30 bytes, it is truncated. v For a label, the database manager sets this to the first 20 bytes of the label. v For a column name, the database manager sets this to the column name. Related tasks Coding SQL statements in REXX applications Related reference Example: A SELECT statement for allocating storage for SQLDA Suppose that your application needs to handle a dynamic SELECT statement that changes from one use to the next. This statement can be read from a display, passed in from another application, or built dynamically by your application. Example: A SELECT statement for allocating storage for SQLDA: Suppose that your application needs to handle a dynamic SELECT statement that changes from one use to the next. This statement can be read from a display, passed in from another application, or built dynamically by your application. In other words, you dont know exactly what this statement is going to be returning every time. Your application needs to handle the varying number of result columns with data types that are unknown ahead of time. For example, the following statement needs to be processed:
SELECT WORKDEPT, PHONENO FROM CORPDATA.EMPLOYEE WHERE LASTNAME = PARKER
Note: This SELECT statement has no INTO clause. Dynamic SELECT statements must not have an INTO clause, even if they return only one row.
234
The statement is assigned to a host variable. The host variable, in this case named DSTRING, is then processed by using the PREPARE statement as shown:
EXEC SQL PREPARE S1 FROM :DSTRING;
Next, you need to determine the number of result columns and their data types. To do this, you need an SQLDA. The first step in defining an SQLDA is to allocate storage for it. (Allocating storage is not necessary in REXX.) The techniques for acquiring storage are language-dependent. The SQLDA must be allocated on a 16-byte boundary. The SQLDA consists of a fixed-length header that is 16 bytes in length. The header is followed by a varying-length array section (SQLVAR), each element of which is 80 bytes in length. The amount of storage that you need to allocate depends on how many elements you want to have in the SQLVAR array. Each column you select must have a corresponding SQLVAR array element. Therefore, the number of columns listed in your SELECT statement determines how many SQLVAR array elements you should allocate. Because this SELECT statement is specified at run time, it is impossible to know exactly how many columns will be accessed. Consequently, you must estimate the number of columns. Suppose, in this example, that no more than 20 columns are ever expected to be accessed by a single SELECT statement. In this case, the SQLVAR array should have a dimension of 20, ensuring that each item in the select-list has a corresponding entry in SQLVAR. This makes the total SQLDA size 20 x 80, or 1600, plus 16 for a total of 1616 bytes Having allocated what you estimated to be enough space for your SQLDA, you need to set the SQLN field of the SQLDA equal to the number of SQLVAR array elements, in this case 20. Having allocated storage and initialized the size, you can now issue a DESCRIBE statement.
EXEC SQL DESCRIBE S1 INTO :SQLDA;
When the DESCRIBE statement is run, SQL places values in the SQLDA that provide information about the select-list for your statement. The following tables show the contents of the SQLDA after the DESCRIBE is run. Only the entries that are meaningful in this context are shown.
Table 43. SQLDA header Description SQLAID SQLDABC SQLN SQLD Value SQLDA 1616 20 2
SQLDAID is an identifier field initialized by SQL when a DESCRIBE is run. SQLDABC is the byte count or size of the SQLDA. The SQLDA header is followed by 2 occurrences of the SQLVAR structure, one for each column in the result table of the SELECT statement being described:
Table 44. SQLVAR element 1 Description SQLTYPE SQLLEN SQLDATA (3:4) SQLNAME Value 453 3 37 8 WORKDEPT
SQL programming
235
Table 45. SQLVAR element 2 Description SQLTYPE SQLLEN SQLDATA(3:4) SQLNAME Value 453 4 37 7 PHONENO
Your program might need to alter the SQLN value if the SQLDA is not large enough to contain the described SQLVAR elements. For example, suppose that instead of the estimated maximum of 20 columns, the SELECT statement actually returns 27. SQL cannot describe this select-list because the SQLVAR needs more elements than the allocated space allows. Instead, SQL sets the SQLD to the actual number of columns specified by the SELECT statement and the remainder of the structure is ignored. Therefore, after a DESCRIBE, you should compare the SQLN value to the SQLD value. If the value of SQLD is greater than the value of SQLN, allocate a larger SQLDA based on the value in SQLD, as follows, and perform the DESCRIBE again:
EXEC SQL DESCRIBE S1 INTO :SQLDA; IF SQLN <= SQLD THEN DO; /*Allocate a larger SQLDA using the value of SQLD.*/ /*Reset SQLN to the larger value.*/ EXEC SQL DESCRIBE S1 INTO :SQLDA; END;
If you use DESCRIBE on a non-SELECT statement, SQL sets SQLD to 0. Therefore, if your program is designed to process both SELECT and non SELECT statements, you can describe each statement after it is prepared to determine whether it is a SELECT statement. This example is designed to process only SELECT statements; the SQLD value is not checked. Your program must now analyze the elements of SQLVAR returned from the successful DESCRIBE. The first item in the select-list is WORKDEPT. In the SQLTYPE field, the DESCRIBE returns a value for the data type of the expression and whether nulls are applicable or not. In this example, SQL sets SQLTYPE to 453 in SQLVAR element 1. This specifies that WORKDEPT is a fixed-length character string result column and that nulls are permitted in the column. SQL sets SQLLEN to the length of the column. Because the data type of WORKDEPT is CHAR, SQL sets SQLLEN equal to the length of the character column. For WORKDEPT, that length is 3. Therefore, when the SELECT statement is later run, a storage area large enough to hold a CHAR(3) string will be needed. Because the data type of WORKDEPT is CHAR FOR SBCS DATA, the first 4 bytes of SQLDATA were set to the CCSID of the character column. The last field in an SQLVAR element is a varying-length character string called SQLNAME. The first 2 bytes of SQLNAME contain the length of the character data. The character data itself is typically the name of a column used in the SELECT statement, in this case WORKDEPT. The exceptions to this are select-list items that are unnamed, such as functions (for example, SUM(SALARY)), expressions (for example, A+B-C), and constants. In these cases, SQLNAME is an empty string. SQLNAME can also contain a label rather than a name. One of the parameters associated with the PREPARE and DESCRIBE statements is the USING clause. You can specify it this way:
236
If you specify: NAMES (or omit the USING parameter entirely) Only column names are placed in the SQLNAME field. SYSTEM NAMES Only the system column names are placed in the SQLNAME field. LABELS Only labels associated with the columns listed in your SQL statement are entered here. ANY Labels are placed in the SQLNAME field for those columns that have labels; otherwise, the column names are entered.
BOTH Names and labels are both placed in the field with their corresponding lengths. Remember to double the size of the SQLVAR array because you are including twice the number of elements. ALL Column names, labels, and system column names are placed in the field with their corresponding lengths. Remember to triple the size of the SQLVAR array
In this example, the second SQLVAR element contains the information for the second column used in the select: PHONENO. The 453 code in SQLTYPE specifies that PHONENO is a CHAR column. SQLLEN is set to 4. Now you need to set up to use the SQLDA to retrieve values when running the SELECT statement. After analyzing the result of the DESCRIBE, you can allocate storage for variables that are to contain the result of the SELECT statement. For WORKDEPT, a character field of length 3 must be allocated; for PHONENO, a character field of length 4 must be allocated. Since both of these results can be the NULL value, an indicator variable must be allocated for each field as well. After the storage is allocated, you must set SQLDATA and SQLIND to point to the allocated storage areas. For each element of the SQLVAR array, SQLDATA points to the place where the result value is to be put. SQLIND points to the place where the null indicator value is to be put. The following tables show what the structure looks like now. Only the entries that are meaningful in this context are shown:
Table 46. SQLDA header Description SQLAID SQLDABC SQLN SQLD Table 47. SQLVAR element 1 Description SQLTYPE SQLLEN SQLDATA SQLIND Value 453 3 Pointer to area for CHAR(3) result Pointer to 2 byte integer indicator for result column Value SQLDA 1616 20 2
SQL programming
237
Table 48. SQLVAR element 2 Description SQLTYPE SQLLEN SQLDATA SQLIND Value 453 4 Pointer to area for CHAR(4) result Pointer to 2 byte integer indicator for result column
You are now ready to retrieve the SELECT statements results. Dynamically defined SELECT statements must not have an INTO statement. Therefore, all dynamically defined SELECT statements must use a cursor. Special forms of the DECLARE, OPEN, and FETCH are used for dynamically defined SELECT statements. The DECLARE statement for the example statement is:
EXEC SQL DECLARE C1 CURSOR FOR S1;
As you can see, the only difference is that the name of the prepared SELECT statement (S1) is used instead of the SELECT statement itself. The actual retrieval of result rows is made as follows:
EXEC SQL OPEN C1; EXEC SQL FETCH C1 USING DESCRIPTOR :SQLDA; DO WHILE (SQLCODE = 0); /*Process the results pointed to by SQLDATA*/ EXEC SQL FETCH C1 USING DESCRIPTOR :SQLDA; END; EXEC SQL CLOSE C1;
The cursor is opened. The result rows from the SELECT are then returned one at a time using a FETCH statement. On the FETCH statement, there is no list of output host variables. Instead, the FETCH statement tells SQL to return results into areas described by your SQLDA. The results are returned into the storage areas pointed to by the SQLDATA and SQLIND fields of the SQLVAR elements. After the FETCH statement has been processed, the SQLDATA pointer for WORKDEPT has its referenced value set to E11. Its corresponding indicator value is 0 since a non-null value was returned. The SQLDATA pointer for PHONENO has its referenced value set to 4502. Its corresponding indicator value is also 0 since a non-null value was returned. Related reference Varying-list SELECT statements on page 230 In dynamic SQL, a varying-list SELECT statement is used when the number and format of the result columns are not predictable; that is, you do not know the data types or the number of variables that you need. SQLDA format on page 232 An SQL descriptor area (SQLDA) consists of four variables followed by an arbitrary number of occurrences of a sequence of six variables collectively named SQLVAR. | Example: A SELECT statement using an allocated SQL descriptor: | Suppose that your application needs to handle a dynamic SELECT statement that changes from one use | to the next. This statement can be read from a display, passed from another application, or built | dynamically by your application.
238
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
In other words, you dont know exactly what this statement is going to be returning every time. Your application needs to be able to handle the varying number of result columns with data types that are unknown ahead of time. For example, the following statement needs to be processed:
SELECT WORKDEPT, PHONENO FROM CORPDATA.EMPLOYEE WHERE LASTNAME = PARKER
Note: This SELECT statement has no INTO clause. Dynamic SELECT statements must not have an INTO clause, even if they return only one row. The statement is assigned to a host variable. The host variable, in this case named DSTRING, is then processed by using the PREPARE statement as shown:
EXEC SQL PREPARE S1 FROM :DSTRING;
Next, you need to determine the number of result columns and their data types. To do this, you need to allocate the largest number of entries for an SQL descriptor that you think you will need. Assume that no more than 20 columns are ever expected to be accessed by a single SELECT statement.
EXEC SQL ALLOCATE DESCRIPTOR mydescr WITH MAX 20;
Now that the descriptor is allocated, the DESCRIBE statement can be done to get the column information.
EXEC SQL DESCRIBE S1 USING DESCRIPTOR mydescr;
When the DESCRIBE statement is run, SQL places values that provide information about the statements select-list into the SQL descriptor area defined by mydescr. If the DESCRIBE determines that not enough entries were allocated in the descriptor, SQLCODE +239 is issued. As part of this diagnostic, the second replacement text value indicates the number of entries that are needed. The following code sample shows how this condition can be detected and shows the descriptor allocated with the larger size.
/* Determine the returned SQLCODE from the DESCRIBE statement */ EXEC SQL GET DIAGNOSTICS CONDITION 1: returned_sqlcode = DB2_RETURNED_SQLCODE; if returned_sqlcode = 239 then do; /* Get the second token for the SQLCODE that indicated not enough entries were allocated */ EXEC SQL GET DIAGNOSTICS CONDITION 1: token = DB2_ORDINAL_TOKEN_2; /* Move the token variable from a character host variable into an integer host variable */ EXEC SQL SET :var1 = :token; /* Deallocate the descriptor that is too small */ EXEC SQL DEALLOCATE DESCRIPTOR mydescr; /* Allocate the new descriptor to be the size indicated by the retrieved token */ EXEC SQL ALLOCATE DESCRIPTOR mydescr WITH MAX :var1; /* Perform the describe with the larger descriptor */ EXEC SQL DESCRIBE s1 USING DESCRIPTOR mydescr; end;
SQL programming
239
| | | | | | | | | | | | | | | | | | | | | | | | |
At this point, the descriptor contains the information about the SELECT statement. Now you are ready to retrieve the SELECT statement results. For dynamic SQL, the SELECT INTO statement is not allowed. You must use a cursor.
EXEC SQL DECLARE C1 CURSOR FOR S1;
You will notice that the prepared statement name is used in the cursor declaration instead of the complete SELECT statement. Now you can loop through the selected rows, processing them as you read them. The following code sample shows how this is done.
EXEC SQL OPEN C1; EXEC SQL FETCH C1 USING SQL DESCRIPTOR mydescr; do while not at end of data; /* process current data returned /* then read the next row */ EXEC SQL FETCH C1 USING SQL DESCRIPTOR mydescr; end; EXEC SQL CLOSE C1; (see below for discussion of doing this) */
| The cursor is opened. The result rows from the SELECT statement are then returned one at a time using a | FETCH statement. On the FETCH statement, there is no list of output host variables. Instead, the FETCH | statement tells SQL to return results into the descriptor area. | After the FETCH has been processed, you can use the GET DESCRIPTOR statement to read the values. | First, you must read the header value that indicates how many descriptor entries were used. | EXEC SQL GET DESCRIPTOR mydescr :count = COUNT; | | | | | | | | | | | | | | | | | | | | | | Next you can read information about each of the descriptor entries. After you determine the data type of the result column, you can do another GET DESCRIPTOR to return the actual value. To get the value of the indicator, specify the INDICATOR item. If the value of the INDICATOR item is negative, the value of the DATA item is not defined. Until another FETCH is done, the descriptor items will maintain their values.
do i = 1 to count; GET DESCRIPTOR mydescr VALUE :i /* set entry :type = TYPE, :length = LENGTH, :result_ind = INDICATOR; if result_ind >= 0 then if type = character GET DESCRIPTOR mydescr VALUE :i :char_result = DATA; else if type = integer GET DESCRIPTOR mydescr VALUE :i :int_result = DATA; else /* continue checking and processing for all end; number to get */ /* get the data type */ /* length value */
/* read data into integer field */ data types that might be returned */
| There are several other descriptor items that you might need to check to determine how to handle the | result data. PRECISION, SCALE, DB2_CCSID, and DATETIME_INTERVAL_CODE are among them. The | host variable that has the DATA value read into it must have the same data type and CCSID as the data
240
| | | | |
being read. If the data type is varying length, the host variable can be declared longer than the actual data. For all other data types, the length must match exactly. NAME, DB2_SYSTEM_COLUMN_NAME, and DB2_LABEL can be used to get name-related values for the result column. See GET DESCRIPTOR for more information about the items returned for a GET DESCRIPTOR statement and for the definition of the TYPE values Parameter markers: A parameter marker is a question mark (?) that appears in a dynamic statement string. The question mark can appear where a host variable might appear if the statement string were a static SQL statement. In the example used, the SELECT statement that was dynamically run had a constant value in the WHERE clause:
WHERE LASTNAME = PARKER
If you want to run the same SELECT statement several times, using different values for LASTNAME, you can use an SQL statement that looks like this:
SELECT WORKDEPT, PHONENO FROM CORPDATA.EMPLOYEE WHERE LASTNAME = ?
| | | | | | | | | | |
When using parameter markers, your application does not need to set the data types and values for the parameters until run time. By specifying a descriptor on the OPEN statement, you can substitute the values for the parameter markers in the SELECT statement. To code such a program, you need to use the OPEN statement with a descriptor clause. This SQL statement is used to not only open a cursor, but to replace each parameter marker with the value of the corresponding descriptor entry. The descriptor name that you specify with this statement must identify a descriptor that contains a valid definition of the values. This descriptor is not used to return information about data items that are part of a SELECT list. It provides information about values that are used to replace parameter markers in the SELECT statement. It gets this information from the application, which must be designed to place appropriate values into the fields of the descriptor. The descriptor is then ready to be used by SQL for replacing parameter markers with the actual values. When you use an SQLDA for input to the OPEN statement with the USING DESCRIPTOR clause, not all of its fields need to be filled in. Specifically, SQLDAID, SQLRES, and SQLNAME can be left blank (SQLNAME can be set if a specific CCSID is needed.) Therefore, when you use this method for replacing parameter markers with values, you need to determine: v How many parameter markers there are v The data types and attributes of these parameters markers (SQLTYPE, SQLLEN, and SQLNAME) v Whether an indicator variable is needed
| In addition, if the routine is to handle both SELECT and non-SELECT statements, you might want to | determine what category of statement it is. If your application uses parameter markers, your program has to perform the following steps. This can be done using either an SQLDA or an allocated descriptor. 1. Read a statement into the DSTRING varying-length character string host variable. 2. Determine the number of parameter markers. 3. Allocate an SQLDA of that size or use ALLOCATE DESCRIPTOR to allocate a descriptor with that number of entries. This is not applicable in REXX.
SQL programming
241
4. For an SQLDA, set SQLN and SQLD to the number of parameter markers. SQLN is not applicable in REXX. For an allocated descriptor, use SET DESCRIPTOR to set the COUNT entry to the number of parameter markers. 5. For an SQLDA, set SQLDABC equal to SQLN*LENGTH(SQLVAR) + 16. This is not applicable in REXX. 6. For each parameter marker: a. Determine the data types, lengths, and indicators. b. For an SQLDA, set SQLTYPE and SQLLEN for each parameter marker. For an allocated descriptor, use SET DESCRIPTOR to set the entries for TYPE, LENGTH, PRECISION, and SCALE for each parameter marker. c. For an SQLDA, allocate storage to hold the input values. d. For an SQLDA, set these values in storage. e. For an SQLDA, set SQLDATA and SQLIND (if applicable) for each parameter marker. For an allocated descriptor, use SET DESCRIPTOR to set entries for DATA and INDICATOR (if applicable) for each parameter marker. f. If character variables are used and they have a CCSID other than the job default CCSID, or graphic variables are used and they have a CCSID other than the associated DBCS CCSID for the job CCSID, v For an SQLDA, set SQLNAME (SQLCCSID in REXX) accordingly. v For an allocated SQL descriptor, use SET DESCRIPTOR to set the DB2_CCSID value. g. Issue the OPEN statement with a USING DESCRIPTOR clause (for an SQLDA) or USING SQL DESCRIPTOR clause (for an allocated descriptor) to open your cursor and substitute values for each of the parameter markers. The statement can then be processed normally. Related reference Example: A SELECT statement for allocating storage for SQLDA on page 234 Suppose that your application needs to handle a dynamic SELECT statement that changes from one use to the next. This statement can be read from a display, passed in from another application, or built dynamically by your application. Example: A SELECT statement using an allocated SQL descriptor on page 238 Suppose that your application needs to handle a dynamic SELECT statement that changes from one use to the next. This statement can be read from a display, passed from another application, or built dynamically by your application.
242
SQL programming
243
244
application that uses dynamic SQL either accepts an SQL statement as input or builds an SQL statement in the form of a character string. The application does not need to know the type of the SQL statement. Related reference Processing non-SELECT statements on page 228 Before building a dynamic SQL non-SELECT statement, you need to verify that this SQL statement is allowed to be run dynamically.
Note: If you are using the system naming convention, the names in parentheses appear instead of the names shown above. An interactive session consists of: v Parameter values you specified for the STRSQL command.
SQL programming
245
v SQL statements you entered in the session along with corresponding messages that follow each SQL statement v Values of any parameters you changed using the session services function v List selections you have made Interactive SQL supplies a unique session-ID consisting of your user ID and the current workstation ID. This session-ID concept allows multiple users with the same user ID to use interactive SQL from more than one workstation at the same time. Also, more than one interactive SQL session can be run from the same workstation at the same time from the same user ID. If an SQL session exists and is being re-entered, any parameters specified on the STRSQL command are ignored. The parameters from the existing SQL session are used. Related reference Start SQL Interactive Session (STRSQL) command
Prompting
The prompt function helps you provide the necessary information for the syntax of the statement that you want to use. The prompt function can be used in any of these statement processing modes: *RUN, *VLD, and *SYN. Prompting is not available for all SQL statements and is not complete for many SQL statements. You have two options when using the prompter: v Type the verb of the statement before pressing F4=Prompt. The statement is parsed and the clauses that are completed are filled in on the prompt displays. If you type SELECT and press F4=Prompt, the following display appears:
246
Specify SELECT Statement Type SELECT statement information. FROM tables . . . . . SELECT columns . . . WHERE conditions . . GROUP BY columns . . HAVING conditions . . ORDER BY columns . . FOR UPDATE OF columns . . . . . . . . . . . . . . . . . . . . . Press F4 for a list.
Type choices, press Enter. DISTINCT rows in result table . . . . . . . . . N UNION with another SELECT . . . . . . . . . . . N Specify additional options . . . . . . . . . . . N Y=Yes, N=No Y=Yes, N=No Y=Yes, N=No
F4=Prompt F12=Cancel
F5=Refresh F6=Insert line F9=Specify subquery F14=Delete line F15=Split line F24=More keys
v Press F4=Prompt before typing anything on the Enter SQL Statements display. You are shown a list of statements. The list of statements varies and depends on the current interactive SQL statement processing mode. For syntax check mode with a language other than *NONE, the list includes all SQL statements. For run and validate modes, only statements that can be run in interactive SQL are shown. You can select the number of the statement you want to use. The system prompts you for the statement you selected. If you press F4=Prompt without typing anything, the following display appears:
Select SQL Statement Select one of the following: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. ALTER TABLE CALL COMMENT ON COMMIT CONNECT CREATE ALIAS CREATE COLLECTION CREATE INDEX CREATE PROCEDURE CREATE TABLE CREATE VIEW DELETE DISCONNECT DROP ALIAS More... Selection __ F3=Exit F12=Cancel
If you press F21=Display Statement on a prompt display, the prompter displays the formatted SQL statement as it was filled in to that point. When Enter is pressed within prompting, the statement that was built through the prompt screens is inserted into the session. If the statement processing mode is *RUN, the statement is run. The prompter remains in control if an error is encountered. Syntax checking:
SQL programming
247
The syntax of the SQL statement is checked when it enters the prompter. The prompter does not accept a syntactically incorrect statement. You must correct the syntax or remove the incorrect part of the statement or prompting will not be allowed. Statement processing mode: The statement processing mode can be selected on the Change Session Attributes display. In *RUN (run) or *VLD (validate) mode, only statements that are allowed to run in interactive SQL can be prompted. In *SYN (syntax check) mode, all SQL statements are allowed. The statement is not actually run in *SYN or *VLD modes; only the syntax and existence of objects are checked. Subqueries: Subqueries can be selected on any display that has a WHERE or HAVING clause. To see the subquery display, press F9=Specify subquery when the cursor is on a WHERE or HAVING input line. A display appears that allows you to type in subselect information. If the cursor is within the parentheses of the subquery when F9 is pressed, the subquery information is filled in on the next display. If the cursor is outside the parentheses of the subquery, the next display is blank. CREATE TABLE prompting: You can enter column definitions individually when you are prompted for a CREATE TABLE statement. Place your cursor in the column definition section of the display, and press F4=Prompt. A display that provides room for entering all the information for one column definition is shown. To enter a column name longer than 18 characters, press F20=Display entire name. A window with enough space for a 30 character name will be displayed. The editing keys, F6=Insert line, F10=Copy line, and F14=Delete line, can be used to add and delete entries in the column definition list. Entering DBCS data: The rules for processing double-byte character set (DBCS) data across multiple lines are the same on the Enter SQL Statements display and in the SQL prompter. Each line must contain the same number of shift-in and shift-out characters. When processing a DBCS data string that requires more than one line for entering, the extra shift-in and shift-out characters are removed. If the last column on a line contains a shift-in and the first column of the next line contains a shift-out, the shift-in and shift-out characters are removed by the prompter when the two lines are assembled. If the last two columns of a line contain a shift-in followed by a single-byte blank and the first column of the next line contains a shift-out, the shift-in, blank, shift-out sequence is removed when the two lines are assembled. This removal allows DBCS information to be read as one continuous character string. As an example, suppose the following WHERE condition were entered. The shift characters are shown here at the beginning and end of the string sections on each of the two lines.
248
Specify SELECT Statement Type SELECT statement information. Press F4 for a list.
FROM tables . . . . . . . . TABLE1_______________________________________ SELECT columns . . . . . . *____________________________________________ WHERE conditions . . . . . COL1 = <AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQ> <RRSS>______________________________________ GROUP BY columns . . . . . _____________________________________________ HAVING conditions . . . . . _____________________________________________ ORDER BY columns . . . . . _____________________________________________ FOR UPDATE OF columns . . . _____________________________________________
When Enter is pressed, the character string is put together, removing the extra shift characters. The statement looks like this on the Enter SQL Statements display:
SELECT * FROM TABLE1 WHERE COL1 = <AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSS>
249
v Selected *SQL for the naming convention. Note: The example shows lists that are not on your server. They are used as an example only. Begin using SQL statements: 1. Type SELECT on the first statement entry line. 2. Type FROM on the second statement entry line. 3. Leave the cursor positioned after FROM.
Enter SQL Statements Type SQL statement, press Enter. ===> SELECT FROM _
4. Press F17=Select tables to obtain a list of tables, because you want the table name to follow FROM. Instead of a list of tables appearing as you expected, a list of collections appears (the Select and Sequence collections display). You have just entered the SQL session and have not selected a schema to work with. 5. Type a 1 in the Seq column next to YOURCOLL2 schema.
Select and Sequence Collections
Type sequence numbers (1-999) to select collection, press Enter. Seq 1 Collection YOURCOLL1 YOURCOLL2 YOURCOLL3 YOURCOLL4 Type SYS SYS SYS SYS Text Company benefits Employee personal data Job classifications/requirements Company insurances
6. Press Enter. The Select and Sequence Tables display appears, showing the tables existing in the YOURCOLL2 schema. 7. Type a 1 in the Seq column next to PEOPLE table.
Select and Sequence Tables Type sequence numbers (1-999) to select tables, press Enter. Seq 1 Table EMPLCO PEOPLE EMPLEXP EMPLEVL EMPLBEN EMPLMED EMPLINVST Collection YOURCOLL2 YOURCOLL2 YOURCOLL2 YOURCOLL2 YOURCOLL2 YOURCOLL2 YOURCOLL2 Type Text TAB Employee company data TAB Employee personal data TAB Employee experience TAB Employee evaluation reports TAB Employee benefits record TAB Employee medical record TAB Employee investments record
8. Press Enter. The Enter SQL Statements display appears again with the table name, YOURCOLL2.PEOPLE, inserted after FROM. The table name is qualified by the schema name in the *SQL naming convention.
250
Enter SQL Statements Type SQL statement, press Enter. ===> SELECT FROM YOURCOLL2.PEOPLE _
9. Position the cursor after SELECT. 10. Press F18=Select columns to obtain a list of columns, because you want the column name to follow SELECT. The Select and Sequence Columns display appears, showing the columns in the PEOPLE table. 11. Type a 2 in the Seq column next to the NAME column. 12. Type a 1 in the Seq column next to the SOCSEC column.
Select and Sequence Columns Type sequence numbers (1-999) to select columns, press Enter. Seq Column 2 NAME EMPLNO 1 SOCSEC STRADDR CITY ZIP PHONE Table PEOPLE PEOPLE PEOPLE PEOPLE PEOPLE PEOPLE PEOPLE Type CHARACTER CHARACTER CHARACTER CHARACTER CHARACTER CHARACTER CHARACTER Digits Length 6 30 11 30 20 9 20
13. Press Enter. The Enter SQL Statements display appears again with SOCSEC, NAME appearing after SELECT.
Enter SQL Statements Type SQL statement, press Enter. ===> SELECT SOCSEC, NAME FROM YOURCOLL2.PEOPLE
14. Press Enter. The statement you created is now run. Once you have used the list function, the values you selected remain in effect until you change them or until you change the list of schemas on the Change Session Attributes display.
251
v v v v v v v v v v v v v v v v v v v
Commitment control attributes. The statement processing control. The SELECT output device. The list of schemas. The list type to select either all your system and SQL objects, or only your SQL objects. The data refresh option when displaying data. The allow copy data option. The The The The The The The The The The The The naming option. programming language. date format. time format. date separator. time separator. decimal point representation. SQL string delimiter. sort sequence. language identifier. SQL rules. CONNECT password option.
Option 2 (Print current session) accesses the Change Printer display, which lets you print the current session immediately and then continue working. You are prompted for printer information. All the SQL statements you entered and all the messages displayed are printed just as they appear on the Enter SQL Statements display. Option 3 (Remove all entries from current session) lets you remove all the SQL statements and messages from the Enter SQL Statements display and the session history. You are prompted to ensure that you really want to delete the information. Option 4 (Save session in source file) accesses the Change Source File display, which lets you save the session in a source file. You are prompted for the source file name. This function lets you embed the source file into a host language program by using the source entry utility (SEU). Note: Option 4 allows you to embed prototyped SQL statements in a high-level language (HLL) program that uses SQL. The source file created by option 4 may be edited and used as the input source file for the Run SQL Statements (RUNSQLSTM) command.
252
1. Option 4 allows you to embed prototype SQL statements in a high-level language (HLL) program that uses SQL. Use the source entry utility (SEU) to copy the statements into your program. The source file can also be edited and used as the input source file for the Run SQL Statements (RUNSQLSTM) command. 2. If rows have been changed and locks are currently being held for this unit of work and you attempt to exit interactive SQL, a warning message is displayed.
SQL programming
253
v When connecting to an application server that does not support distributed unit of work, a RELEASE ALL followed by a COMMIT be issued to end previous connections, including the implicit connection to local. v When connecting to a non-DB2 UDB for iSeries application server, a RELEASE ALL followed by a COMMIT be issued to end previous connections, including the implicit connection to local, and change the commitment control level to at least *CHG. When you are connecting to a non-DB2 UDB for iSeries application server, some session attributes are changed to attributes that are supported by that application server. The following table shows the attributes that change.
Table 49. Values table Session attribute Date format *YMD *DMY *MDY *JUL Time format *HMS with a : separator *HMS with any other separator *ISO *EUR *USA *USA *JIS *EUR Commitment control *CHG, *NONE *ALL Naming convention Allow copy data Data refresh Decimal point Sort sequence *SYS *NO, *YES *ALWAYS *SYSVAL Any value other than *HEX *SQL *OPTIMIZE *FORWARD *PERIOD *HEX *CS Repeatable Read Original value New value
Notes: 1. If you are connected to an application server that is running a release prior to Version 2 Release 3, the sort sequence value changes to *HEX. 2. When you are connected to a DB2/2 or DB2/6000 application server, the date and time formats specified must be the same. After the connection is completed, a message is sent stating that the session attributes have been changed. The changed session attributes can be displayed by using the session services display. While interactive SQL is running, no other connection can be established for the default activation group. When connected to a remote system with interactive SQL, a statement processing mode of syntax-only checks the syntax of the statement against the syntax supported by the local system instead of the remote system. Similarly, the SQL prompter and list support use the statement syntax and naming conventions
254
supported by the local system. The statement is run, however, on the remote system. Because of differences in the level of SQL support between the two systems, syntax errors may be found in the statement on the remote system at run time. Lists of schemas and tables are available when you are connected to the local relational database. Lists of columns are available only when you are connected to a relational database manager that supports the DESCRIBE TABLE statement. When you exit interactive SQL with connections that have pending changes or connections that use protected conversations, the connections remain. If you do not perform additional work over the connections, the connections are ended during the next COMMIT or ROLLBACK operation. You can also end the connections by doing a RELEASE ALL and a COMMIT before exiting interactive SQL. Using interactive SQL for remote access to non-DB2 UDB for iSeries application servers might require some setup. Note: In the output of a communications trace, there may be a reference to a CREATE TABLE XXX statement. This is used to determine package existence; it is part of normal processing, and can be ignored. Related concepts Distributed database programming Related reference Determining the connection type on page 272 When a remote SQL connection is established, it uses either an unprotected or a protected network connection.
255
v v v v v v v v v v v v | v | v v
DELETE DROP GRANT INSERT LABEL ON LOCK TABLE REFRESH TABLE RELEASE SAVEPOINT RENAME REVOKE ROLLBACK SAVEPOINT SET CURRENT DEGREE SET ENCRYPTION PASSWORD SET PATH
v SET SCHEMA v SET TRANSACTION v UPDATE In the source member, statements end with a semicolon and do not begin with EXEC SQL. If the record length of the source member is longer than 80, only the first 80 characters will be read. Comments in the source member can be either line comments or block comments. Line comments begin with a double hyphen (--) and end at the end of the line. Block comments start with /* and can continue across many lines until the next */ is reached. Block comments can be nested. Only SQL statements and comments are allowed in the source file. The output listing and the resulting messages for the SQL statements are sent to a print file. The default print file is QSYSPRT. To perform syntax checking only on all statements in the source member, specify the PROCESS(*SYN) parameter on the RUNSQLSTM command. Related reference Run SQL Statement (RUNSQLSTM) command
256
The SET TRANSACTION statement can be used within the source member to override the level of commitment control specified on the RUNSQLSTM command. Note: The job must be at a unit of work boundary to use the SQL statement processor with commitment control.
5722SS1 V5R4M0 060210 Run SQL Statements Source file...............CORPDATA/SRC Member....................SCHEMA Commit....................*NONE Naming....................*SYS Generation level..........10 Date format...............*JOB Date separator............*JOB Time format...............*HMS Time separator ...........*JOB Default Collection........*NONE IBM SQL flagging..........*NOFLAG ANS flagging..............*NONE Decimal point.............*JOB Sort Sequence.............*JOB Language ID...............*JOB Printer file..............*LIBL/QSYSPRT Source file CCSID.........65535 Job CCSID.................0 Statement processing......*RUN Allow copy of data........*OPTIMIZE Allow blocking............*READ SQL rules.................*DB2 Decimal result options: Maximum precision.......31 Maximum scale...........31 Minimum divide scale....0 Source member changed on 04/01/98 11:54:10
SCHEMA
02/10/06 15:35:18
Page
SQL programming
257
5722SS1 V5R4M0 060210 Run SQL Statements SCHEMA 02/10/06 15:35:18 Page 2 Record *...+... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 SEQNBR Last change 1 2 DROP COLLECTION DEPT; 3 DROP COLLECTION MANAGER; 4 5 CREATE SCHEMA DEPT 6 CREATE TABLE EMP (EMPNAME CHAR(50), EMPNBR INT) 7 -- EMP will be created in collection DEPT 8 CREATE INDEX EMPIND ON EMP(EMPNBR) 9 -- EMPIND will be created in DEPT 10 GRANT SELECT ON EMP TO PUBLIC; -- grant authority 11 12 INSERT INTO DEPT/EMP VALUES(JOHN SMITH, 1234); 13 /* table must be qualified since no 14 longer in the schema */ 15 16 CREATE SCHEMA AUTHORIZATION MANAGER 17 -- this schema will use MANAGERs 18 -- user profile 19 CREATE TABLE EMP_SALARY (EMPNBR INT, SALARY DECIMAL(7,2), 20 LEVEL CHAR(10)) 21 CREATE VIEW LEVEL AS SELECT EMPNBR, LEVEL 22 FROM EMP_SALARY 23 CREATE INDEX SALARYIND ON EMP_SALARY(EMPNBR,SALARY) 24 25 GRANT ALL ON LEVEL TO JONES GRANT SELECT ON EMP_SALARY TO CLERK 26 -- Two statements can be on the same line * * * * * E N D O F S O U R C E * * * * *
5722SS1 V5R4M0 060210 Run SQL Statements SCHEMA 02/10/06 15:35:18 Page 3 Record *...+... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8 SEQNBR Last change MSG ID SEV RECORD TEXT SQL7953 0 1 Position 1 Drop of DEPT in QSYS complete. SQL7953 0 3 Position 3 Drop of MANAGER in QSYS complete. SQL7952 0 5 Position 3 Schema DEPT created. SQL7950 0 6 Position 8 Table EMP created in DEPT. SQL7954 0 8 Position 8 Index EMPIND created in DEPT on table EMP in DEPT. SQL7966 0 10 Position 8 GRANT of authority to EMP in DEPT completed. SQL7956 0 10 Position 40 1 rows inserted in EMP in DEPT. SQL7952 0 13 Position 28 Schema MANAGER created. SQL7950 0 19 Position 9 Table EMP_SALARY created in collection MANAGER. SQL7951 0 21 Position 9 View LEVEL created in MANAGER. SQL7954 0 23 Position 9 Index SALARYIND created in MANAGER on table EMP_SALARY in MANAGER. SQL7966 0 25 Position 9 GRANT of authority to LEVEL in MANAGER completed. SQL7966 0 25 Position 37 GRANT of authority to EMP_SALARY in MANAGER completed. Message Summary Total Info Warning Error Severe Terminal 13 13 0 0 0 0 00 level severity errors found in source * * * * * E N D O F L I S T I N G * * * * *
258
The application requester supports the application side of a connection. The application server is the local or remote database to which an application requester is connected. DB2 UDB for iSeries provides support for Distributed Relational Database Architecture (DRDA) to allow an application requester to communicate with application servers. In addition, DB2 UDB for iSeries can call exit programs to allow access to data on other database management systems which do not support DRDA. These exit programs are called application requester driver (ARD) programs. DB2 UDB for iSeries supports two levels of distributed relational database: v Remote unit of work (RUW) Remote unit of work is where the preparation and running of SQL statements occurs at only one application server during a unit of work. DB2 UDB for iSeries supports RUW over either APPC or TCP/IP. v Distributed unit of work (DUW) Distributed unit of work is where the preparation and running of SQL statements can occur at multiple applications servers during a unit of work. However, a single SQL statement can only refer to objects located at a single application server. DB2 UDB for iSeries supports DUW over APPC and, beginning in V5R1, introduced support for DUW over TCP/IP. Related concepts Introduction to DB2 UDB for iSeries Structured Query Language on page 2 Structured Query Language (SQL) is a standardized language for defining and manipulating data in a relational database. These topics describe the System i implementation of the SQL using DB2 UDB for iSeries and the DB2 Query Manager and SQL Development Kit licensed program. SQL packages on page 11 An SQL package is an object that contains the control structure produced when the SQL statements in an application program are bound to a remote relational database management system (DBMS). Distributed database programming Related reference Accessing data using Distributed Relational Database Architecture (DRDA) on page 244 A distributed relational database consists of a set of SQL objects that are spread across interconnected computer systems. Each relational database has a relational database manager to manage the tables in its environment.
259
v v v v v
Create SQL COBOL Program (CRTSQLCBL) command Create SQL ILE COBOL Object (CRTSQLCBLI) command Create SQL PL/I Program (CRTSQLPLI) command Create SQL RPG Program (CRTSQLRPG) command Create SQL ILE RPG Object (CRTSQLRPGI) command Related tasks Preparing and running a program with SQL statements Related reference DB2 UDB for iSeries CL command descriptions on page 300 DB2 UDB for iSeries provides these CL commands for SQL. CONNECT (Type 1) CONNECT (Type 2) DISCONNECT DROP GRANT (Package Privileges) REVOKE (Package Privileges) RELEASE (Connection) SET CONNECTION
Before running the SETUP, you may need to edit the SETUP member of the QSQL/QSQSAMP file. Instructions are included in the member as comments. To run the SETUP, specify the following command on the system command line:
========> SBMDBJOB QSQL/QSQSAMP SETUP
Wait for the batch job to complete. To use the example program, specify the following command on the command line:
========> ADDLIBLE QSQSAMP
To call the first display that allows you to customize the example program, specify the following command on the command line.
260
The following display opens. From this display, you can customize your database example program.
DB2 for OS/400 ORGANIZATION APPLICATION
E (ERASE)
__
EM (EMPLOYEE)
SEARCH CRITERIA..: __ DI (DEPARTMENT ID) DN (DEPARTMENT NAME) EI (EMPLOYEE ID) MI (MANAGER ID) EN (EMPLOYEE NAME) LOCATION.........: DATA.............: ________________
MN (MANAGER NAME)
_______________________________
261
sent to the remote system during the creation of the SQL package. The runtime support will return an SQLCODE of -84 or -525 when the statement cannot be run on the current application server. For example, multiple-row FETCH, blocked INSERT, and scrollable cursor support are allowed only in distributed programs where both the application requester and the application server are running on the i5/OS V5R2, or later, with the following exception. An application requester that is not running on the i5/OS operating system can issue read-only, insensitive scrollable cursor operations on an i5/OS V5R3 application server. A further restriction on the use of multiple-row FETCH statements, blocked INSERT statements, and scrollable cursors is that the transmission of binary large object (BLOB), character large object (CLOB), and double-byte character large object (DBCLOB) data is not allowed when those functions are used. Related information Characteristics of SQL statements
262
Note: When you connect to a DB2 Universal Database server, the following additional rules apply: v The specified date and time formats must be the same format v A value of *BLANK must be used for the TEXT parameter v Default schemas (DFTRDBCOL) are not supported v The CCSID of the source program from which the package is being created must not be 65535; if 65535 is used, an empty package is created. Target release (TGTRLS) parameter: When you create the package, the SQL statements are checked to determine which release can support the function. This release is set as the restore level of the package. For example, if the package contains a CREATE TABLE statement which adds a FOREIGN KEY constraint to the table, then the restore level of the package will be Version 3 Release 1, because FOREIGN KEY constraints were not supported before this release. TGTRLS message are suppressed when the TGTRLS parameter is *CURRENT. SQL statement size: The create SQL package function might not be able to handle an SQL statement of the same size that the precompiler can process. While the SQL program is being precompiled, the SQL statement is placed into the associated space of the program. When this occurs, each token is separated by a blank. In addition, when the RDB parameter is specified, the host variables of the source statement are replaced with an H. The create SQL package function passes this statement to the application server, along with a list of the host variables for that statement. The addition of the blanks between the tokens and the replacement of host variables can cause the statement to exceed the maximum SQL statement size (SQL0101 reason 5). Statements that do not require a package: In some cases, you might try to create an SQL package, but the SQL package is not created and the program still runs. This situation occurs when the program contains only SQL statements that do not require an SQL package to run. For example, a program that contains only the SQL statement DESCRIBE TABLE will generate message SQL5041 during SQL package creation. The SQL statements that do not require an SQL package are: v COMMIT v CONNECT v DESCRIBE TABLE v DISCONNECT v RELEASE v RELEASE SAVEPOINT v ROLLBACK v SAVEPOINT v SET CONNECTION Package object type: SQL packages are always created as non-ILE objects and always run in the default activation group. ILE programs and service programs:
SQL programming
263
ILE programs and service programs that bind several modules containing SQL statements must have a separate SQL package for each module. Package creation connection: The type of connection used for the package creation is based on the type of connection specified by the RDBCNNMTH parameter. If RDBCNNMTH(*DUW) was specified, commitment control is used and the connection may be a read-only connection. If the connection is read-only, then the package creation will fail. Unit of work: Because package creation implicitly performs a commit or rollback, the commit definition must be at a unit of work boundary before the package creation is attempted. The following conditions must all be true for a commit definition to be at a unit of work boundary: v SQL is at a unit of work boundary. v There are no local or DDM files open using commitment control and no closed local or DDM files with pending changes. v There are no API resources registered. v There are no LU 6.2 resources registered that are not associated with DRDA or DDM. Creating packages locally: The name specified on the RDB parameter can be the name of the local system. If it is the name of the local system, the SQL package is created on the local system. The SQL package can be saved (Save Object (SAVOBJ) command) and then restored (Restore Object (RSTOBJ) command) to another system. When you run the program with a connection to the local system, the SQL package is not used. If you specify *LOCAL for the RDB parameter, an *SQLPKG object is not created, but the package information is saved in the *PGM object. Labels: You can use the LABEL ON statement to create a description for an SQL package. Consistency token: The program and its associated SQL package contain a consistency token that is checked when a call is made to the SQL package. The consistency tokens must match; otherwise, the package cannot be used. It is possible for the program and SQL package to appear to be uncoordinated. Assume that the program and the application server are on two distinct i5/OS operating systems. The program is running in session A and it is re-created in session B (where the SQL package is also re-created). The next call to the program in session A might cause a consistency token error. To avoid locating the SQL package on each call, SQL maintains a list of addresses for SQL packages that are used by each session. When session B re-creates the SQL package, the old SQL package is moved to the QRPLOBJ library. The address to the SQL package in session A is still valid. You can avoid this situation by creating the program and SQL package from the session that is running the program, or by submitting a remote command to delete the old SQL package before creating the program. To use the new SQL package, you should end the connection with the remote system. You can either sign off the session and then sign on again, or you can use the interactive SQL (STRSQL) command to issue a
264
DISCONNECT for unprotected network connections or a RELEASE followed by a COMMIT for protected connections. RCLDDMCNV should then be used to end the network connections. Call the program again. SQL and recursion: If you start SQL from an attention key program while you are already precompiling, you will receive unpredictable results. The Create SQL (CRTSQLxxx), Create SQL Package (CRTSQLPKG), and Start SQL Interactive Session (STRSQL) commands and the SQL runtime environment are not recursive. They produce unpredictable results if recursion is attempted. Recursion occurs if, while one of the commands is running (or running a program with embedded SQL statements), the job is interrupted before the command has completed, and another SQL function is started.
SQL programming
265
are now two different types of connections about which the reader must be aware: SQL connections of the type described above, and network connections which replace the term conversation. Where there might be the possibility of confusion between the two types of connections, the word will be qualified by SQL or network to help the reader to understand the intended meaning. The following is an example of an application that runs in multiple activation groups. This example is used to illustrate the interaction between activation groups, connection management, and commitment control. It is not a recommended coding style.
.... EXEC SQL CONNECT TO SYSB END-EXEC. EXEC SQL SELECT .... END-EXEC. CALL PGM2. .... Figure 2. Source code for PGM1
... EXEC SQL CONNECT TO SYSC; EXEC SQL DECLARE C1 CURSOR FOR SELECT ....; EXEC SQL OPEN C1; do { EXEC SQL FETCH C1 INTO :st1; EXEC SQL UPDATE ... SET COL1 = COL1+10 WHERE CURRENT OF C1; PGM3(st1); } while SQLCODE == 0; EXEC SQL CLOSE C1; EXEC SQL COMMIT; .... Figure 3. Source code for PGM2
266
... EXEC SQL INSERT INTO TAB VALUES(:st1); EXEC SQL COMMIT; .... Figure 4. Source code for PGM3
SQL programming
267
In this example, PGM1 is a non-ILE program created using the CRTSQLCBL command. This program runs in the default activation group. PGM2 is created using the CRTSQLCI command, and it runs in a system-named activation group. PGM3 is also created using the CRTSQLCI command, but it runs in the activation group named APPGRP. Because APPGRP is not the default value for the ACTGRP parameter, the CRTPGM command is issued separately. The CRTPGM command is followed by a CRTSQLPKG command that creates the SQL package object on the SYSD relational database. In this example, the user has not explicitly started the job level commitment definition. SQL implicitly starts commitment control. 1. PGM1 is called and runs in the default activation group. 2. PGM1 connects to relational database SYSB and runs a SELECT statement. 3. PGM1 then calls PGM2, which runs in a system-named activation group.
268
4. PGM2 does a connect to relational database SYSC. Because PGM1 and PGM2 are in different activation groups, the connection started by PGM2 in the system-named activation group does not disconnect the connection started by PGM1 in the default activation group. Both connections are active. PGM2 opens the cursor and fetches and updates a row. PGM2 is running under commitment control, is in the middle of a unit of work, and is not at a connectable state. 5. PGM2 calls PGM3, which runs in activation group APPGRP. 6. The INSERT statement is the first statement run in activation group APPGRP. The first SQL statement causes an implicit connect to relational database SYSD. A row is inserted into table TAB located at relational database SYSD. The insert is then committed. The pending changes in the system-named activation group are not committed, because commitment control was started by SQL with a commit scope of activation group. 7. PGM3 is then exited and control returns to PGM2. PGM2 fetches and updates another row. 8. PGM3 is called again to insert the row. An implicit connect was done on the first call to PGM3. It is not done on subsequent calls because the activation group did not end between calls to PGM3. Finally, all the rows are processed by PGM2 and the unit of work associated with the system-named activation group is committed.
SQL programming
269
270
If there are pending changes, protected connections, or an active SET TRANSACTION statement, SQL is placed in the exited state. If programs precompiled with CLOSQLCSR(*ENDJOB) were run, SQL will remain active for the default activation group until the job ends. v At the end of a unit of work, if SQL is in the exited state. This occurs when you issue a COMMIT or ROLLBACK command outside of an SQL program. v At the end of a job. Related reference Ending connections on page 276 Because remote SQL connections use resources, you need to end the connections that are no longer used, as soon as possible. You can end connections implicitly or explicitly.
Distributed support
DB2 UDB for iSeries supports these levels of distributed relational database. v Remote unit of work (RUW) Remote unit of work is where the preparation and running of SQL statements occurs at only one application server during a unit of work. An activation group with an application process at an application requester can connect to an application server and, within one or more units of work, run any number of static or dynamic SQL statements that refer to objects on the application server. Remote unit of work is also referred to as DRDA level 1. v Distributed unit of work (DUW) Distributed unit of work is where the preparation and running of SQL statements can occur at multiple applications servers during a unit of work. However, a single SQL statement can only refer to objects located at a single application server. Distributed unit of work is also referred to as DRDA level 2. Distributed unit of work allows: Update access to multiple application servers in one logical unit of work or Update access to a single application server with read access to multiple application servers, in one logical unit of work.
SQL programming
271
Whether multiple application servers can be updated in a unit of work is dependent on the existence of a sync point manager at the application requester, sync point managers at the application servers, and two-phase commit protocol support between the application requester and the application servers. The sync point manager is a system component that coordinates commit and rollback operations among the participants in the two-phase commit protocol. When running distributed updates, the sync point managers on the different systems cooperate to ensure that resources reach a consistent state. The protocols and flows used by sync point managers are also referred to as two-phase commit protocols. If two-phase commit protocols will be used, the connection is a protected resource; otherwise the connection is an unprotected resource. The type of data transport protocol used between systems affects whether the network connection is protected or unprotected. Before V5R1, TCP/IP connections were always unprotected; thus they could participate in a distributed unit of work in only a limited way. In V5R1, full support for DUW with TCP/IP was added. For example, if the first connection made from the program is to a pre-V5R1 system over TCP/IP, updates can be performed over it, but any subsequent connections, even over Advanced Program-to-Program Communication (APPC), will be read-only. Note that when using interactive SQL, the first SQL connection is to the local system. Therefore, in the pre-V5R1 environment, in order to make updates to a remote system using TCP/IP, you must do a RELEASE ALL followed by a COMMIT to end all SQL connections before doing the CONNECT TO remote-tcp-system.
272
There are no open updatable DDM files that use a different connection under commitment control for the commitment definition. There are no API commitment control resources for the commitment definition. There are no protected connections registered for the commitment definition. If running with commitment control, SQL will register a one-phase updatable DRDA resource for remote connections or a two-phase updatable DRDA resource for local and ARD connections. 2. The connection is to a remote relational database and the connection is unprotected. The connection is read-only. This will occur only when the following are true: v The connection is not local. v The application server does not support distributed unit of work v At least one of the following is true: The commitment control level of the program issuing the connect is *NONE. Another connection exists to an application server that does not support distributed unit-of-work and that application server can perform committable updates Another connection exists to an application server that supports distributed unit-of-work (including local). There are open updatable local files under commitment control for the commitment definition. There are open updatable DDM files that use a different connection under commitment control for the commitment definition. There are no one-phase API commitment control resources for the commitment definition. There are protected connections registered for the commitment definition. If running with commitment control, SQL will register a one-phase DRDA read-only resource. 3. The connection is to a remote relational database and the connection is protected. It is unknown if committable updates can be performed. This will occur when all of the following are true: v The connection is not local. v The commitment control level of the program issuing the connect is not *NONE. v The application server supports both distributed unit of work and two-phase commit protocol (protected connections). If running with commitment control, SQL will register a two-phase DRDA undetermined resource. 4. The connection is to a remote relational database and the connection is unprotected. It is unknown if committable updates can be performed. This will occur only when all of the following are true: v The connection is not local. v The application server supports distributed unit of work v Either the application server does not support two-phase commit protocols (protected connections) or the commitment control level of the program issuing the connect is *NONE. If running with commitment control, SQL will register a one-phase DRDA undetermined resource. 5. The connection is to the local database or an application requester driver (ARD) program and the connection is protected. It is unknown if committable updates can be performed. If running with commitment control, SQL will register a two-phase DRDA undetermined resource. The following table summarizes the type of connection that will result for remote distributed unit of work connections. SQLERRD(4) is set on successful CONNECT and SET CONNECTION statements.
Table 50. Summary of connection type Connect under commitment control No Application server supports two-phase commit No Application server supports distributed unit of work No Other updatable one-phase resource registered No SQLERRD(4)
2
SQL programming
273
Table 50. Summary of connection type (continued) Connect under commitment control No No No No No No No Yes Yes Yes Yes Yes Yes Yes Yes
1
Application server supports two-phase commit No No No Yes Yes Yes Yes No No No No Yes Yes Yes Yes
Application server supports distributed unit of work No Yes Yes No No Yes Yes No No Yes Yes No No Yes Yes
Other updatable one-phase resource registered Yes No Yes No Yes No Yes No Yes No Yes No Yes No Yes
SQLERRD(4)
2 4 4 2 2 4 4 1 2 4 4 N/A 1 N/A 1 3 3
DRDA does not allow protected connections to be used to application servers that support only remote unit of work (DRDA1). This includes all DB2 for i5/OS TCP/IP connections.
Related concepts Commitment control Related reference Accessing remote databases with interactive SQL on page 253 In interactive SQL, you can communicate with a remote relational database by using the SQL CONNECT statement. Interactive SQL uses the CONNECT (Type 2) semantics (distributed unit of work) for CONNECT statements.
274
SQL programming
275
Table 51. Summary of determining connection status values Connection method Connection exists to updatable remote unit of work application server Where first committable update occurred 1 SQLERRD(3) or DB2_CONNECTION_STATUS 1 Yes No No No No no updates one-phase this connection two-phase 2 1 2 1 1
v No updates indicates no committable updates have been performed, no DDM files open for update using a protected connection, no local files are open for update, and no commitment control APIs are registered. v One-phase indicates the first committable update was performed using an unprotected connection or DDM files are open for update using unprotected connections. v Two-phase indicates a committable update was performed on a two-phase distributed-unit-of-work application server, DDM files are open for update using a protected connection, commitment control APIs are registered, or local files are open for update under commitment control.
If an attempt is made to perform a committable update over a read-only connection, the unit of work will be placed in a rollback required state. If an unit of work is in a rollback required state, the only statement allowed is a ROLLBACK statement; all other statements will result in SQLCODE -918.
Ending connections
Because remote SQL connections use resources, you need to end the connections that are no longer used, as soon as possible. You can end connections implicitly or explicitly. Connections can be explicitly ended by either the DISCONNECT statement or the RELEASE statement followed by a successful COMMIT. The DISCONNECT statement can only be used with connections that use unprotected connections or with local connections. The DISCONNECT statement will end the connection when the statement is run. The RELEASE statement can be used with either protected or unprotected connections. When the RELEASE statement is run, the connection is not ended but instead placed into the released state. A connection that is in the release stated can still be used. The connection is not ended until a successful COMMIT is run. A ROLLBACK or an unsuccessful COMMIT will not end a connection in the released state.
276
When a remote SQL connection is established, a distributed data management (DDM) network connection (Advanced Program-to-Program Communication (APPC) conversation or TCP/IP connection) is used. When the SQL connection is ended, the network connection might either be placed in the unused state or dropped. Whether a network connection is dropped or placed in the unused state depends on the DDMCNV job attribute. If the job attribute value is *KEEP and the connection is to a server on the System i platform, the connection becomes unused. If the job attribute value is *DROP and the connection is to a server on the System i platform, the connection is dropped. If the connection is to a server on a non-System i platform, the connection is always dropped. *DROP is desirable in the following situations: v When the cost of maintaining the unused connection is high and the connection will not be used relatively soon. v When running with a mixture of programs, some compiled with RUW connection management and some programs compiled with DUW connection management. Attempts to run programs compiled with RUW connection management to remote locations will fail when protected connections exist. v When running with protected connections using either DDM or DRDA. Additional overhead is incurred on commits and rollbacks for unused protected connections. The Reclaim DDM connections (RCLDDMCNV) command may be used to end all unused connections, if they are at a commit boundary. Related reference Implicit connection management for the default activation group on page 270 An application requester can be implicitly connected to an application server. Implicit connection management for nondefault activation groups on page 271 An application requester can be implicitly connected to an application server. Implicit SQL connection occurs when the application requester detects that the first SQL statement issued for the activation group is not a CONNECT statement with parameters.
SQL programming
277
.... EXEC SQL WHENEVER SQLERROR GO TO done; EXEC SQL WHENEVER NOT FOUND GO TO done; .... EXEC SQL DECLARE C1 CURSOR WITH HOLD FOR SELECT PARTNO, PRICE FROM PARTS WHERE SITES_UPDATED = N FOR UPDATE OF SITES_UPDATED; /* Connect to the systems */ EXEC SQL CONNECT TO LOCALSYS; EXEC SQL CONNECT TO SYSB; EXEC SQL CONNECT TO SYSC; /* Make the local system the current connection */ EXEC SQL SET CONNECTION LOCALSYS; /* Open the cursor */ EXEC SQL OPEN C1; while (SQLCODE==0) { /* Fetch the first row */ EXEC SQL FETCH C1 INTO :partnumber,:price; /* Update the row which indicates that the updates have been propagated to the other sites */ EXEC SQL UPDATE PARTS SET SITES_UPDATED=Y WHERE CURRENT OF C1; /* Check if the part data is on SYSB */ if ((partnumber > 10) && (partnumber < 100)) { /* Make SYSB the current connection and update the price */ EXEC SQL SET CONNECTION SYSB; EXEC SQL UPDATE PARTS SET PRICE=:price WHERE PARTNO=:partnumber; } /* Check if the part data is on SYSC */ if ((partnumber > 50) && (partnumber < 200)) { /* Make SYSC the current connection and update the price */ EXEC SQL SET CONNECTION SYSC; EXEC SQL UPDATE PARTS SET PRICE=:price WHERE PARTNO=:partnumber; } /* Commit the changes made at all 3 sites */ EXEC SQL COMMIT; /* Set the current connection to local so the next row can be fetched */ EXEC SQL SET CONNECTION LOCALSYS; } done: EXEC SQL WHENEVER SQLERROR /* Release the connections EXEC SQL RELEASE SYSB; EXEC SQL RELEASE SYSC; /* Close the cursor */ EXEC SQL CLOSE C1; /* Do another commit which The local connection is released. */ EXEC SQL COMMIT; ... CONTINUE; that are no longer being used */
will end the released connections. still active because it was not
278
In this program, there are three application servers active: LOCALSYS which the local system, and two remote systems, SYSB and SYSC. SYSB and SYSC also support distributed unit of work and two-phase commit. Initially all connections are made active by using the CONNECT statement for each of the application servers involved in the transaction. When using DUW, a CONNECT statement does not disconnect the previous connection, but instead places the previous connection in the dormant state. After all the application servers have been connected, the local connection is made the current connection using the SET CONNECTION statement. The cursor is then opened and the first row of data fetched. It is then determined at which application servers the data needs to be updated. If SYSB needs to be updated, then SYSB is made the current connection using the SET CONNECTION statement and the update is run. The same is done for SYSC. The changes are then committed. Because two-phase commit is being used, it is guaranteed that the changes are committed at the local system and the two remote systems. Because the cursor was declared WITH HOLD, it remains open after the commit. The current connection is then changed to the local system so that the next row of data can be fetched. This set of fetches, updates, and commits is repeated until all the data has been processed. After all the data has been fetched, the connections for both remote systems are released. They cannot be disconnected because they use protected connections. After the connections are released, a commit is issued to end the connections. The local system is still connected and continues processing.
... EXEC SQL SET CONNECTION SYS5 END-EXEC. ... * Check if the connection is updatable. EXEC SQL CONNECT END-EXEC. * If connection is updatable, update sales information otherwise * inform the user. IF SQLERRD(3) = 1 THEN EXEC SQL INSERT INTO SALES_TABLE VALUES(:SALES-DATA) END-EXEC ELSE DISPLAY Unable to update sales information at this time. ... Figure 6. Example of checking connection status
SQL programming
279
The following distributed unit of work example shows how the same cursor name is opened in two different connections, resulting in two instances of cursor C1.
..... EXEC SQL DECLARE C1 CURSOR FOR SELECT * FROM CORPDATA.EMPLOYEE; /* Connect to local and open C1 */ EXEC SQL CONNECT TO LOCALSYS; EXEC SQL OPEN C1; /* Connect to the remote system and open C1 */ EXEC SQL CONNECT TO SYSA; EXEC SQL OPEN C1; /* Keep processing until done */ while (NOT_DONE) { /* Fetch a row of data from the local system */ EXEC SQL SET CONNECTION LOCALSYS; EXEC SQL FETCH C1 INTO :local_emp_struct; /* Fetch a row of data from the remote system */ EXEC SQL SET CONNECTION SYSA; EXEC SQL FETCH C1 INTO :rmt_emp_struct; /* Process the data */ ..... } /* Close the cursor on the remote system */ EXEC SQL CLOSE C1; /* Close the cursor on the local system */ EXEC SQL SET CONNECTION LOCALSYS; EXEC SQL CLOSE C1; ..... Figure 7. Example of cursors in a DUW program
Problem handling
The primary strategy for capturing and reporting error information for the distributed database function is called first failure data capture (FFDC). The purpose of FFDC support is to provide accurate information about errors that are detected in the distributed data management (DDM) component of the i5/OS operating system from which an Authorized Program Analysis Report (APAR) can be created. By means of this function, key structures
280
and the DDM data stream are automatically dumped to a spooled file. The first 1024 bytes of the error information are also logged in the system error log. This automatic dumping of error information about the first occurrence of an error means that the failure does not need to be re-created to be reported by the customer. FFDC is active in both the application requester and application server functions of the i5/OS DDM component. However, for the FFDC data to be logged, the system value QSFWERRLOG must be set to *LOG. Note: Not all negative SQLCODEs are dumped; only those that can be used to produce an APAR are dumped. For more information about handling problems on distributed relational database operations, see the Distributed Database Problem Determination Guide SC26-4782. When an SQL error is detected, an SQLCODE with a corresponding SQLSTATE is returned in the SQLCA. Related reference SQL messages and codes
Reference
Reference information for SQL programming includes sample tables and CL commands.
281
on the call to the procedure. Since this is an SQL external stored procedure, it can be called from any SQL interface, including interactive SQL and iSeries Navigator. To call the procedure where SAMPLE is the schema you want to create, issue the following statement:
CALL QSYS.CREATE_SQL_SAMPLE (SAMPLE)
The schema name must be specified in uppercase. The schema must not already exist. Note: In these sample tables, a question mark (?) indicates a null value. Related reference Referential integrity and tables on page 16 Referential integrity is the condition of a set of tables in a database in which all references from one table to another are valid. Example: DELETE rules on page 90 Suppose that deleting a department from the DEPARTMENT table sets WORKDEPT in the EMPLOYEE table to null for every employee assigned to that department. Multiple-row FETCH using a row storage area on page 224 Before using a multiple-row FETCH statement with the row storage area, the application must define a row storage area and an associated description area.
ALTER TABLE DEPARTMENT ADD FOREIGN KEY ROD (ADMRDEPT) REFERENCES DEPARTMENT ON DELETE CASCADE
282
SPIFFY COMPUTER SERVICE DIV. 000010 PLANNING INFORMATION CENTER DEVELOPMENT CENTER MANUFACTURING SYSTEMS ADMINISTRATION SYSTEMS SUPPORT SERVICES OPERATIONS SOFTWARE SUPPORT BRANCH OFFICE F2 BRANCH OFFICE G2 BRANCH OFFICE H2 BRANCH OFFICE I2 BRANCH OFFICE J2 000020 000030 ? 000060 000070 000050 000090 000100 ? ? ? ? ?
SQL programming
283
PRIMARY KEY (EMPNO)) ALTER TABLE EMPLOYEE ADD FOREIGN KEY RED (WORKDEPT) REFERENCES DEPARTMENT ON DELETE SET NULL ALTER TABLE EMPLOYEE ADD CONSTRAINT NUMBER CHECK (PHONENO >= 0000 AND PHONENO <= 9999)
284
EMP NO 000010 000020 000030 000050 000060 000070 000090 000100 000110 000120 000130 000140 000150 000160 000170 000180 000190 000200 000210 000220 000230 000240 000250 000260 000270 000280 000290 000300 000310 000320 000330 000340 200010 200120 200140 200170 200220 200240 200280 200310 200330 200340
FIRST MID NAME INIT CHRISTINE I MICHAEL L SALLY A JOHN B IRVING F EVA D EILEEN W THEODORE Q VINCENZO G SEAN DOLORES M HEATHER A BRUCE ELIZABETH R MASATOSHI J MARILYN S JAMES H DAVID WILLIAM T JENNIFER K JAMES J SALVATORE M DANIEL S SYBIL P MARIA L ETHEL R JOHN R PHILIP X MAUDE F RAMLAL V WING JASON R DIAN J GREG KIM N KIYOSHI REBA K ROBERT M EILEEN R MICHELLE F HELENA ROY R
LASTNAME HAAS THOMPSON KWAN GEYER STERN PULASKI HENDERSON SPENSER LUCCHESSI OCONNELL QUINTANA NICHOLLS ADAMSON PIANKA YOSHIMURA SCOUTTEN WALKER BROWN JONES LUTZ JEFFERSON MARINO SMITH JOHNSON PEREZ SCHNEIDER PARKER SMITH SETRIGHT MEHTA LEE GOUNOT HEMMINGER ORLANDO NATZ YAMAMOTO JOHN MONTEVERDE SCHWARTZ SPRINGER WONG ALONZO
WORK DEPT A00 B01 C01 E01 D11 D21 E11 E21 A00 A00 C01 C01 D11 D11 D11 D11 D11 D11 D11 D11 D21 D21 D21 D21 D21 E11 E11 E11 E11 E21 E21 E21 A00 A00 C01 D11 D11 D21 E11 E11 E21 E21
PHONE NO 3978 3476 4738 6789 6423 7831 5498 0972 3490 2167 4578 1793 4510 3782 2890 1682 2986 4501 0942 0672 2094 3780 0961 8953 9001 8997 4502 2095 3332 9990 2103 5698 3978 2167 1793 2890 0672 3780 8997 3332 2103 5698
HIRE DATE 1965-01-01 1973-10-10 1975-04-05 1949-08-17 1973-09-14 1980-09-30 1970-08-15 1980-06-19 1958-05-16 1963-12-05 1971-07-28 1976-12-15 1972-02-12 1977-10-11 1978-09-15 1973-07-07 1974-07-26 1966-03-03 1979-04-11 1968-08-29 1966-11-21 1979-12-05 1969-10-30 1975-09-11 1980-09-30 1967-03-24 1980-05-30 1972-06-19 1964-09-12 1965-07-07 1976-02-23 1947-05-05 1965-01-01 1972-05-05 1976-12-15 1978-09-15 1968-08-29 1979-12-05 1967-03-24 1964-09-12 1976-02-23 1947-05-05
JOB PRES MANAGER MANAGER MANAGER MANAGER MANAGER MANAGER MANAGER SALESREP CLERK ANALYST ANALYST DESIGNER DESIGNER DESIGNER DESIGNER DESIGNER DESIGNER DESIGNER DESIGNER CLERK CLERK CLERK CLERK CLERK OPERATOR OPERATOR OPERATOR OPERATOR FILEREP FILEREP FILEREP SALESREP CLERK ANALYST DESIGNER DESIGNER CLERK OPERATOR OPERATOR FIELDREP FIELDREP
ED LEVEL 18 18 20 16 16 16 16 14 19 14 16 18 16 17 16 17 16 16 17 18 14 17 15 16 15 17 12 14 12 16 14 16 18 14 18 16 18 17 17 12 14 16
SEX F M F M M F F M M M F F M F M F M M M F M M M F F F M M F M M M F M F M F M F F F M
BIRTH DATE 1933-08-24 1948-02-02 1941-05-11 1925-09-15 1945-07-07 1953-05-26 1941-05-15 1956-12-18 1929-11-05 1942-10-18 1925-09-15 1946-01-19 1947-05-17 1955-04-12 1951-01-05 1949-02-21 1952-06-25 1941-05-29 1953-02-23 1948-03-19 1935-05-30 1954-03-31 1939-11-12 1936-10-05 1953-05-26 1936-03-28 1946-07-09 1936-10-27 1931-04-21 1932-08-11 1941-07-18 1926-05-17 1933-08-14 1942-10-18 1946-01-19 1951-01-05 1948-03-19 1954-03-31 1936-03-28 1931-04-21 1941-07-18 1926-05-17
SALARY 52750 41250 38250 40175 32250 36170 29750 26150 46500 29250 23800 28420 25280 22250 24680 21340 20450 27740 18270 29840 22180 28760 19180 17250 27380 26250 15340 17750 15900 19950 25370 23840 46500 29250 28420 24680 29840 28760 26250 15900 25370 23840
BONUS 1000 800 800 800 500 700 600 500 900 600 500 600 500 400 500 500 400 600 400 600 400 600 400 300 500 500 300 400 300 400 500 500 1000 600 600 500 600 600 500 300 500 500
COMM 4220 3300 3060 3214 2580 2893 2380 2092 3720 2340 1904 2274 2022 1780 1974 1707 1636 2217 1462 2387 1774 2301 1534 1380 2190 2100 1227 1420 1272 1596 2030 1907 4220 2340 2274 1974 2387 2301 2100 1272 2030 1907
SQL programming
285
286
ALTER TABLE EMPPROJACT ADD FOREIGN KEY REPAPA (PROJNO, ACTNO, EMSTDATE) REFERENCES PROJACT ON DELETE RESTRICT
SQL programming
287
Table 53. Columns of the Employee to project activity table (continued) Column name EMSTDATE EMENDATE Description Start date of the activity Completion date of the activity
288
EMPNO 000130 000130 000140 000030 000140 000140 000140 000140 000010 000110 000010 000200 000200 000220 000150 000150 000170 000170 000190 000190 000160 000170 000180 000210 000210 000050 000090 000280 000290 000300 000310 000050 000100 000320 000320 000330 000330 000340 000340 000020
PROJNO IF1000 IF1000 IF1000 IF2000 IF2000 IF2000 IF2000 IF2000 MA2100 MA2100 MA2110 MA2111 MA2111 MA2111 MA2112 MA2112 MA2112 MA2112 MA2112 MA2112 MA2113 MA2113 MA2113 MA2113 MA2113 OP1000 OP1010 OP1010 OP1010 OP1010 OP1010 OP2010 OP2010 OP2011 OP2011 OP2012 OP2012 OP2013 OP2013 PL2100
ACTNO 90 100 90 10 100 100 110 110 10 20 10 50 60 40 60 180 60 70 70 80 60 80 70 80 180 10 10 130 130 130 130 10 10 140 150 140 160 140 170 30
EMPTIME 1.00 .50 .50 .50 1.00 .50 .50 .50 .50 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 .50 .50 .25 1.00 1.00 1.00 1.00 1.00 .75 1.00 .75 .25 .25 .75 .50 .50 1.00
EMSTDATE 1982-10-01 1982-10-01 1982-10-01 1982-01-01 1982-01-01 1982-03-01 1982-03-01 1982-10-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-06-15 1982-01-01 1982-01-01 1982-07-15 1982-01-01 1982-06-01 1982-01-01 1982-10-01 1982-07-15 1982-01-01 1982-04-01 1982-10-01 1982-10-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01
EMENDATE 1983-01-01 1983-01-01 1983-01-01 1983-01-01 1982-03-01 1982-07-01 1982-07-01 1983-01-01 1982-11-01 1983-03-01 1983-02-01 1982-06-15 1983-02-01 1983-02-01 1982-07-15 1983-02-01 1983-06-01 1983-02-01 1982-10-01 1983-10-01 1983-02-01 1983-02-01 1982-06-15 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1982-09-15
SQL programming
289
ALTER TABLE PROJECT ADD FOREIGN KEY (DEPTNO) REFERENCES DEPARTMENT ON DELETE RESTRICT ALTER TABLE PROJECT ADD FOREIGN KEY (RESPEMP) REFERENCES EMPLOYEE ON DELETE RESTRICT ALTER TABLE PROJECT ADD FOREIGN KEY RPP (MAJPROJ) REFERENCES PROJECT ON DELETE CASCADE
290
PROJNO AD3100 AD3110 AD3111 AD3112 AD3113 IF1000 IF2000 MA2100 MA2110 MA2111 MA2112 MA2113 OP1000 OP1010 OP2000 OP2010 OP2011 OP2012 OP2013 PL2100
PROJNAME
DEPTNO
RESPEMP 000010 000070 000230 000250 000270 000030 000030 000010 000060 000220 000150 000160 000050 000090 000050 000100 000320 000330 000340 000020
PRSTAFF 6.5 6 2 1 2 2 1 12 9 2 3 3 6 5 5 4 1 1 1 1
PRSTDATE 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-02-15 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01
PRENDATE 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1982-12-01 1982-12-01 1982-12-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1983-02-01 1982-09-15
MAJPROJ ? AD3100 AD3110 AD3110 AD3110 ? ? ? MA2100 MA2110 MA2110 MA2110 ? OP1000 ? OP2000 OP2010 OP2010 OP2010 MA2100
ADMIN SERVICES D01 GENERAL ADMIN D21 SYSTEMS PAYROLL PROGRAMMING PERSONNEL PROGRAMMING ACCOUNT PROGRAMMING QUERY SERVICES USER EDUCATION WELD LINE AUTOMATION WL PROGRAMMING W L PROGRAM DESIGN W L ROBOT DESIGN W L PROD CONT PROGS OPERATION SUPPORT OPERATION GEN SYSTEMS SERVICES SYSTEMS SUPPORT SCP SYSTEMS SUPPORT APPLICATIONS SUPPORT DB/DC SUPPORT WELD LINE PLANNING D21 D21 D21 C01 C01 D01 D11 D11 D11 D11 E01 E11 E01 E21 E21 E21 E21 B01
SQL programming
291
ALTER TABLE PROJACT ADD FOREIGN KEY RPAP (PROJNO) REFERENCES PROJECT ON DELETE RESTRICT
292
PROJNO AD3113 AD3113 AD3113 AD3113 AD3113 AD3113 AD3113 AD3113 AD3113 AD3113 AD3113 IF1000 IF1000 IF1000 IF2000 IF2000 IF2000 IF2000 IF2000 MA2100 MA2100 MA2110 MA2111 MA2111 MA2111 MA2112 MA2112 MA2112 MA2112 MA2112 MA2113 MA2113 MA2113 MA2113 MA2113 OP1000 OP1010 OP1010 OP2010 OP2011 OP2011
ACTNO 70 80 80 180 180 180 60 60 60 70 70 10 90 100 10 100 100 110 110 10 20 10 50 60 40 60 180 70 70 80 60 80 70 80 180 10 10 130 10 140 150
ACSTAFF ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
ACSTDATE 1982-07-01 1982-01-01 1982-03-01 1982-03-01 1982-04-15 1982-06-01 1982-03-01 1982-04-01 1982-09-01 1982-09-01 1982-10-15 1982-06-01 1982-10-01 1982-10-01 1982-01-01 1982-01-01 1982-03-01 1982-03-01 1982-10-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-06-15 1982-01-01 1982-01-01 1982-07-15 1982-06-01 1982-01-01 1982-10-01 1982-07-15 1982-01-01 1982-04-01 1982-10-01 1982-10-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01 1982-01-01
ACENDATE ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
SQL programming
293
ACSTAFF ? ? ? ? ?
ACENDATE ? ? ? ? ?
294
ACTDESC OPER COMPUTER SYS MAINT SOFTWARE SYS ADM OPERATING SYS ADM DATA BASES ADM DATA COMM DOCUMENT
SQL programming
295
1988-12-2308.53.58.000000
ISTERN
1988-12-2214.07.21.136421
CHAAS
296
CREATE TABLE ORG (DEPTNUMB SMALLINT NOT NULL, DEPTNAME VARCHAR(14), MANAGER SMALLINT, DIVISION VARCHAR(10), LOCATION VARCHAR(13))
297
298
ID 3650
NAME Gafney
DEPT 84
JOB Clerk
YEARS 5
SALARY 13030.50
COMM 188.00
SQL programming
299
SALES_DATE 03/30/1996 03/30/1996 03/30/1996 03/30/1996 03/31/1996 03/31/1996 03/31/1996 03/31/1996 03/31/1996 03/31/1996 03/31/1996 04/01/1996 04/01/1996 04/01/1996 04/01/1996 04/01/1996 04/01/1996 04/01/1996 04/01/1996 04/01/1996 04/01/1996
SALES_PERSON LEE GOUNOT GOUNOT GOUNOT LUCCHESSI LEE LEE LEE LEE GOUNOT GOUNOT LUCCHESSI LUCCHESSI LEE LEE LEE LEE GOUNOT GOUNOT GOUNOT GOUNOT
REGION Manitoba Ontario-South Quebec Manitoba Manitoba Ontario-South Ontario-North Quebec Manitoba Ontario-South Quebec Ontario-South Manitoba Ontario-South Ontario-North Quebec Manitoba Ontario-South Ontario-North Quebec Manitoba
SALES 4 2 18 1 1 14 3 7 3 2 1 3 1 8 ? 8 9 3 1 3 7
300
| | | | | | | | |
UNDER NO CIRCUMSTANCES IS IBM, ITS PROGRAM DEVELOPERS OR SUPPLIERS LIABLE FOR ANY OF THE FOLLOWING, EVEN IF INFORMED OF THEIR POSSIBILITY: 1. LOSS OF, OR DAMAGE TO, DATA; 2. DIRECT, SPECIAL, INCIDENTAL, OR INDIRECT DAMAGES, OR FOR ANY ECONOMIC CONSEQUENTIAL DAMAGES; OR 3. LOST PROFITS, BUSINESS, REVENUE, GOODWILL, OR ANTICIPATED SAVINGS. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OR LIMITATION OF DIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, SO SOME OR ALL OF THE ABOVE LIMITATIONS OR EXCLUSIONS MAY NOT APPLY TO YOU.
SQL programming
301
302
Appendix. Notices
This information was developed for products and services offered in the U.S.A. IBM may not offer the products, services, or features discussed in this document in other countries. Consult your local IBM representative for information on the products and services currently available in your area. Any reference to an IBM product, program, or service is not intended to state or imply that only that IBM product, program, or service may be used. Any functionally equivalent product, program, or service that does not infringe any IBM intellectual property right may be used instead. However, it is the users responsibility to evaluate and verify the operation of any non-IBM product, program, or service. IBM may have patents or pending patent applications covering subject matter described in this document. The furnishing of this document does not grant you any license to these patents. You can send license inquiries, in writing, to: IBM Director of Licensing IBM Corporation North Castle Drive Armonk, NY 10504-1785 U.S.A. For license inquiries regarding double-byte (DBCS) information, contact the IBM Intellectual Property Department in your country or send inquiries, in writing, to: IBM World Trade Asia Corporation Licensing 2-31 Roppongi 3-chome, Minato-ku Tokyo 106-0032, Japan The following paragraph does not apply to the United Kingdom or any other country where such provisions are inconsistent with local law: INTERNATIONAL BUSINESS MACHINES CORPORATION PROVIDES THIS PUBLICATION AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Some states do not allow disclaimer of express or implied warranties in certain transactions, therefore, this statement may not apply to you. This information could include technical inaccuracies or typographical errors. Changes are periodically made to the information herein; these changes will be incorporated in new editions of the publication. IBM may make improvements and/or changes in the product(s) and/or the program(s) described in this publication at any time without notice. Any references in this information to non-IBM Web sites are provided for convenience only and do not in any manner serve as an endorsement of those Web sites. The materials at those Web sites are not part of the materials for this IBM product and use of those Web sites is at your own risk. IBM may use or distribute any of the information you supply in any way it believes appropriate without incurring any obligation to you. Licensees of this program who wish to have information about it for the purpose of enabling: (i) the exchange of information between independently created programs and other programs (including this one) and (ii) the mutual use of the information which has been exchanged, should contact: IBM Corporation
Copyright IBM Corp. 1998, 2006
303
Software Interoperability Coordinator, Department YBWA 3605 Highway 52 N Rochester, MN 55901 U.S.A. Such information may be available, subject to appropriate terms and conditions, including in some cases, payment of a fee. | The licensed program described in this information and all licensed material available for it are provided | by IBM under terms of the IBM Customer Agreement, IBM International Program License Agreement, | IBM License Agreement for Machine Code, or any equivalent agreement between us. Any performance data contained herein was determined in a controlled environment. Therefore, the results obtained in other operating environments may vary significantly. Some measurements may have been made on development-level systems and there is no guarantee that these measurements will be the same on generally available systems. Furthermore, some measurements may have been estimated through extrapolation. Actual results may vary. Users of this document should verify the applicable data for their specific environment. Information concerning non-IBM products was obtained from the suppliers of those products, their published announcements or other publicly available sources. IBM has not tested those products and cannot confirm the accuracy of performance, compatibility or any other claims related to non-IBM products. Questions on the capabilities of non-IBM products should be addressed to the suppliers of those products. All statements regarding IBMs future direction or intent are subject to change or withdrawal without notice, and represent goals and objectives only. All IBM prices shown are IBMs suggested retail prices, are current and are subject to change without notice. Dealer prices may vary. This information is for planning purposes only. The information herein is subject to change before the products described become available. This information contains examples of data and reports used in daily business operations. To illustrate them as completely as possible, the examples include the names of individuals, companies, brands, and products. All of these names are fictitious and any similarity to the names and addresses used by an actual business enterprise is entirely coincidental. COPYRIGHT LICENSE: This information contains sample application programs in source language, which illustrate programming techniques on various operating platforms. You may copy, modify, and distribute these sample programs in any form without payment to IBM, for the purposes of developing, using, marketing or distributing application programs conforming to the application programming interface for the operating platform for which the sample programs are written. These examples have not been thoroughly tested under all conditions. IBM, therefore, cannot guarantee or imply reliability, serviceability, or function of these programs. Each copy or any portion of these sample programs or any derivative work, must include a copyright notice as follows: (your company name) (year). Portions of this code are derived from IBM Corp. Sample Programs. Copyright IBM Corp. _enter the year or years_. All rights reserved. If you are viewing this information softcopy, the photographs and color illustrations may not appear.
304
Trademarks
The following terms are trademarks of International Business Machines Corporation in the United States, other countries, or both: | | | | | | | | | | | | | | | | | | | | AIX DB2 DB2 Universal Database Distributed Relational Database Architecture Domino DRDA i5/OS IBM IBM (logo) Integrated Language Environment iSeries Lotus Lotus Notes Net.Data OS/400 PowerPC RPG/400 System i System/36 z/OS Microsoft, Windows, Windows NT, and the Windows logo are trademarks of Microsoft Corporation in the United States, other countries, or both. Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the United States, other countries, or both. | Linux is a registered trademark of Linus Torvalds in the United States, other countries, or both. UNIX is a registered trademark of The Open Group in the United States and other countries. Other company, product, and service names may be trademarks or service marks of others.
305
Except as expressly granted in this permission, no other permissions, licenses or rights are granted, either express or implied, to the publications or any information, data, software or other intellectual property contained therein. IBM reserves the right to withdraw the permissions granted herein whenever, in its discretion, the use of the publications is detrimental to its interest or, as determined by IBM, the above instructions are not being properly followed. You may not download, export or re-export this information except in full compliance with all applicable laws and regulations, including all United States export laws and regulations. IBM MAKES NO GUARANTEE ABOUT THE CONTENT OF THESE PUBLICATIONS. THE PUBLICATIONS ARE PROVIDED AS-IS AND WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY, NON-INFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.
306
Printed in USA