DB2 for OS/390 IBM Version 5
Call Level Interface Guide and Reference
SC26-8959-03
Note!
Before using this information and the product it supports, be sure to read the general information under “Notices” on page vii.
First Edition (June 1997)
This edition applies to Version 5 of IBM DATABASE 2 Server for OS/390 (DB2 for OS/390), 5655-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 manual since the hardcopy manual was published are indicated by the hash (#) symbol in the left-hand margin.
Copyright International Business Machines Corporation 1997.All rights reserved. US Government Users Restricted Rights – Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
Contents
Notices ...... vii Programming Interface Information ...... vii Trademarks ...... viii
Chapter 1. Introduction to this Book and the DB2 Library ...... 1 About This Book ...... 1 Who Should Use This Book ...... 1 How this Book is Structured ...... 1 How to Read the Syntax Diagrams ...... 2 How to Use the DB2 Library ...... 3 How to Obtain DB2 Information ...... 5 Summary of Changes to DB2 for OS/390 Version 5 ...... 7 Summary of Changes to This Book ...... 14
Chapter 2. Introduction to CLI ...... 15 DB2 CLI Background Information ...... 15 Differences Between DB2 CLI and Embedded SQL ...... 17 Advantages of Using DB2 CLI ...... 20 Deciding Which Interface To Use ...... 20 Other Information Sources ...... 21
Chapter 3. Writing a DB2 CLI Application ...... 23 Initialization and Termination ...... 24 Transaction Processing ...... 29 Diagnostics ...... 35 Data Types and Data Conversion ...... 37 Working With String Arguments ...... 43 Querying Environment and Data Source Information ...... 45
Chapter 4. Configuring CLI and Running Sample Applications ...... 49 Installing DB2 CLI ...... 49 DB2 CLI Runtime Environment ...... 50 Setting up DB2 CLI Runtime Environment ...... 51 Preparing a DB2 CLI Application ...... 54 DB2 CLI Initialization File ...... 60
Chapter 5. Functions ...... 71 DB2 CLI Function Summary ...... 72 SQLAllocConnect - Allocate Connection Handle ...... 76 SQLAllocEnv - Allocate Environment Handle ...... 80 SQLAllocStmt - Allocate a Statement Handle ...... 82 SQLBindCol - Bind a Column to an Application Variable ...... 84 SQLBindParameter - Binds A Parameter Marker to a Buffer ...... 89 SQLCancel - Cancel Statement ...... 100 SQLColAttributes - Get Column Attributes ...... 102 SQLColumnPrivileges - Get Privileges Associated With The Columns of A Table ...... 108 SQLColumns - Get Column Information for a Table ...... 113 SQLConnect - Connect to a Data Source ...... 118 SQLDataSources - Get List of Data Sources ...... 124
Copyright IBM Corp. 1997 iii
SQLDescribeCol - Describe Column Attributes ...... 127 SQLDescribeParam - Describe parameter marker ...... 132 SQLDisconnect - Disconnect from a Data Source ...... 134 SQLDriverConnect - (Expanded) Connect to a Data Source ...... 136 SQLError - Retrieve Error Information ...... 142 SQLExecDirect - Execute a Statement Directly ...... 148 SQLExecute - Execute a Statement ...... 153 SQLExtendedFetch - Extended Fetch (Fetch Array of Rows) ...... 156 SQLFetch - Fetch Next Row ...... 163 SQLForeignKeys - Get the List of Foreign Key Columns ...... 168 SQLFreeConnect - Free Connection Handle ...... 177 SQLFreeEnv - Free Environment Handle ...... 179 SQLFreeStmt - Free (or Reset) a Statement Handle ...... 181 SQLGetConnectOption - Returns Current Setting of A Connect Option ..... 184 SQLGetCursorName - Get Cursor Name ...... 186 SQLGetData - Get Data From a Column ...... 192 SQLGetEnvAttr - Returns Current Setting of An Environment Attribute ..... 205 SQLGetFunctions - Get Functions ...... 207 SQLGetInfo - Get General Information ...... 212 SQLGetSQLCA - Get SQLCA Data Structure ...... 228 SQLGetStmtOption - Returns Current Setting of A Statement Option ...... 235 SQLGetTypeInfo - Get Data Type Information ...... 237 SQLMoreResults - Determine If There Are More Result Sets ...... 245 SQLNativeSql - Get Native SQL Text ...... 249 SQLNumParams - Get Number of Parameters in A SQL Statement ...... 252 SQLNumResultCols - Get Number of Result Columns ...... 254 SQLParamData - Get Next Parameter For Which A Data Value Is Needed .. 256 SQLParamOptions - Specify an Input Array for a Parameter ...... 258 SQLPrepare - Prepare a Statement ...... 260 SQLPrimaryKeys - Get Primary Key Columns of A Table ...... 268 SQLProcedureColumns - Get Input/Output Parameter Information for A Procedure ...... 273 SQLProcedures - Get List of Procedure Names ...... 282 SQLPutData - Passing Data Value for A Parameter ...... 286 SQLRowCount - Get Row Count ...... 289 SQLSetColAttributes - Set Column Attributes ...... 291 SQLSetConnection - Set Connection Handle ...... 295 SQLSetConnectOption - Set Connection Option ...... 297 SQLSetCursorName - Set Cursor Name ...... 303 SQLSetEnvAttr - Set Environment Attribute ...... 306 SQLSetParam - Binds A Parameter Marker to a Buffer ...... 309 SQLSetStmtOption - Set Statement Option ...... 314 SQLSpecialColumns - Get Special (Row Identifier) Columns ...... 319 SQLStatistics - Get Index and Statistics Information For A Base Table ..... 325 SQLTablePrivileges - Get Privileges Associated With A Table ...... 330 SQLTables - Get Table Information ...... 334 SQLTransact - Transaction Management ...... 338
Chapter 6. Using Advanced Features ...... 341 Environment, Connection, and Statement Options ...... 341 Distributed Unit of Work (Coordinated Distributed Transactions) ...... 342 Querying System Catalog Information ...... 346 Sending/Retrieving Long Data in Pieces ...... 349 Using Arrays to Input Parameter Values ...... 350 iv Call Level Interface Guide and Reference
Retrieving A Result Set Into An Array ...... 353 Using Stored Procedures ...... 356 Writing Multithreaded Applications ...... 360 Mixing Embedded SQL and DB2 CLI ...... 366 Using Vendor Escape Clauses ...... 369
Chapter 7. Problem Diagnosis ...... 373 Tracing ...... 373 Debugging ...... 383
Appendix A.Programming Hints and Tips...... 385 Avoiding Common Initialization File Problems ...... 385 Setting Common Connection Options ...... 385 Setting Common Statement Options ...... 385 Using SQLSetColAttributes() to Reduce Network Flow ...... 386 Comparing Binding and SQLGetData ...... 387 Increasing Transfer Efficiency ...... 387 Limiting Use of Catalog Functions ...... 387 Using Column Names of Function Generated Result Sets ...... 387 Making use of Dynamic SQL Statement Caching ...... 388 Optimizing Insertion and Retrieval of Data ...... 388 Using SQLDriverConnect Instead of SQLConnect ...... 388 Turning Off Statement Scanning ...... 388 Problem Solving and Debugging ...... 389
Appendix B.DB2 CLI and ODBC...... 391 ODBC APIs and Data Types ...... 391 ODBC Function List ...... 393 Isolation Levels ...... 395
Appendix C.Extended Scalar Functions...... 397 String Functions ...... 397 Date and Time Functions ...... 398 System Functions ...... 399
Appendix D.Appendix D. SQLSTATE Cross Reference...... 401
Appendix E.Data Conversion ...... 411 Data Type Attributes ...... 412 Converting Data from SQL to C Data Types ...... 416 Converting Data from C to SQL Data Types ...... 425
Appendix F.Example Code ...... 435 DB2 CLI Application ...... 435 Stored Procedure ...... 464
Glossary ...... 485
Bibliography ...... 491
Index ...... 497
Contents v
vi Call Level Interface Guide and Reference
Notices
References in this publication to IBM products, programs, or services do not imply that IBM intends to make these available in all countries in which IBM operates. 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. Subject to IBM's valid intellectual property or other legally protectable rights, any functionally equivalent product, program, or service may be used instead of the IBM product, program, or service. The evaluation and verification of operation in conjunction with other products, except those expressly designated by IBM, are the responsibility of the user.
IBM may have patents or pending patent applications covering subject matter in this document. The furnishing of this document does not give 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.
Licensees of this program who wish to have information about it for the purpose of enabling (1) the exchange of information between independently created programs and other programs (including this one) and (2) the mutual use of the information that has been exchanged, should contact:
IBM Corporation IBM Corporation J74/G4 555 Bailey Avenue P.O. Box 49023 San Jose, CA 95161-9023
Such information may be available, subject to appropriate terms and conditions, including in some cases, payment of a fee.
Programming Interface Information This book is intended to help the customer write applications that use DB2 Call Level Interface to access IBM DB2 for OS/390 servers. This book documents General-use Programming Interface and Associated Guidance Information provided by DATABASE 2 for OS/390 (DB2 for OS/390).
General-use programming interfaces allow the customer to write programs that obtain the services of DB2 for OS/390.
Copyright IBM Corp. 1997 vii
Trademarks The following terms are trademarks of the IBM Corporation in the United States or other countries or both:
AIX IBM BookManager IMS C++/MVS IMS/ESA CICS Language Environment CICS/ESA MVS CICS/MVS MVS/ESA DATABASE 2 OS/2 DB2 OS/390 DB2/2 OS/400 DB2/6000 Parallel Sysplex DFSMS QMF DFSMShsm RACF Distributed Relational Database Architecture SQL/DS DRDA VTAM
Throughout the library, the DB2 licensed program and a particular DB2 subsystem are each referred to as “DB2.” In each case, the context makes the meaning clear. The term MVS is used to represent the MVS/Enterprise Systems Architecture (MVS/ESA). CICS is used to represent CICS/MVS and CICS/ESA; IMS is used to represent IMS/ESA; C and C language are used to represent the C/370 and C/C++ for MVS/ESA programming language.
Other company, product, and service names, which may be denoted by a double asterisk (**), may be trademarks or service marks of others.
UNIX is a registered trademark in the United States and other countries licensed exclusively through X/Open Company Limited.
Microsoft, Windows, and the Windows 95 logo are trademarks or registered trademarks of Microsoft Corporation.
viii Call Level Interface Guide and Reference
Chapter 1. Introduction to this Book and the DB2 Library
About This Book This book provides the information necessary to write applications using DB2 Call Level Interface to access IBM DATABASE 2 servers as well as any database that supports DRDA level 1 or DRDA level 2 protocols. This book should also be used as a supplement when writing portable ODBC applications that can be executed in a native DB2 for OS/390 environment using the DB2 Call Level Interface.
Who Should Use This Book DB2 application programmers with a knowledge of SQL and the C programming language.
ODBC application programmers with a knowledge of SQL and the C programming language.
How this Book is Structured This book is divided into the following chapters: “Chapter 1. Introduction to this Book and the DB2 Library,” identifies the purpose, the audience, and the use of this book. “Chapter 2. Introduction to CLI” on page 15, introduces DB2 CLI and discusses the background of the interface and its relation to embedded SQL and Microsoft ODBC. “Chapter 3. Writing a DB2 CLI Application” on page 23, provides an overview of a typical DB2 CLI application. This chapter discusses the basic tasks or steps within a simple DB2 CLI application. General concepts are introduced as well as the basic functions and the interaction between them. “Chapter 4. Configuring CLI and Running Sample Applications” on page 49, contains information for setting up the necessary environment to compile and run DB2 CLI applications. Sample applications are provided in order to verify your environment. “Chapter 5. Functions” on page 71, is a reference for the functions that make up DB2 CLI. “Chapter 6. Using Advanced Features” on page 341, provides an overview of more advanced tasks and the functions used to perform them. “Chapter 7. Problem Diagnosis” on page 373, explains how to work with traces and debug applications.
The appendixes contain the following information: Appendix A, “Programming Hints and Tips” on page 385, provides some common hints and tips for improving performance and/or portability of DB2 CLI applications.
Copyright IBM Corp. 1997 1