db2 sql

1582
DB2 Universal Database for z/OS Version 8 SQL Reference SC18-7426-09

Upload: pedrofmb

Post on 14-Apr-2015

37 views

Category:

Documents


2 download

TRANSCRIPT

DB2 Universal Database for z/OSVersion 8

SQL Reference

SC18-7426-09

DB2 Universal Database for z/OSVersion 8

SQL Reference

SC18-7426-09

Note Before using this information and the product it supports, be sure to read the general information under Notices on page 1467.

Tenth Edition, Softcopy Only (June 2012) This edition applies to Version 8 of IBM DB2 Universal Database for z/OS (DB2 UDB for z/OS), product number 5625-DB2, and to any subsequent releases until otherwise indicated in new editions. Make sure you are using the correct edition for the level of the product. This softcopy version is based on the printed edition of the book and includes the changes indicated in the printed version by vertical bars. Additional changes made to this softcopy version of the book since the hardcopy book was published are indicated by the hash (#) symbol in the left-hand margin. Editorial changes that have no technical significance are not noted. This and other books in the DB2 UDB for z/OS library are periodically updated with technical changes. These updates are made available to licensees of the product on CD-ROM and on the Web (currently at www.ibm.com/software/data/db2/zos/library.html). Check these resources to ensure that you are using the most current information. Copyright IBM Corporation 1982, 2012. US Government Users Restricted Rights Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.

ContentsAbout this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvWho should read this book . . . . . . . . Conventions and terminology used in this book . Terminology and citations . . . . . . . Conventions for describing mixed data values Industry standards . . . . . . . . . . How to read the syntax diagrams . . . . . Accessibility . . . . . . . . . . . . . How to send your comments . . . . . . . . xv . xvi . xvi . xvi . . . . . . . . . . . . . . . . . . . . . . xvii . . . . . . . . . . . . . . . . . . . . . . xvii . . . . . . . . . . . . . . . . . . . . . . xix . . . . . . . . . . . . . . . . . . . . . . xix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

|

Summary of changes to this book

. . . . . . . . . . . . . . . . . . . . . . . xxi

Chapter 1. DB2 concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Structured query language. . . . . . . . . . . . . . . . . . Static SQL . . . . . . . . . . . . . . . . . . . . . . Dynamic SQL . . . . . . . . . . . . . . . . . . . . . Deferred embedded SQL . . . . . . . . . . . . . . . . . Interactive SQL . . . . . . . . . . . . . . . . . . . . SQL Call Level Interface (CLI) and Open Database Connectivity (ODBC) . Java database connectivity and embedded SQL for Java . . . . . . . Schemas . . . . . . . . . . . . . . . . . . . . . . . . Tables . . . . . . . . . . . . . . . . . . . . . . . . Indexes . . . . . . . . . . . . . . . . . . . . . . . . Keys . . . . . . . . . . . . . . . . . . . . . . . . . Unique keys . . . . . . . . . . . . . . . . . . . . . Primary keys . . . . . . . . . . . . . . . . . . . . . Parent keys . . . . . . . . . . . . . . . . . . . . . . Foreign keys . . . . . . . . . . . . . . . . . . . . . Constraints . . . . . . . . . . . . . . . . . . . . . . . Unique constraints . . . . . . . . . . . . . . . . . . . Referential constraints . . . . . . . . . . . . . . . . . . Check constraints . . . . . . . . . . . . . . . . . . . . Triggers . . . . . . . . . . . . . . . . . . . . . . . Storage structures . . . . . . . . . . . . . . . . . . . . Storage groups . . . . . . . . . . . . . . . . . . . . . Databases . . . . . . . . . . . . . . . . . . . . . . . Catalog . . . . . . . . . . . . . . . . . . . . . . . . Views . . . . . . . . . . . . . . . . . . . . . . . . Sequences . . . . . . . . . . . . . . . . . . . . . . . Routines . . . . . . . . . . . . . . . . . . . . . . . Functions . . . . . . . . . . . . . . . . . . . . . . Stored procedures . . . . . . . . . . . . . . . . . . . Application processes, concurrency, and recovery . . . . . . . . . . Locking, commit, and rollback . . . . . . . . . . . . . . . Unit of work . . . . . . . . . . . . . . . . . . . . . Unit of recovery . . . . . . . . . . . . . . . . . . . . Rolling back work . . . . . . . . . . . . . . . . . . . Packages and application plans . . . . . . . . . . . . . . . . Distributed data . . . . . . . . . . . . . . . . . . . . . Connections . . . . . . . . . . . . . . . . . . . . . Distributed unit of work . . . . . . . . . . . . . . . . . Remote unit of work . . . . . . . . . . . . . . . . . . Character conversion . . . . . . . . . . . . . . . . . . . Character sets and code pages . . . . . . . . . . . . . . . Coded character sets and CCSIDs . . . . . . . . . . . . . . Copyright IBM Corp. 1982, 2012

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

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

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

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

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

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

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

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

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

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

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

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

|

|

|

. 2 . 2 . 2 . 2 . 3 . 3 . 3 . 3 . 4 . 5 . 5 . 5 . 6 . 6 . 6 . 6 . 7 . 7 . 9 . 10 . 10 . 11 . 11 . 11 . 11 . 12 . 13 . 13 . 13 . 14 . 14 . 14 . 15 . 15 . 16 . 17 . 17 . 18 . 21 . 23 . 25 . 28

iii

|

Determining the encoding scheme and CCSID of a string . Expanding conversions . . . . . . . . . . . . Contracting conversions . . . . . . . . . . . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. . .

. 28 . 31 . 31

Chapter 2. Language elements . . . . . . . . . . . . . . . . . . . . . . . . . 33Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 SQL identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Host identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Restrictions for distributed access . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Naming conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 SQL path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Qualification of unqualified object names . . . . . . . . . . . . . . . . . . . . . . . . . 45 Unqualified alias, index, sequence, table, trigger, and view names . . . . . . . . . . . . . . . . 45 Unqualified data type, function, procedure, and specific names . . . . . . . . . . . . . . . . . 46 Aliases and synonyms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Authorization IDs and authorization-names . . . . . . . . . . . . . . . . . . . . . . . . 47 Authorization IDs and schema names . . . . . . . . . . . . . . . . . . . . . . . . . 48 Authorization IDs and statement preparation . . . . . . . . . . . . . . . . . . . . . . . 48 Authorization IDs and dynamic SQL . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Authorization IDs and remote execution. . . . . . . . . . . . . . . . . . . . . . . . . 52 Data types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Nulls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Character strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Graphic strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Binary strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Large objects (LOBs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Datetime values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Row ID values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 XML values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Distinct types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Promotion of data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Casting between data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Assignment and comparison. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Numeric assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 String assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Datetime assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Row ID assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Distinct type assignments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Assignments to LOB locators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Numeric comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 String comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Datetime comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Row ID comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Distinct type comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Rules for result data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Numeric operands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Character and graphic string operands . . . . . . . . . . . . . . . . . . . . . . . . . 93 Binary string operands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Datetime operands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Row ID operands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Distinct type operands. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Conversion rules for operations that combine strings . . . . . . . . . . . . . . . . . . . . 95 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Integer constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Floating-point constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Decimal constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Character string constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Datetime constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

|

|

|

iv

SQL Reference

| | | |

| |

| |

| |

|

|

Graphic string constants . . . . . . . . . . . . . . . . . Special registers . . . . . . . . . . . . . . . . . . . . General rules for special registers. . . . . . . . . . . . . . CURRENT APPLICATION ENCODING SCHEME . . . . . . . . CURRENT CLIENT_ACCTNG . . . . . . . . . . . . . . CURRENT CLIENT_APPLNAME . . . . . . . . . . . . . CURRENT CLIENT_USERID . . . . . . . . . . . . . . . CURRENT CLIENT_WRKSTNNAME . . . . . . . . . . . . CURRENT DATE . . . . . . . . . . . . . . . . . . . CURRENT DEGREE . . . . . . . . . . . . . . . . . . CURRENT LOCALE LC_CTYPE . . . . . . . . . . . . . . CURRENT MEMBER . . . . . . . . . . . . . . . . . . CURRENT MAINTAINED TABLE TYPES FOR OPTIMIZATION . . . CURRENT OPTIMIZATION HINT . . . . . . . . . . . . . CURRENT PACKAGE PATH . . . . . . . . . . . . . . . CURRENT PACKAGESET . . . . . . . . . . . . . . . . CURRENT PATH . . . . . . . . . . . . . . . . . . . CURRENT PRECISION . . . . . . . . . . . . . . . . . CURRENT REFRESH AGE . . . . . . . . . . . . . . . . CURRENT RULES. . . . . . . . . . . . . . . . . . . CURRENT SCHEMA . . . . . . . . . . . . . . . . . . CURRENT SERVER . . . . . . . . . . . . . . . . . . CURRENT SQLID . . . . . . . . . . . . . . . . . . . CURRENT TIME . . . . . . . . . . . . . . . . . . . CURRENT TIMESTAMP. . . . . . . . . . . . . . . . . CURRENT TIMEZONE . . . . . . . . . . . . . . . . . USER . . . . . . . . . . . . . . . . . . . . . . . Using special registers in a user-defined function or a stored procedure . Column names . . . . . . . . . . . . . . . . . . . . . Qualified column names. . . . . . . . . . . . . . . . . Correlation names . . . . . . . . . . . . . . . . . . . Column name qualifiers to avoid ambiguity . . . . . . . . . . Column name qualifiers in correlated references. . . . . . . . . Resolution of column name qualifiers and column names . . . . . References to variables . . . . . . . . . . . . . . . . . . References to host variables . . . . . . . . . . . . . . . Host variables in dynamic SQL . . . . . . . . . . . . . . References to LOB host variables . . . . . . . . . . . . . . References to LOB locator variables . . . . . . . . . . . . . References to stored procedure result sets . . . . . . . . . . . References to result set locator variables . . . . . . . . . . . References to built-in session variables . . . . . . . . . . . . Host structures in PL/I, C, and COBOL . . . . . . . . . . . . Host-variable-arrays in PL/I, C, C++, and COBOL . . . . . . . . . Functions. . . . . . . . . . . . . . . . . . . . . . . Types of functions . . . . . . . . . . . . . . . . . . . Function invocation . . . . . . . . . . . . . . . . . . Function resolution . . . . . . . . . . . . . . . . . . Best fit consideration . . . . . . . . . . . . . . . . . . Expressions . . . . . . . . . . . . . . . . . . . . . . Without operators . . . . . . . . . . . . . . . . . . . With arithmetic operators . . . . . . . . . . . . . . . . With the concatenation operator . . . . . . . . . . . . . . Scalar-fullselect . . . . . . . . . . . . . . . . . . . . Datetime operands and durations . . . . . . . . . . . . . Datetime arithmetic in SQL. . . . . . . . . . . . . . . . Precedence of operations . . . . . . . . . . . . . . . . CASE expressions . . . . . . . . . . . . . . . . . . . CAST specification . . . . . . . . . . . . . . . . . . Sequence reference . . . . . . . . . . . . . . . . . . Predicates . . . . . . . . . . . . . . . . . . . . . .

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

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

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

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

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

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

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

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

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

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

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

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

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

101 102 103 105 106 106 107 107 108 108 108 109 110 110 111 111 112 112 113 114 115 116 116 117 117 117 118 118 120 121 121 122 123 124 126 126 128 129 129 130 130 130 132 133 134 134 135 136 140 140 142 142 146 148 149 150 155 155 158 165 169

Contents

v

|

| | | |

Basic predicate . . . . . . . . . . . . . . Quantified predicate . . . . . . . . . . . . BETWEEN predicate . . . . . . . . . . . . DISTINCT predicate . . . . . . . . . . . . EXISTS predicate . . . . . . . . . . . . . IN predicate . . . . . . . . . . . . . . . LIKE predicate . . . . . . . . . . . . . . NULL predicate . . . . . . . . . . . . . Search conditions . . . . . . . . . . . . . . Options affecting SQL . . . . . . . . . . . . SQL processing options for dynamic statements . . . Decimal point representation . . . . . . . . . Apostrophes and quotation marks in string delimiters. Katakana characters for EBCDIC . . . . . . . . Mixed data in character strings . . . . . . . . Formatting of datetime strings. . . . . . . . . SQL standard language . . . . . . . . . . . Positioned updates of columns . . . . . . . . Mappings from SQL to XML . . . . . . . . . . Mapping SQL character sets to XML character sets . . Mapping SQL identifiers to XML names . . . . . Mapping SQL data values to XML data values . . .

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

170 171 173 174 176 177 179 186 187 188 190 190 191 192 192 193 193 194 195 195 195 196

Chapter 3. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197Aggregate functions . . . . . . AVG . . . . . . . . . . COUNT . . . . . . . . . COUNT_BIG . . . . . . . MAX . . . . . . . . . . MIN . . . . . . . . . . STDDEV . . . . . . . . . SUM . . . . . . . . . . VARIANCE or VARIANCE_SAMP XMLAGG . . . . . . . . | Scalar functions . . . . . . . ABS . . . . . . . . . . ACOS . . . . . . . . . . ADD_MONTHS . . . . . . ASCII . . . . . . . . . . # ASIN . . . . . . . . . . ATAN . . . . . . . . . . ATANH . . . . . . . . . ATAN2 . . . . . . . . . BLOB . . . . . . . . . . CCSID_ENCODING . . . . . CEILING . . . . . . . . . CHAR. . . . . . . . . . CHARACTER_LENGTH . . . # CLOB . . . . . . . . . . COALESCE . . . . . . . . CONCAT. . . . . . . . . COS . . . . . . . . . . COSH . . . . . . . . . . DATE . . . . . . . . . . DAY . . . . . . . . . . DAYOFMONTH . . . . . . DAYOFWEEK . . . . . . . DAYOFWEEK_ISO . . . . . DAYOFYEAR . . . . . . . DAYS . . . . . . . . . . DBCLOB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 205 206 207 209 210 211 212 213 214 216 217 218 219 221 222 223 224 225 226 227 228 229 237 239 242 244 245 246 247 249 250 251 252 253 254 255

vi

SQL Reference

|

|

| |

# # # # # #

#

DECIMAL or DEC . . . . . . . . . . . . DECRYPT_BIT, DECRYPT_CHAR, and DECRYPT_DB. DEGREES . . . . . . . . . . . . . . . DIGITS . . . . . . . . . . . . . . . . DOUBLE_PRECISION or DOUBLE . . . . . . . ENCRYPT_TDES . . . . . . . . . . . . . EXP . . . . . . . . . . . . . . . . . FLOAT . . . . . . . . . . . . . . . . FLOOR . . . . . . . . . . . . . . . . GENERATE_UNIQUE . . . . . . . . . . . GETHINT . . . . . . . . . . . . . . . GETVARIABLE. . . . . . . . . . . . . . GRAPHIC . . . . . . . . . . . . . . . HEX . . . . . . . . . . . . . . . . . HOUR. . . . . . . . . . . . . . . . . IDENTITY_VAL_LOCAL . . . . . . . . . . IFNULL . . . . . . . . . . . . . . . . INSERT . . . . . . . . . . . . . . . . INTEGER or INT . . . . . . . . . . . . . JULIAN_DAY . . . . . . . . . . . . . . LAST_DAY . . . . . . . . . . . . . . . LCASE . . . . . . . . . . . . . . . . LEFT . . . . . . . . . . . . . . . . . LENGTH . . . . . . . . . . . . . . . . LN . . . . . . . . . . . . . . . . . . LOCATE . . . . . . . . . . . . . . . . LOG10 . . . . . . . . . . . . . . . . LOWER . . . . . . . . . . . . . . . . LTRIM . . . . . . . . . . . . . . . . MAX . . . . . . . . . . . . . . . . . MICROSECOND . . . . . . . . . . . . . MIDNIGHT_SECONDS . . . . . . . . . . . MIN . . . . . . . . . . . . . . . . . MINUTE . . . . . . . . . . . . . . . . MOD . . . . . . . . . . . . . . . . . MONTH . . . . . . . . . . . . . . . . MQPUBLISH . . . . . . . . . . . . . . MQPUBLISHXML . . . . . . . . . . . . . MQREAD . . . . . . . . . . . . . . . MQREADCLOB . . . . . . . . . . . . . MQREADXML . . . . . . . . . . . . . . MQRECEIVE . . . . . . . . . . . . . . MQRECEIVECLOB . . . . . . . . . . . . MQRECEIVEXML . . . . . . . . . . . . . MQSEND . . . . . . . . . . . . . . . MQSENDXML . . . . . . . . . . . . . . MQSENDXMLFILE . . . . . . . . . . . . MQSENDXMLFILECLOB . . . . . . . . . . MQSUBSCRIBE . . . . . . . . . . . . . MQUNSUBSCRIBE . . . . . . . . . . . . MULTIPLY_ALT . . . . . . . . . . . . . NEXT_DAY . . . . . . . . . . . . . . . NULLIF . . . . . . . . . . . . . . . . POSITION . . . . . . . . . . . . . . . POSSTR . . . . . . . . . . . . . . . . POWER . . . . . . . . . . . . . . . . QUARTER . . . . . . . . . . . . . . . RADIANS . . . . . . . . . . . . . . . RAISE_ERROR . . . . . . . . . . . . . . RAND. . . . . . . . . . . . . . . . . REAL . . . . . . . . . . . . . . . . .

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

259 261 264 265 266 268 271 272 273 274 276 277 279 283 284 285 290 291 294 296 297 298 299 302 303 304 307 308 310 311 312 313 314 315 316 318 319 321 323 325 327 328 330 332 334 336 338 340 342 344 346 347 349 350 353 355 356 357 358 359 360

Contents

vii

# #

#

#

| | | | | # # # #

REPEAT . . . . . . . . . . REPLACE . . . . . . . . . RIGHT . . . . . . . . . . ROUND . . . . . . . . . . ROUND_TIMESTAMP . . . . . ROWID . . . . . . . . . . RTRIM . . . . . . . . . . SECOND . . . . . . . . . . SIGN . . . . . . . . . . . SIN. . . . . . . . . . . . SINH . . . . . . . . . . . SMALLINT . . . . . . . . . SOAPHTTPC and SOAPHTTPV . . SOAPHTTPNC and SOAPHTTPNV . SPACE . . . . . . . . . . SQRT . . . . . . . . . . . STRIP . . . . . . . . . . . SUBSTR . . . . . . . . . . SUBSTRING . . . . . . . . . TAN . . . . . . . . . . . TANH . . . . . . . . . . . TIME . . . . . . . . . . . TIMESTAMP . . . . . . . . TIMESTAMPDIFF . . . . . . . TIMESTAMP_FORMAT . . . . . TRANSLATE . . . . . . . . TRUNCATE . . . . . . . . . TRUNC_TIMESTAMP . . . . . UCASE . . . . . . . . . . UPPER . . . . . . . . . . VALUE . . . . . . . . . . VARCHAR . . . . . . . . . VARCHAR_FORMAT . . . . . VARGRAPHIC . . . . . . . . WEEK . . . . . . . . . . . WEEK_ISO . . . . . . . . . XML2CLOB . . . . . . . . . XMLCONCAT . . . . . . . . XMLELEMENT. . . . . . . . XMLFOREST . . . . . . . . XMLNAMESPACES . . . . . . YEAR . . . . . . . . . . . Table functions . . . . . . . . . ADMIN_TASK_LIST . . . . . . ADMIN_TASK_STATUS . . . . . MQREADALL . . . . . . . . MQREADALLCLOB . . . . . . MQREADALLXML . . . . . . MQRECEIVEALL . . . . . . . MQRECEIVEALLCLOB . . . . . MQRECEIVEALLXML . . . . .

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

362 364 367 369 371 373 374 375 376 377 378 379 381 382 384 385 386 388 390 393 394 395 396 398 400 402 405 407 408 409 411 412 418 419 423 424 425 426 427 429 431 433 434 435 440 443 445 447 449 451 453

Chapter 4. Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455Authorization . . . . subselect . . . . . . select-clause . . . . from-clause . . . . where-clause . . . group-by-clause . . having-clause . . . Examples of subselects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456 458 458 462 469 470 471 471

viii

SQL Reference

|

|

fullselect . . . . . . . . . . . . . . . . Character conversion in unions and concatenations. Selecting the result CCSID . . . . . . . . . Examples of fullselects . . . . . . . . . . select-statement . . . . . . . . . . . . . common-table-expression . . . . . . . . . order-by-clause . . . . . . . . . . . . . fetch-first-clause . . . . . . . . . . . . update-clause . . . . . . . . . . . . . read-only-clause . . . . . . . . . . . . optimize-for-clause . . . . . . . . . . . isolation-clause . . . . . . . . . . . . . queryno-clause . . . . . . . . . . . . . Examples of select statements . . . . . . . .

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

476 477 477 478 479 480 482 484 484 485 486 486 488 488

Chapter 5. Statements

. . . . . . . . . . . . . . . . . . . . . . . . . . . . 491. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495 495 496 498 498 499 499 501 503 506 523 529 545 556 562 567 570 615 627 628 632 633 642 644 651 653 659 662 665 668 675 676 700 718 731 740 746 766 767 786 798 806 809 811 851

|

|

|

|

How SQL statements are invoked . . . . . . . Embedding a statement in an application program . Dynamic preparation and execution . . . . . . Static invocation of a SELECT statement . . . . Dynamic invocation of a SELECT statement . . . Interactive invocation. . . . . . . . . . . Checking the execution of SQL statements. . . . ALLOCATE CURSOR . . . . . . . . . . . ALTER DATABASE . . . . . . . . . . . . ALTER FUNCTION (external) . . . . . . . . . ALTER FUNCTION (SQL scalar) . . . . . . . . ALTER INDEX . . . . . . . . . . . . . . ALTER PROCEDURE (external) . . . . . . . . ALTER PROCEDURE (SQL) . . . . . . . . . ALTER SEQUENCE . . . . . . . . . . . . ALTER STOGROUP . . . . . . . . . . . . ALTER TABLE . . . . . . . . . . . . . . ALTER TABLESPACE . . . . . . . . . . . ALTER VIEW . . . . . . . . . . . . . . ASSOCIATE LOCATORS . . . . . . . . . . BEGIN DECLARE SECTION . . . . . . . . . CALL . . . . . . . . . . . . . . . . . CLOSE . . . . . . . . . . . . . . . . COMMENT . . . . . . . . . . . . . . . COMMIT. . . . . . . . . . . . . . . . CONNECT . . . . . . . . . . . . . . . CREATE ALIAS . . . . . . . . . . . . . CREATE AUXILIARY TABLE . . . . . . . . . CREATE DATABASE . . . . . . . . . . . . CREATE DISTINCT TYPE . . . . . . . . . . CREATE FUNCTION. . . . . . . . . . . . CREATE FUNCTION (external scalar) . . . . . . CREATE FUNCTION (external table) . . . . . . CREATE FUNCTION (sourced) . . . . . . . . CREATE FUNCTION (SQL scalar) . . . . . . . CREATE GLOBAL TEMPORARY TABLE . . . . . CREATE INDEX . . . . . . . . . . . . . CREATE PROCEDURE . . . . . . . . . . . CREATE PROCEDURE (external). . . . . . . . CREATE PROCEDURE (SQL) . . . . . . . . . CREATE SEQUENCE. . . . . . . . . . . . CREATE STOGROUP . . . . . . . . . . . CREATE SYNONYM . . . . . . . . . . . . CREATE TABLE . . . . . . . . . . . . . CREATE TABLESPACE . . . . . . . . . . .

Contents

ix

CREATE TRIGGER . . . . . . . . . CREATE VIEW . . . . . . . . . . . DECLARE CURSOR . . . . . . . . . DECLARE GLOBAL TEMPORARY TABLE . DECLARE STATEMENT . . . . . . . DECLARE TABLE . . . . . . . . . . DECLARE VARIABLE . . . . . . . . DELETE . . . . . . . . . . . . . # DESCRIBE . . . . . . . . . . . . DESCRIBE CURSOR . . . . . . . . . DESCRIBE INPUT. . . . . . . . . . # DESCRIBE OUTPUT . . . . . . . . . DESCRIBE PROCEDURE . . . . . . . # DESCRIBE TABLE. . . . . . . . . . DROP . . . . . . . . . . . . . . END DECLARE SECTION . . . . . . . EXECUTE . . . . . . . . . . . . EXECUTE IMMEDIATE . . . . . . . . EXPLAIN . . . . . . . . . . . . FETCH . . . . . . . . . . . . . FREE LOCATOR . . . . . . . . . . | GET DIAGNOSTICS . . . . . . . . GRANT . . . . . . . . . . . . . GRANT (collection privileges) . . . . . GRANT (database privileges) . . . . . GRANT (distinct type or JAR privileges) . . GRANT (function or procedure privileges) . GRANT (package privileges) . . . . . . GRANT (plan privileges) . . . . . . . GRANT (schema privileges) . . . . . . | GRANT (sequence privileges) . . . . . GRANT (system privileges) . . . . . . GRANT (table or view privileges) . . . . GRANT (use privileges) . . . . . . . HOLD LOCATOR . . . . . . . . . INCLUDE . . . . . . . . . . . . INSERT . . . . . . . . . . . . . LABEL . . . . . . . . . . . . . LOCK TABLE . . . . . . . . . . . OPEN . . . . . . . . . . . . . PREPARE . . . . . . . . . . . . | REFRESH TABLE. . . . . . . . . . RELEASE (connection) . . . . . . . . RELEASE SAVEPOINT . . . . . . . . RENAME . . . . . . . . . . . . REVOKE . . . . . . . . . . . . REVOKE (collection privileges) . . . . . REVOKE (database privileges) . . . . . REVOKE (distinct type or JAR privileges) . REVOKE (function or procedure privileges) . REVOKE (package privileges) . . . . . REVOKE (plan privileges) . . . . . . . REVOKE (schema privileges) . . . . . . | REVOKE (sequence privileges) . . . . . REVOKE (system privileges) . . . . . . REVOKE (table or view privileges) . . . . REVOKE (use privileges) . . . . . . . ROLLBACK . . . . . . . . . . . SAVEPOINT . . . . . . . . . . . SELECT . . . . . . . . . . . . . SELECT INTO. . . . . . . . . . .

. 872 . 884 . 891 . 903 . 915 . 916 . 922 . 925 . 933 . 934 . 936 . 939 . 946 . 949 . 951 . 965 . 966 . 971 . 974 . 992 . . . . . . . . . . . . . . . . . . . . . . . 1017 . . . . . . . . . . . . . . . . . . . . . . . 1018 . . . . . . . . . . . . . . . . . . . . . . . 1031 . . . . . . . . . . . . . . . . . . . . . . . 1034 . . . . . . . . . . . . . . . . . . . . . . . 1035 . . . . . . . . . . . . . . . . . . . . . . . 1038 . . . . . . . . . . . . . . . . . . . . . . . 1040 . . . . . . . . . . . . . . . . . . . . . . . 1045 . . . . . . . . . . . . . . . . . . . . . . . 1047 . . . . . . . . . . . . . . . . . . . . . . . 1048 . . . . . . . . . . . . . . . . . . . . . . . 1050 . . . . . . . . . . . . . . . . . . . . . . . 1051 . . . . . . . . . . . . . . . . . . . . . . . 1054 . . . . . . . . . . . . . . . . . . . . . . . 1057 . . . . . . . . . . . . . . . . . . . . . . . 1059 . . . . . . . . . . . . . . . . . . . . . . . 1060 . . . . . . . . . . . . . . . . . . . . . . . 1062 . . . . . . . . . . . . . . . . . . . . . . . 1077 . . . . . . . . . . . . . . . . . . . . . . . 1079 . . . . . . . . . . . . . . . . . . . . . . . 1081 . . . . . . . . . . . . . . . . . . . . . . . 1086 . . . . . . . . . . . . . . . . . . . . . . . 1102 . . . . . . . . . . . . . . . . . . . . . . . 1104 . . . . . . . . . . . . . . . . . . . . . . . 1107 . . . . . . . . . . . . . . . . . . . . . . . 1108 . . . . . . . . . . . . . . . . . . . . . . . 1111 . . . . . . . . . . . . . . . . . . . . . . . 1116 . . . . . . . . . . . . . . . . . . . . . . . 1117 . . . . . . . . . . . . . . . . . . . . . . . 1120 . . . . . . . . . . . . . . . . . . . . . . . 1122 . . . . . . . . . . . . . . . . . . . . . . . 1127 . . . . . . . . . . . . . . . . . . . . . . . 1129 . . . . . . . . . . . . . . . . . . . . . . . 1130 . . . . . . . . . . . . . . . . . . . . . . . 1132 . . . . . . . . . . . . . . . . . . . . . . . 1134 . . . . . . . . . . . . . . . . . . . . . . . 1137 . . . . . . . . . . . . . . . . . . . . . . . 1140 . . . . . . . . . . . . . . . . . . . . . . . 1142 . . . . . . . . . . . . . . . . . . . . . . . 1145 . . . . . . . . . . . . . . . . . . . . . . . 1147 . . . . . . . . . . . . . . . . . . . . . . . 1148

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

x

SQL Reference

| | | | |

SET CONNECTION . . . . . . . . . . . . . . . . . SET CURRENT APPLICATION ENCODING SCHEME . . . . . SET CURRENT DEGREE . . . . . . . . . . . . . . . SET CURRENT LOCALE LC_CTYPE . . . . . . . . . . . SET CURRENT MAINTAINED TABLE TYPES FOR OPTIMIZATION SET CURRENT OPTIMIZATION HINT . . . . . . . . . . SET CURRENT PACKAGE PATH . . . . . . . . . . . . SET CURRENT PACKAGESET . . . . . . . . . . . . . SET CURRENT PRECISION . . . . . . . . . . . . . . SET CURRENT REFRESH AGE . . . . . . . . . . . . . SET CURRENT RULES . . . . . . . . . . . . . . . . SET CURRENT SQLID . . . . . . . . . . . . . . . . SET ENCRYPTION PASSWORD. . . . . . . . . . . . . SET host-variable assignment. . . . . . . . . . . . . . SET PATH . . . . . . . . . . . . . . . . . . . . SET SCHEMA . . . . . . . . . . . . . . . . . . . SET transition-variable assignment . . . . . . . . . . . . SIGNAL SQLSTATE . . . . . . . . . . . . . . . . . UPDATE . . . . . . . . . . . . . . . . . . . . VALUES. . . . . . . . . . . . . . . . . . . . . VALUES INTO . . . . . . . . . . . . . . . . . . WHENEVER . . . . . . . . . . . . . . . . . . .

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1152 1154 1155 1157 1159 1161 1162 1166 1168 1169 1171 1172 1174 1176 1179 1182 1185 1188 1189 1200 1201 1203

Chapter 6. SQL control statements|References to SQL parameters and SQL-procedure-statement . . . assignment-statement . . . . CALL statement . . . . . . CASE statement . . . . . . compound-statement . . . . GET DIAGNOSTICS statement . GOTO statement . . . . . . IF statement . . . . . . . ITERATE statement . . . . . LEAVE statement. . . . . . LOOP statement . . . . . . REPEAT statement . . . . . RESIGNAL statement . . . . RETURN statement . . . . . SIGNAL statement . . . . . WHILE statement . . . . .

. . . . . . . . . . . . . . . . . . . . . . 1205. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1206 1207 1208 1210 1212 1214 1219 1220 1222 1223 1224 1225 1226 1227 1230 1232 1236

| |

| | |

SQL variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Appendix A. Limits in DB2 UDB for z/OS . . . . . . . . . . . . . . . . . . . . 1237 Appendix B. Reserved schema names and reserved words . . . . . . . . . . . . 1243Reserved schema names . Reserved words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1243 . 1243

Appendix C. Characteristics of SQL statements in DB2 UDB for z/OS. . . . . . . . 1247Actions allowed on SQL statements . . . . . . . . . . . SQL statements allowed in external functions and stored procedures SQL statements allowed in SQL procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1247 . 1250 . 1252

Appendix D. SQL communication area (SQLCA) . . . . . . . . . . . . . . . . . 1257Description of SQLCA fields . The included SQLCA . . . The REXX SQLCA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1257 . 1260 . 1262

Appendix E. SQL descriptor area (SQLDA) . . . . . . . . . . . . . . . . . . . 1265Contents

xi

Field descriptions . . . . . . . . The SQLDA Header . . . . . . . SQLVAR entries . . . . . . . . Unrecognized and unsupported SQLTYPES The included SQLDA . . . . . . . Identifying an SQLDA in C or C++. . . The REXX SQLDA . . . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

1266 1266 1267 1276 1276 1282 1282

Appendix F. DB2 catalog tables . . . . . . . . . . . . . . . . . . . . . . . . 1285|Table spaces and indexes . . . . . . Catalog table space buffer pools . . . SQL statements allowed on the catalog Reorganizing the catalog . . . . . New and changed catalog tables . . . SYSIBM.IPLIST table . . . . . . . SYSIBM.IPNAMES table . . . . . . SYSIBM.LOCATIONS table . . . . . SYSIBM.LULIST table . . . . . . . SYSIBM.LUMODES table . . . . . . SYSIBM.LUNAMES table . . . . . . SYSIBM.MODESELECT table . . . . . SYSIBM.SYSAUXRELS table . . . . . SYSIBM.SYSCHECKDEP table . . . . SYSIBM.SYSCHECKS table . . . . . SYSIBM.SYSCHECKS2 table . . . . . SYSIBM.SYSCOLAUTH table. . . . . SYSIBM.SYSCOLDIST table . . . . . SYSIBM.SYSCOLDISTSTATS table . . . SYSIBM.SYSCOLDIST_HIST table . . . SYSIBM.SYSCOLSTATS table . . . . . SYSIBM.SYSCOLUMNS table. . . . . SYSIBM.SYSCOLUMNS_HIST table . . SYSIBM.SYSCONSTDEP table . . . . SYSIBM.SYSCOPY table . . . . . . SYSIBM.SYSDATABASE table . . . . SYSIBM.SYSDATATYPES table . . . . SYSIBM.SYSDBAUTH table . . . . . SYSIBM.SYSDBRM table . . . . . . SYSIBM.SYSDUMMY1 table . . . . . SYSIBM.SYSFIELDS table . . . . . . SYSIBM.SYSFOREIGNKEYS table . . . SYSIBM.SYSINDEXES table . . . . . SYSIBM.SYSINDEXES_HIST table . . . SYSIBM.SYSINDEXPART table . . . . SYSIBM.SYSINDEXPART_HIST table . . SYSIBM.SYSINDEXSTATS table . . . . SYSIBM.SYSINDEXSTATS_HIST table . . SYSIBM.SYSJARCLASS_SOURCE table . SYSIBM.SYSJARCONTENTS table . . . SYSIBM.SYSJARDATA table . . . . . SYSIBM.SYSJAROBJECTS table . . . . SYSIBM.SYSJAVAOPTS table . . . . . SYSIBM.SYSKEYCOLUSE table . . . . SYSIBM.SYSKEYS table . . . . . . SYSIBM.SYSLOBSTATS table . . . . . SYSIBM.SYSLOBSTATS_HIST table . . . SYSIBM.SYSPACKAGE table . . . . . SYSIBM.SYSPACKAUTH table . . . . SYSIBM.SYSPACKDEP table . . . . . SYSIBM.SYSPACKLIST table . . . . . SYSIBM.SYSPACKSTMT table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1286 1290 1291 1293 1294 1302 1303 1306 1307 1308 1309 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1328 1330 1331 1335 1337 1338 1340 1341 1342 1343 1344 1348 1349 1352 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1371 1372 1373 1374

|

xii

SQL Reference

SYSIBM.SYSPARMS table . . . . SYSIBM.SYSPKSYSTEM table . . SYSIBM.SYSPLAN table . . . . SYSIBM.SYSPLANAUTH table . . SYSIBM.SYSPLANDEP table . . . SYSIBM.SYSPLSYSTEM table. . . SYSIBM.SYSRELS table . . . . . SYSIBM.SYSRESAUTH table . . . SYSIBM.SYSROUTINEAUTH table . SYSIBM.SYSROUTINES table. . . SYSIBM.SYSROUTINES_OPTS table SYSIBM.SYSROUTINES_SRC table . SYSIBM.SYSSCHEMAAUTH table . SYSIBM.SYSSEQUENCEAUTH table SYSIBM.SYSSEQUENCES table . . SYSIBM.SYSSEQUENCESDEP table SYSIBM.SYSSTMT table . . . . SYSIBM.SYSSTOGROUP table . . SYSIBM.SYSSTRINGS table . . . SYSIBM.SYSSYNONYMS table . . SYSIBM.SYSTABAUTH table . . . SYSIBM.SYSTABCONST table . . SYSIBM.SYSTABLEPART table . . SYSIBM.SYSTABLEPART_HIST table SYSIBM.SYSTABLES table . . . . SYSIBM.SYSTABLESPACE table . . SYSIBM.SYSTABLES_HIST table. . SYSIBM.SYSTABSTATS table . . . SYSIBM.SYSTABSTATS_HIST table . SYSIBM.SYSTRIGGERS table . . . SYSIBM.SYSUSERAUTH table . . SYSIBM.SYSVIEWDEP table . . . SYSIBM.SYSVIEWS table . . . . SYSIBM.SYSVOLUMES table . . . SYSIBM.USERNAMES table . . .

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1377 1380 1381 1385 1386 1387 1388 1389 1390 1391 1398 1399 1400 1401 1402 1404 1405 1408 1409 1411 1412 1414 1415 1419 1421 1426 1429 1430 1431 1432 1433 1436 1437 1439 1440

Appendix G. Using the catalog in database design . . . . . . . . . . . . . . . . 1441|Retrieving catalog information about DB2 storage groups . Retrieving catalog information about a table. . . . . . Retrieving catalog information about partition order . . . Retrieving catalog information about aliases. . . . . . Retrieving catalog information about columns . . . . . Retrieving catalog information about indexes . . . . . Retrieving catalog information about views . . . . . . Retrieving catalog information about authorizations . . . Retrieving catalog information about parent keys . . . . Retrieving catalog information about foreign keys . . . . Retrieving catalog information about check pending . . . Retrieving catalog information about check constraints . . Retrieving catalog information about LOBs . . . . . . Retrieving catalog information about user-defined functions Retrieving catalog information about triggers . . . . . Retrieving catalog information about sequences . . . . Adding and retrieving comments . . . . . . . . . Verifying the accuracy of the database definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . and stored procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1441 1441 1441 1442 1442 1443 1443 1443 1444 1444 1445 1445 1445 1446 1446 1446 1447 1447

|

Appendix H. Sample user-defined functions. . . . . . . . . . . . . . . . . . . 1449ALTDATE . . ALTTIME . . CURRENCY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1450 . 1453 . 1455

Contents

xiii

DAYNAME . . . MONTHNAME . . TABLE_LOCATION . TABLE_NAME . . TABLE_SCHEMA . WEATHER . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

1457 1458 1459 1461 1463 1465

Notices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1467Programming interface information Trademarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1468 . 1469

Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1471 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1505 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . X-1 .

xiv

SQL Reference

About this bookThis book is a reference for Structured Query Language (SQL) for DB2 Universal Database for z/OS. Unless otherwise stated, references to SQL in this book imply SQL for DB2 UDB for z/OS, and all objects described in this book are objects of DB2 UDB for z/OS. The syntax and semantics of most SQL statements are essentially the same in all IBM relational database products, and the language elements common to the products provide a base for the definition of IBM SQL. Consult IBM DB2 Universal Database SQL Reference for Cross-Platform Development if you intend to develop applications that adhere to IBM SQL. This information assumes that your DB2 subsystem is running in Version 8 new-function mode. New functions are available only in new-function mode, unless explicitly stated otherwise in the product documentation. A few general exceptions exist for utilities and for optimization. In most cases, new functions are not supported in compatibility mode unless noted. For utilities and optimization, new functions are available in compatibility mode unless noted. The new functions that are available in compatibility mode and enabling-new-function mode are almost identical, but some new functions are supported to provide easier migration. Exceptions to these general statements are noted in the information.

Important In this version of DB2 UDB for z/OS, the DB2 Utilities Suite is available as an optional product. You must separately order and purchase a license to such utilities, and discussion of those utility functions in this publication is not intended to otherwise imply that you have a license to them. See Part 1 of DB2 Utility Guide and Reference for packaging details. The DB2 Utilities Suite is designed to work with the DFSORT program, which you are licensed to use in support of the DB2 utilities even if you do not otherwise license DFSORT or DB2 Sort for general use. If your primary sort product is not DFSORT or DB2 Sort, consider the following informational APARs mandatory reading: v II14047/II14213: USE OF DFSORT BY DB2 UTILITIES v II13495: HOW DFSORT TAKES ADVANTAGE OF 64-BIT REAL ARCHITECTURE These informational APARs are periodically updated. Visit the following Web site for information about ordering DB2 books and obtaining other valuable information about DB2 UDB for z/OS: http://www.ibm.com/software/data/db2/zos/library.html

Who should read this bookThis book is intended for end users, application programmers, system and database administrators, and for persons involved in error detection and diagnosis.

Copyright IBM Corp. 1982, 2012

xv

This book is a reference rather than a tutorial. It assumes that you are already familiar with SQL programming concepts. When you first use this book, consider reading Chapters 1 and 2 sequentially. These chapters describe the basic concepts of relational databases and SQL, the basic syntax of SQL, and the language elements that are common to many SQL statements. The rest of the chapters and appendixes are designed for the quick location of answers to specific SQL questions. They provide you with query forms, SQL statements, SQL procedure statements, DB2 limits, SQLCA, SQLDA, catalog tables, and SQL reserved words.

Conventions and terminology used in this bookThis section explains conventions and terminology used in this book.

Terminology and citationsIn this information, DB2 Universal Database for z/OS is referred to as "DB2 UDB for z/OS." In cases where the context makes the meaning clear, DB2 UDB for z/OS is referred to as "DB2." When this information refers to titles of books in this library, a short title is used. (For example, "See DB2 SQL Reference" is a citation to IBM DB2 Universal Database for z/OS SQL Reference.) When referring to a DB2 product other than DB2 UDB for z/OS, this information uses the product's full name to avoid ambiguity. The following terms are used as indicated: DB2 # # # # # # Represents either the DB2 licensed program or a particular DB2 subsystem.

OMEGAMON Refers to any of the following products: v IBM Tivoli OMEGAMON XE for DB2 Performance Expert on z/OS v IBM Tivoli OMEGAMON XE for DB2 Performance Monitor on z/OS v IBM DB2 Performance Expert for Multiplatforms and Workgroups v IBM DB2 Buffer Pool Analyzer for z/OS C, C++, and C language Represent the C or C++ programming language. CICS Represents CICS Transaction Server for z/OS or CICS Transaction Server for OS/390. IMS Represents the IMS Database Manager or IMS Transaction Manager. MVS Represents the MVS element of the z/OS operating system, which is equivalent to the Base Control Program (BCP) component of the z/OS operating system. RACF Represents the functions that are provided by the RACF component of the z/OS Security Server.

Conventions for describing mixed data valuesAt sites using a double-byte character set (DBCS), character strings can include a mixture of single-byte and double-byte characters. When mixed data values are shown in the examples, the conventions shown in Figure 1 on page xvii apply:

xvi

SQL Reference

Figure 1. Conventions used when mixed data values are shown in examples

| | | | | | | | | | | | | | | | | |

Industry standardsDB2 UDB for z/OS is consistent with the following industry standards for SQL: v Information technology - Database languages - SQL- Part 1: Framework (SQL/Framework) ISO/IEC 9075-1:1999 v Information technology - Database languages - SQL- Part 2: Foundation (SQL/Foundation) ISO/IEC 9075-2:1999 v Information technology - Database languages - SQL- Part 4: Persistent Stored Modules (SQL/PSM) ISO/IEC 9075-4:1999 v Information technology - Database languages - SQL- Part 5: Host Language Bindings (SQL/Bindings) ISO/IEC 9075-5:1999 v Information technology - Database languages - SQL- Part 10: Object Language (SQL/OLB) ISO/IEC 9075-10:2000 v Information technology - Database languages - SQL- Part 13: SQL Routines and Types Using the Java Programming Language (SQL/JRT) ISO/IEC 9075-13:2002 v Information technology - Database languages - SQL- Part 14: XML-Related Specifications (SQL/XML) ISO/IEC 9075-14:2003 v ANSI (American National Standards Institute) X3.135-1999, Database Language SQL

How to read the syntax diagramsThe following rules apply to the syntax diagrams that are used in this book: v Read the syntax diagrams from left to right, from top to bottom, following the path of the line. The symbol indicates the beginning of a statement. The symbol indicates that the statement syntax is continued on the next line. The symbol indicates that a statement is continued from the previous line. The symbol indicates the end of a statement. v Required items appear on the horizontal line (the main path).required_item

About this book

xvii

v Optional items appear below the main path.required_item optional_item

If an optional item appears above the main path, that item has no effect on the execution of the statement and is used only for readability.optional_item required_item

v If you can choose from two or more items, they appear vertically, in a stack. If you must choose one of the items, one item of the stack appears on the main path.required_item required_choice1 required_choice2

If choosing one of the items is optional, the entire stack appears below the main path.required_item optional_choice1 optional_choice2

If one of the items is the default, it appears above the main path and the remaining choices are shown below.default_choice required_item optional_choice optional_choice

v An arrow returning to the left, above the main line, indicates an item that can be repeated.

required_item

repeatable_item

If the repeat arrow contains a comma, you must separate repeated items with a comma., required_item repeatable_item

A repeat arrow above a stack indicates that you can repeat the items in the stack. v Keywords appear in uppercase (for example, FROM). They must be spelled exactly as shown. Variables appear in all lowercase letters (for example, column-name). They represent user-supplied names or values. v If punctuation marks, parentheses, arithmetic operators, or other such symbols are shown, you must enter them as part of the syntax.

xviii

SQL Reference

AccessibilityAccessibility features help a user who has a physical disability, such as restricted mobility or limited vision, to use software products. The major accessibility features in z/OS products, including DB2 UDB for z/OS, enable users to: v Use assistive technologies such as screen reader and screen magnifier software v Operate specific or equivalent features by using only a keyboard v Customize display attributes such as color, contrast, and font size Assistive technology products, such as screen readers, function with the DB2 UDB for z/OS user interfaces. Consult the documentation for the assistive technology products for specific information when you use assistive technology to access these interfaces. Online documentation for Version 8 of DB2 UDB for z/OS is available in the Information management software for z/OS solutions information center, which is an accessible format when used with assistive technologies such as screen reader or screen magnifier software. The Information management software for z/OS solutions information center is available at the following Web site: http://publib.boulder.ibm.com/infocenter/dzichelp

How to send your commentsYour feedback helps IBM to provide quality information. Please send any comments that you have about this book or other DB2 UDB for z/OS documentation. You can use the following methods to provide comments: v Send your comments by e-mail to [email protected] and include the name of the product, the version number of the product, and the number of the book. If you are commenting on specific text, please list the location of the text (for example, a chapter and section title or a help topic title). v You can send comments from the Web. Visit the DB2 for z/OS - Technical Resources Web site at: http://www.ibm.com/support/docview.wss?&uid=swg27011656 This Web site has a an online reader comment form that you can use to send comments. v You can also send comments by using the feedback link at the footer of each page in the Information Management Software for z/OS Solutions Information Center at http://publib.boulder.ibm.com/infocenter/db2zhelp.

About this book

xix

xx

SQL Reference

Summary of changes to this bookThe major changes to this book are: Chapter 1, DB2 concepts includes new descriptions of materialized query tables, referential constraints, and mixed-byte character strings. Chapter 2, Language elements contains numerous changes to descriptions of naming conventions, assignment and comparison, constants, special registers (including several new special registers), host variable arrays, functions, expressions (including the CAST specification and new expressions scalar fullselect, NEXT VALUE, and PREVIOUS VALUE), and predicates (including the new predicate DISTINCT). Chapter 3, Functions includes changes to many column, scalar, and table functions and addition of several new functions. Also, the term aggregate functions is now used to refer to the functions that were previously called column functions. The new functions are: v DECRYPT_BIT, DECRYPT_CHAR, and DECRYPT_DB on page 261 v ENCRYPT_TDES on page 268 v GETHINT on page 276 v GETVARIABLE on page 277 v XML2CLOB on page 425 v XMLAGG on page 214 v XMLCONCAT on page 426 v XMLELEMENT on page 427 v XMLFOREST on page 429 v XMLNAMESPACES on page 431 For a list and brief description of all the functions, see Table 46 on page 197. Chapter 4, Queries includes changes for subselect and select-statement. For subselect, the description of the select list is enhanced, the GROUP BY clause is changed to allow expressions, and the FROM clause is expanded to support specifying a cardinality when using a table function and specifying an INSERT statement, which allows selecting values from rows that are being inserted. For select-statement, the description is changed to include support of nested table expressions. Chapter 5. Statements includes new statements, as well as changed statements. The new statements are: v ALTER SEQUENCE on page 562 v ALTER VIEW on page 627 v CREATE SEQUENCE on page 798 v GET DIAGNOSTICS on page 1018 v GRANT (sequence privileges) on page 1050 v REFRESH TABLE on page 1102 v REVOKE (sequence privileges) on page 1132 v SET CURRENT MAINTAINED TABLE TYPES FOR OPTIMIZATION on page 1159 v SET CURRENT PACKAGE PATH on page 1162 v SET CURRENT REFRESH AGE on page 1169 v SET SCHEMA on page 1182 Copyright IBM Corp. 1982, 2012

xxi

Statements with new clauses, new values for existing clauses, or other changes include: v ALTER FUNCTION (external) on page 506 v ALTER FUNCTION (SQL scalar) on page 523 v ALTER INDEX on page 529 v ALTER PROCEDURE (external) on page 545 v ALTER PROCEDURE (SQL) on page 556 v ALTER TABLE on page 570 v ALTER TABLESPACE on page 615 v COMMENT on page 644 v CREATE AUXILIARY TABLE on page 662 v CREATE DISTINCT TYPE on page 668 v CREATE FUNCTION (external scalar) on page 676 v CREATE FUNCTION (external table) on page 700 v CREATE INDEX on page 746 v CREATE PROCEDURE (external) on page 767 v CREATE PROCEDURE (SQL) on page 786 v CREATE TABLE on page 811 v CREATE TABLESPACE on page 851 v DECLARE CURSOR on page 891 v DELETE on page 925 v DROP on page 951 v EXECUTE on page 966 v EXECUTE IMMEDIATE on page 971 v EXPLAIN on page 974 v FETCH on page 992 v INSERT on page 1062 v LOCK TABLE on page 1079 v PREPARE on page 1086 v REVOKE (table or view privileges) on page 1137 v SELECT INTO on page 1148 v SET CURRENT PACKAGESET on page 1166 v SET PATH on page 1179 v UPDATE on page 1189 Chapter 6, SQL control statements includes changes to statements that can be used in SQL procedures and addition of new statements. The new statements that can be used in SQL procedures are: v ITERATE statement on page 1223 v RESIGNAL statement on page 1227 v RETURN statement on page 1230 v SIGNAL statement on page 1232 Appendix F, DB2 catalog tables includes descriptions of new and changed catalog tables. (See New and changed catalog tables on page 1294 for a summary of all catalog table changes.)

xxii

SQL Reference

Chapter 1. DB2 conceptsStructured query language. . . . . . . . . . 2 Static SQL . . . . . . . . . . . . . . 2 Dynamic SQL . . . . . . . . . . . . . 2 Deferred embedded SQL . . . . . . . . . 2 Interactive SQL . . . . . . . . . . . . 3 SQL Call Level Interface (CLI) and Open Database Connectivity (ODBC) . . . . . . . . . . 3 Java database connectivity and embedded SQL for Java . . . . . . . . . . . . . . . . 3 Schemas . . . . . . . . . . . . . . . . 3 Tables . . . . . . . . . . . . . . . . 4 Indexes . . . . . . . . . . . . . . . . 5 Keys . . . . . . . . . . . . . . . . . 5 Unique keys . . . . . . . . . . . . . 5 Primary keys . . . . . . . . . . . . . 6 Parent keys . . . . . . . . . . . . . . 6 Foreign keys . . . . . . . . . . . . . 6 Constraints . . . . . . . . . . . . . . . 6 Unique constraints . . . . . . . . . . . 7 Referential constraints . . . . . . . . . . 7 Check constraints . . . . . . . . . . . . 9 Triggers . . . . . . . . . . . . . . . 10 Storage structures . . . . . . . . . . . . 10 Storage groups . . . . . . . . . . . . . 11 Databases . . . . . . . . . . . . . . . 11 Catalog . . . . . . . . . . . . . . . . 11 Views . . . . . . . . . . . . . . . . 11 Sequences . . . . . . . . . . . . . . . 12 Routines . . . . . . . . . . . . . . . Functions . . . . . . . . . . . . . . Stored procedures . . . . . . . . . . . Application processes, concurrency, and recovery . . Locking, commit, and rollback . . . . . . . Unit of work . . . . . . . . . . . . . Unit of recovery . . . . . . . . . . . . Rolling back work . . . . . . . . . . . Rolling back all changes . . . . . . . . Rolling back selected changes using savepoints . . . . . . . . . . . . . Packages and application plans . . . . . . . . Distributed data . . . . . . . . . . . . . Connections . . . . . . . . . . . . . Distributed unit of work . . . . . . . . . Connection management . . . . . . . . SQL connection states . . . . . . . . . Application process connection states. . . . When a connection is ended . . . . . . . Remote unit of work . . . . . . . . . . Connection management . . . . . . . . Character conversion . . . . . . . . . . . Character sets and code pages . . . . . . . Coded character sets and CCSIDs . . . . . . Determining the encoding scheme and CCSID of a string . . . . . . . . . . . . . . . Expanding conversions . . . . . . . . . Contracting conversions . . . . . . . . . 13 13 13 14 14 14 15 15 15 16 16 17 17 18 18 19 20 21 21 21 23 25 28 28 31 31

| | | | |

| | |

|

Copyright IBM Corp. 1982, 2012

1

DB2 concepts

Structured query languageStructured query language (SQL) is a standardized language for defining and manipulating data in a relational database. In accordance with the relational model of data, the database is perceived as a set of tables, relationships are represented by values in tables, and data is retrieved by specifying a result table that can be derived from one or more tables. DB2 UDB for z/OS transforms the specification of a result table into a sequence of internal operations that optimize data retrieval. This transformation occurs when the SQL statement is prepared. This transformation is also known as binding. All executable SQL statements must be prepared before they can be executed. The result of preparation is the executable or operational form of the statement. The method of preparing an SQL statement and the persistence of its operational form distinguish static SQL from dynamic SQL.

Static SQLThe source form of a static SQL statement is embedded within an application program written in a host language such as COBOL. The statement is prepared before the program is executed and the operational form of the statement persists beyond the execution of the program. Static SQL statements in a source program must be processed before the program is compiled. This processing can be accomplished through the DB2 precompiler or the SQL statement coprocessor. The DB2 precompiler or the coprocessor checks the syntax of the SQL statements, turns them into host language comments, and generates host language statements to invoke DB2. The preparation of an SQL application program includes precompilation, the preparation of its static SQL statements, and compilation of the modified source program, as described in Part 5 of DB2 Application Programming and SQL Guide.

Dynamic SQLPrograms that contain embedded dynamic SQL statements must be precompiled like those that contain static SQL, but unlike static SQL, the dynamic statements are constructed and prepared at run time. The source form of a dynamic statement is a character string that is passed to DB2 by the program using the static SQL PREPARE or EXECUTE IMMEDIATE statement. A statement that is prepared using the PREPARE statement can be referenced in a DECLARE CURSOR, DESCRIBE, or EXECUTE statement. Whether the operational form of the statement is persistent depends on whether dynamic statement caching is enabled. For details on dynamic statement caching, see Part 6 of DB2 Application Programming and SQL Guide. SQL statements embedded in a REXX application are dynamic SQL statements. SQL statements submitted to an interactive SQL facility and to the CALL Level Interface (CLI) are also dynamic SQL.

Deferred embedded SQLA deferred embedded SQL statement is neither fully static nor fully dynamic. Like a static statement, it is embedded within an application, but like a dynamic statement, it is prepared during the execution of the application. Although prepared at run time, a deferred embedded SQL statement is processed with bind-time rules such that the authorization ID and qualifier determined at bind

2

SQL Reference

DB2 conceptstime for the plan or package owner are used. Deferred embedded SQL statements are used for DB2 private protocol access to remote data.

Interactive SQLIn this book, interactive SQL refers to SQL statements submitted to SPUFI (SQL processor using file input). SPUFI prepares and executes these statements dynamically. For more details about using SPUFI, see Part 1 of DB2 Application Programming and SQL Guide.

SQL Call Level Interface (CLI) and Open Database Connectivity (ODBC)The DB2 Call Level Interface (CLI) is an application programming interface in which functions are provided to application programs to process dynamic SQL statements. DB2 CLI allows users to access SQL functions directly through a call interface. CLI programs can also be compiled using an Open Database Connectivity (ODBC) Software Developers Kit, available from Microsoft or other vendors, enabling access to ODBC data sources. Unlike using embedded SQL, no precompilation is required. Applications developed using this interface can be executed on a variety of databases without being compiled against each of databases. Through the interface, applications use procedure calls at execution time to connect to databases, to issue SQL statements, and to get returned data and status information. The DB2 ODBC Guide and Reference describes the APIs supported with this interface.

Java database connectivity and embedded SQL for JavaDB2 provides two standards-based Java programming APIs: Java Database Connectivity (JDBC) and embedded SQL for Java (SQLJ). Both can be used to create Java applications and applets that access DB2. Static SQL cannot be used by JDBC. SQLJ applications use JDBC as a foundation for such tasks as connecting to databases and handling SQL errors, but can contain embedded static SQL statements in the SQLJ source files. An SQLJ file has to be translated with the SQLJ translator before the resulting Java source code can be compiled. The DB2 Application Programming Guide and Reference for Java describes the APIs supported with these interfaces.

SchemasA schema is a collection of named objects. The objects that a schema can contain include distinct types, functions, stored procedures, sequences, and triggers. An object is assigned to a schema when it is created. The schema name of the object determines the schema to which the object belongs. When a distinct type, function, sequence, or trigger is created, it is given a qualified, two-part name. The first part is the schema name (or the qualifier), which is either implicitly or explicitly specified. The second part is the name of the object. When a stored procedure is created, it is given a three-part name. The first part is a location name, which is implicitly or explicitly specified, the second part is the schema name, which is implicitly or explicitly specified, and the third part is the name of the object.Chapter 1. DB2 concepts

3

DB2 conceptsSchemas extend the concept of qualifiers for tables, views, indexes, and aliases to enable the qualifiers for distinct types, functions, stored procedures, sequences, and triggers to be called schema names.

TablesTables are logical structures maintained by DB2. Tables are made up of columns and rows. There is no inherent order of the rows within a table. At the intersection of every column and row is a specific data item called a value. A column is a set of values of the same type. A row is a sequence of values such that the nth value is a value of the nth column of the table. Every table must have one or more columns, but the number of rows can be zero. Some types of tables include: base table A table created with the SQL statement CREATE TABLE and used to hold persistent user data. auxiliary table A table created with the SQL statement CREATE AUXILIARY TABLE and used to hold the data for a column that is defined in a base table. temporary table A table defined by either the SQL statement CREATE GLOBAL TEMPORARY TABLE (a created temporary table) or DECLARE GLOBAL TEMPORARY TABLE (a declared temporary table) and used to hold data temporarily, such as the intermediate results of SQL transactions. Both created temporary tables and declared temporary tables persist only as long as the application process. The description of a created temporary table is stored in the DB2 catalog and the description is shareable across application processes while the description of a declared temporary table is neither stored nor shareable. Thus, each application process might refer to the same declared temporary table but have its own unique description of it. For a complete comparison of the two types of temporary tables, including how they differ from base tables, see Part 2 (Volume 1) of DB2 Administration Guide. | | | | | | | | | | | | | | | | | | | materialized query table A table created with the SQL statement CREATE TABLE and used to contain materialized data that is derived from one or more source tables specified by a fullselect. A source table is a base table, view, table expression, or user-defined table function. The fullselect specifies the queries that are used to refresh the materialized query table and to keep the data in the materialized query table synchronized with the data in the source tables from which the materialized query table was derived. The fullselect is used to determine the column definitions of a materialized query table and is executed against the source tables to populate the materialized query table. A materialized query table can be either user-maintained or system-maintained. A user-maintained materialized query table can be updated by the user with INSERT, UPDATE, DELETE, and REFRESH TABLE statements and with the LOAD utility. A system-maintained materialized query table can be updated only through the REFRESH TABLE statement. Materialized query tables can be used to improve the performance of dynamic SQL queries. If the database manager determines that a portion of

4

SQL Reference

DB2 concepts| | | | | a query could be resolved using a materialized query table, the query may be rewritten by the database manager to use the materialized query table. This decision is based in part on the settings of the CURRENT REFRESH AGE and the CURRENT MAINTAINED TABLE TYPES FOR OPTIMIZATION special registers. result table A set of rows that DB2 selects or generates from one or more tables or views. empty table A table with zero rows. sample table One of several tables sent with the DB2 licensed program that contains sample data. Many examples in this book are based on sample tables. See Appendix A of DB2 Application Programming and SQL Guide for a description of the sample tables.

IndexesAn index is an ordered set of pointers to rows of a base table or an auxiliary table. Each index is based on the values of data in one or more columns. An index is an object that is separate from the data in the table. When you define an index using the CREATE INDEX statement, DB2 builds this structure and maintains it automatically. Indexes can be used by DB2 to improve performance and ensure uniqueness. In most cases, access to data is faster with an index. A table with a unique index cannot have rows with identical keys. For more details on designing indexes and on their uses, see The Official Introduction to DB2 UDB for z/OS.

KeysA key is one or more columns that are identified as such in the description of a table, an index, or a referential constraint. Referential constraints are described in Referential constraints on page 7. The same column can be part of more than one key. A composite key is an ordered set of two or more columns of the same table. The ordering of the columns is not constrained by their ordering within the table. The term value, when used with respect to a composite key, denotes a composite value. Thus, a rule, such as the value of the foreign key must be equal to the value of the parent key, means that each component of the value of the foreign key must be equal to the corresponding component of the value of the parent key.

Unique keysA unique key is a key that is constrained so that no two of its values are equal. DB2 enforces the constraint during the execution of the LOAD utility and the SQL INSERT and UPDATE statements. The mechanism used to enforce the constraint is a unique index. Thus, every unique key is a key of a unique index. Such an index is also said to have the UNIQUE attribute. The columns of a unique key cannot contain null values. A unique key can be defined using the UNIQUE clause of the CREATE TABLE or ALTER TABLE statement. When a unique key is defined in a CREATE TABLEChapter 1. DB2 concepts

5

DB2 conceptsstatement, the table is marked unavailable until the unique index is created by the user. However, if the CREATE TABLE statement is processed by the schema processor, DB2 automatically creates the unique index. When a unique key is defined in an ALTER TABLE statement, a unique index must already exist on the columns of that unique key.

Primary keysA primary key is a unique key that is a part of the definition of a table. A table can have only one primary key, and the columns of a primary key cannot contain null values. Primary keys are optional and can be defined in CREATE TABLE or ALTER TABLE statements. The unique index on a primary key is called a primary index. When a primary key is defined in a CREATE TABLE statement, the table is marked unavailable until the primary index is created by the user unless the CREATE TABLE statement is proce