Programmer's Guide to the Oracle Precompilers
Total Page:16
File Type:pdf, Size:1020Kb
Oracle® Database Programmer's Guide to the Oracle Precompilers 12c Release 2 (12.2) E85800-01 January 2017 Oracle Database Programmer's Guide to the Oracle Precompilers, 12c Release 2 (12.2) E85800-01 Copyright © 2008, 2017, Oracle and/or its affiliates. All rights reserved. Primary Author: Siddharth Naidu Contributors: Denis Raphaely, Simon Watt, Radhakrishnan Hari, Nancy Ikeda, Ken Jacobs, Valarie Moore, Tim Smith, Scott Urman, Arun Desai, Mallikharjun Vemana, Subhranshu Banerjee This software and related documentation are provided under a license agreement containing restrictions on use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is prohibited. The information contained herein is subject to change without notice and is not warranted to be error-free. If you find any errors, please report them to us in writing. If this is software or related documentation that is delivered to the U.S. Government or anyone licensing it on behalf of the U.S. Government, then the following notice is applicable: U.S. GOVERNMENT END USERS: Oracle programs, including any operating system, integrated software, any programs installed on the hardware, and/or documentation, delivered to U.S. Government end users are "commercial computer software" pursuant to the applicable Federal Acquisition Regulation and agency- specific supplemental regulations. As such, use, duplication, disclosure, modification, and adaptation of the programs, including any operating system, integrated software, any programs installed on the hardware, and/or documentation, shall be subject to license terms and license restrictions applicable to the programs. No other rights are granted to the U.S. Government. This software or hardware is developed for general use in a variety of information management applications. It is not developed or intended for use in any inherently dangerous applications, including applications that may create a risk of personal injury. If you use this software or hardware in dangerous applications, then you shall be responsible to take all appropriate fail-safe, backup, redundancy, and other measures to ensure its safe use. Oracle Corporation and its affiliates disclaim any liability for any damages caused by use of this software or hardware in dangerous applications. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc. AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced Micro Devices. UNIX is a registered trademark of The Open Group. This software or hardware and documentation may provide access to or information about content, products, and services from third parties. Oracle Corporation and its affiliates are not responsible for and expressly disclaim all warranties of any kind with respect to third-party content, products, and services unless otherwise set forth in an applicable agreement between you and Oracle. Oracle Corporation and its affiliates will not be responsible for any loss, costs, or damages incurred due to your access to or use of third-party content, products, or services, except as set forth in an applicable agreement between you and Oracle. Contents Preface Intended Audience xxiii Structure xxiii Documentation Accessibility xxv Related Documents xxvi Conventions xxvi 1 Getting Acquainted 1.1 What Is an Oracle Precompiler? 1-1 1.1.1 Language Alternatives 1-2 1.2 Why Use an Oracle Precompiler? 1-3 1.3 Why Use SQL? 1-3 1.4 Why Use PL/SQL? 1-4 1.5 What Do the Oracle Precompilers Offer? 1-4 1.6 Do the Oracle Precompilers Meet Industry Standards? 1-5 1.6.1 Requirements 1-6 1.6.2 Compliance 1-6 1.6.3 FIPS Flagger 1-6 1.6.4 FIPS Option 1-7 1.6.5 Certification 1-7 2 Learning the Basics 2.1 Key Concepts of Embedded SQL Programming 2-1 2.1.1 Embedded SQL Statements 2-1 2.1.2 Executable versus Declarative Statements 2-2 2.1.3 Embedded SQL Syntax 2-3 2.1.4 Static versus Dynamic SQL Statements 2-4 2.1.5 Embedded PL/SQL Blocks 2-4 2.1.6 Host and Indicator Variables 2-4 2.1.7 Oracle Datatypes 2-5 2.1.8 Arrays 2-5 iii 2.1.9 Datatype Equivalencing 2-5 2.1.10 Private SQL Areas, Cursors, and Active Sets 2-5 2.1.11 Transactions 2-6 2.1.12 Errors and Warnings 2-6 2.2 Steps in Developing an Embedded SQL Application 2-7 2.3 A Sample Program 2-9 2.4 Sample Tables 2-10 2.4.1 Sample Data 2-10 3 Meeting Program Requirements 3.1 The Declare Section 3-1 3.1.1 An Example 3-2 3.2 INCLUDE Statements 3-2 3.3 The SQLCA 3-3 3.4 Oracle Datatypes 3-3 3.4.1 Internal Datatypes 3-4 3.4.2 CHAR 3-4 3.4.3 DATE 3-5 3.4.4 LONG 3-5 3.4.5 LONG RAW 3-5 3.4.6 MLSLABEL 3-5 3.4.7 NUMBER 3-6 3.4.8 RAW 3-6 3.4.9 ROWID 3-6 3.4.10 VARCHAR2 3-6 3.4.11 SQL Pseudocolumns and Functions 3-7 3.4.12 ROWLABEL Column 3-8 3.4.13 External Datatypes 3-8 3.4.14 CHAR 3-9 3.4.15 CHARF 3-10 3.4.16 CHARZ 3-10 3.4.17 DATE 3-10 3.4.18 DECIMAL 3-11 3.4.19 DISPLAY 3-11 3.4.20 FLOAT 3-11 3.4.21 INTEGER 3-11 3.4.22 LONG 3-11 3.4.23 LONG RAW 3-11 3.4.24 LONG VARCHAR 3-12 3.4.25 LONG VARRAW 3-12 iv 3.4.26 MLSLABEL 3-12 3.4.27 NUMBER 3-12 3.4.28 RAW 3-13 3.4.29 ROWID 3-13 3.4.30 STRING 3-13 3.4.31 UNSIGNED 3-14 3.4.32 VARCHAR 3-14 3.4.33 VARCHAR2 3-14 3.4.34 VARNUM 3-15 3.4.35 VARRAW 3-15 3.5 Datatype Conversion 3-15 3.5.1 DATE Values 3-17 3.5.2 RAW and LONG RAW Values 3-17 3.6 Declaring and Referencing Host Variables 3-17 3.6.1 Some Examples 3-18 3.6.2 VARCHAR Variables 3-18 3.6.3 Host Variable Guidelines 3-19 3.7 Declaring and Referencing Indicator Variables 3-19 3.7.1 INDICATOR Keyword 3-19 3.7.2 An Example 3-19 3.7.3 Indicator Variable Guidelines 3-20 3.8 Datatype Equivalencing 3-20 3.8.1 Why Equivalence Datatypes? 3-20 3.8.2 Host Variable Equivalencing 3-21 3.8.3 An Example 3-22 3.8.4 About Using the CHARF Datatype Specifier 3-23 3.8.5 Guidelines 3-23 3.9 Globalization Support 3-24 3.10 Multibyte Globalization Support Character Sets 3-26 3.10.1 Character Strings in Embedded SQL 3-26 3.10.2 Dynamic SQL 3-26 3.10.3 Embedded DDL 3-26 3.10.4 Multibyteultibyte Globalization Support Host Variables 3-26 3.10.5 Restrictions 3-27 3.10.6 Blank Padding 3-27 3.10.7 Indicator Variables 3-27 3.11 Concurrent Logons 3-27 3.11.1 Some Preliminaries 3-28 3.11.2 Default Databases and Connections 3-28 3.11.3 Explicit Logons 3-29 3.11.4 Single Explicit Logons 3-29 v 3.11.5 Multiple Explicit Logons 3-32 3.11.6 Implicit Logons 3-33 3.11.7 Single Implicit Logons 3-33 3.11.8 Multiple Implicit Logons 3-34 3.12 Embedding OCI (Oracle Call Interface) Calls 3-34 3.12.1 About Setting Up the LDA 3-35 3.12.2 Remote and Multiple Connections 3-35 3.13 About Developing X/Open Applications 3-35 3.13.1 Oracle-Specific Issues 3-36 3.13.2 About Connecting to Oracle 3-37 3.13.3 Transaction Control 3-37 3.13.4 OCI Calls 3-37 3.13.5 Linking 3-37 4 Using Embedded SQL 4.1 About Using Host Variables 4-1 4.1.1 Output versus Input Host Variables 4-1 4.2 About Using Indicator Variables 4-2 4.2.1 Input Variables 4-2 4.2.2 Output Variables 4-2 4.2.3 Inserting Nulls 4-3 4.2.4 Handling Returned Nulls 4-3 4.2.5 Fetching Nulls 4-3 4.2.6 Testing for Nulls 4-4 4.2.7 Fetching Truncated Values 4-4 4.3 The Basic SQL Statements 4-4 4.3.1 Selecting Rows 4-5 4.3.2 Available Clauses 4-6 4.3.3 Inserting Rows 4-6 4.3.4 Using Subqueries 4-6 4.3.5 Updating Rows 4-6 4.3.6 Deleting Rows 4-7 4.3.7 Using the WHERE Clause 4-7 4.4 Cursors 4-7 4.4.1 Declaring a Cursor 4-8 4.4.2 Opening a Cursor 4-8 4.4.3 Fetching from a Cursor 4-9 4.4.4 Closing a Cursor 4-10 4.4.5 Using the CURRENT OF Clause 4-10 4.4.6 Restrictions 4-10 vi 4.4.7 A Typical Sequence of Statements 4-11 4.4.8 A Complete Example 4-11 4.5 Cursor Variables 4-12 4.5.1 About Declaring a Cursor Variable 4-13 4.5.2 Allocating a Cursor Variable 4-13 4.5.3 Opening a Cursor Variable 4-13 4.5.4 Fetching from a Cursor Variable 4-14 4.5.5 Closing a Cursor Variable 4-15 5 Using Embedded PL/SQL 5.1 Advantages of PL/SQL 5-1 5.1.1 Better Performance 5-1 5.1.2 Integration with Oracle 5-1 5.1.3 Cursor FOR Loops 5-2 5.1.4 Subprograms 5-2 5.1.5 Parameter Modes 5-3 5.1.6 Packages 5-3 5.1.7 PL/SQL Tables 5-3 5.1.8 User-defined Records 5-4 5.2 About Embedding PL/SQL Blocks 5-5 5.3 About Using Host Variables 5-5 5.3.1 An Example 5-5 5.3.2 A More Complex Example 5-6 5.3.3 VARCHAR Pseudotype 5-8 5.4 About Using Indicator Variables 5-8 5.4.1 Handling Nulls 5-9 5.4.2 Handling Truncated Values 5-9 5.5 About Using Host Arrays 5-10 5.5.1 ARRAYLEN Statement 5-11 5.6 About Using Cursors 5-12 5.6.1 An Alternative 5-13 5.7 Stored Subprograms 5-13 5.7.1 Creating Stored Subprograms 5-14 5.7.2 Calling a Stored Subprogram 5-15 5.7.3 Remote Access 5-17 5.7.4 Getting Information about Stored Subprograms 5-17 5.8 About Using Dynamic PL/SQL 5-18 5.8.1 Restriction 5-18 vii 6 Running the Oracle Precompilers 6.1 The Precompiler Command 6-1 6.2 What Occurs during Precompilation? 6-2 6.3 Precompiler Options 6-2 6.3.1 Default Values 6-3 6.3.2 Determining Current Values 6-4 6.3.3 Case Sensitivity 6-4 6.3.4 Configuration Files 6-4 6.4 Entering Options 6-5 6.4.1