Developing Embedded SQL Applications
Total Page:16
File Type:pdf, Size:1020Kb
IBM DB2 10.1 for Linux, UNIX, and Windows Developing Embedded SQL Applications SC27-3874-00 IBM DB2 10.1 for Linux, UNIX, and Windows Developing Embedded SQL Applications SC27-3874-00 Note Before using this information and the product it supports, read the general information under Appendix B, “Notices,” on page 209. Edition Notice This document contains proprietary information of IBM. It is provided under a license agreement and is protected by copyright law. The information contained in this publication does not include any product warranties, and any statements provided in this manual should not be interpreted as such. You can order IBM publications online or through your local IBM representative. v To order publications online, go to the IBM Publications Center at http://www.ibm.com/shop/publications/ order v To find your local IBM representative, go to the IBM Directory of Worldwide Contacts at http://www.ibm.com/ planetwide/ To order DB2 publications from DB2 Marketing and Sales in the United States or Canada, call 1-800-IBM-4YOU (426-4968). When you send information to IBM, you grant IBM a nonexclusive right to use or distribute the information in any way it believes appropriate without incurring any obligation to you. © Copyright IBM Corporation 1993, 2012. US Government Users Restricted Rights – Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. Contents Chapter 1. Introduction to embedded Include files for COBOL embedded SQL SQL................1 applications .............29 Embedding SQL statements in a host language . 2 Include files for FORTRAN embedded SQL Embedded SQL statements in C and C++ applications .............32 applications..............2 Declaring the SQLCA for Error Handling ....34 Embedded SQL statements in FORTRAN Error Handling Using the WHENEVER Statement 35 applications..............4 Connecting to DB2 databases in embedded SQL Embedded SQL statements in COBOL applications 5 applications ..............36 Embedded SQL statements in REXX applications 6 Data types that map to SQL data types in Supported development software for embedded SQL embedded SQL applications .........37 applications...............8 Supported SQL data types in C and C++ Setting up the embedded SQL development embedded SQL applications ........37 environment ..............8 Supported SQL data types in COBOL embedded SQL applications ...........45 Chapter 2. Designing embedded SQL Supported SQL data types in FORTRAN embedded SQL applications ........48 applications .............9 Supported SQL data types in REXX embedded Authorization Considerations for Embedded SQL . 9 SQL applications ...........50 Static and dynamic SQL statement execution in Host Variables in embedded SQL applications . 52 embedded SQL applications .........10 Declaring host variables in embedded SQL Embedded SQL dynamic statements .....10 applications .............54 Determining when to execute SQL statements Declaring Host Variables with the db2dclgn statically or dynamically in embedded SQL Declaration Generator ..........55 applications .............11 Column data types and host variables in Performance of embedded SQL applications . 13 embedded SQL applications ........55 32-bit and 64-bit support for embedded SQL Declaring XML host variables in embedded SQL applications ..............14 applications .............56 Restrictions on embedded SQL applications . 15 Identifying XML values in an SQLDA ....58 Restrictions on character sets using C and C++ to Identifying null SQL values with null indicator program embedded SQL applications.....15 variables ..............58 Restrictions on using COBOL to program Including SQLSTATE and SQLCODE host embedded SQL applications ........15 variables in embedded SQL applications....60 Restrictions on using FORTRAN to program Referencing host variables in embedded SQL embedded SQL applications ........16 applications .............60 Restrictions on using REXX to program Example: Referencing XML host variables in embedded SQL applications ........16 embedded SQL applications ........61 Recommendations for developing embedded SQL Host variables in C and C++ embedded SQL applications with XML and XQuery .....17 applications .............62 Concurrent transactions and multi-threaded Host variables in COBOL.........88 database access in embedded SQL applications . 17 Host variables in FORTRAN........99 Recommendations for using multiple threads . 19 Host variables in REXX .........105 Code page and country or region code Executing XQuery expressions in embedded SQL considerations for multi-threaded UNIX applications ..............111 applications .............20 Executing SQL statements in embedded SQL Troubleshooting multi-threaded embedded SQL applications ..............112 applications .............20 Comments in embedded SQL applications. 113 Executing static SQL statements in embedded Chapter 3. Programming embedded SQL applications ...........113 SQL applications ..........23 Retrieving host variable information from the Embedded SQL source files .........23 SQLDA structure in embedded SQL applications 114 Embedded SQL application template in C ....24 Providing variable input to dynamically Include files and definitions required for embedded executed SQL statements by using parameter SQL applications ............27 markers ..............125 Include files for C and C++ embedded SQL Calling procedures in embedded SQL applications .............27 applications .............127 © Copyright IBM Corp. 1993, 2012 iii Reading and scrolling through result sets in Error-checking utilities .........160 embedded SQL applications .......128 Building applications and routines written in C Error message retrieval in embedded SQL andC++..............162 applications .............133 Building applications and routines written in Disconnecting from embedded SQL applications 136 COBOL ..............175 Building and running embedded SQL Chapter 4. Building embedded SQL applications written in REXX .......189 applications ............139 Building embedded SQL applications from the command line .............191 Precompilation of embedded SQL applications with Building embedded SQL applications written in the PRECOMPILE command ........140 C or C++ (Windows) ..........191 Precompilation of embedded SQL applications that access more than one database server . 142 Embedded SQL application packages and access Chapter 5. Deploying and running plans ...............142 embedded SQL applications .....193 Package schema qualification using CURRENT Restrictions on linking to libdb2.so ......193 PACKAGE PATH special register ......143 Precompiler generated timestamps .....146 Chapter 6. Enabling compatibility Errors and warnings from precompilation of features for migration ........195 embedded SQL applications .......147 Compiling and linking source files containing embedded SQL.............147 Appendix A. Overview of the DB2 Binding embedded SQL packages to a database 148 technical information ........199 Effect of DYNAMICRULES bind option on DB2 technical library in hardcopy or PDF format 199 dynamic SQL ............148 Displaying SQL state help from the command line Using special registers to control the statement processor ...............202 compilation environment ........150 Accessing different versions of the DB2 Package recreation using the BIND command Information Center ...........202 and an existing bind file.........151 Updating the DB2 Information Center installed on Rebinding existing packages with the REBIND your computer or intranet server .......202 command .............151 Manually updating the DB2 Information Center Bind considerations ..........152 installed on your computer or intranet server . 204 Blocking considerations .........153 DB2 tutorials .............205 Advantages of deferred binding ......153 DB2 troubleshooting information .......206 Performance improvements when using REOPT Terms and conditions...........206 option of the BIND command .......153 Binding applications and utilities (DB2 Connect Appendix B. Notices ........209 server)................154 Package storage and maintenance ......157 Index ...............213 Package versioning ..........157 Resolution of unqualified table names ....158 Building embedded SQL applications using the sample build script ...........158 iv Developing Embedded SQL Applications Chapter 1. Introduction to embedded SQL Embedded SQL database applications connect to databases and execute embedded SQL statements. Embedded SQL statements are embedded within a host language application. Embedded SQL database applications support the embedding of SQL statements to be executed statically or dynamically. You can develop embedded SQL applications for DB2® in the following host programming languages: C, C++, COBOL, FORTRAN, and REXX. Note: Support for embedded SQL in FORTRAN and REXX has been deprecated and will remain at the DB2 Universal Database™ , Version 5.2 level. Building embedded SQL applications involves two prerequisite steps before application compilation and linking. v Preparing the source files containing embedded SQL statements using the DB2 precompiler. The PREP (PRECOMPILE) command is used to invoke the DB2 precompiler, which reads your source code, parses and converts the embedded SQL statements to DB2 runtime services API calls, and finally writes the output to a new modified source file. The precompiler produces access plans for the SQL statements, which are stored together as a package within the database. v Binding the statements