Pro*C/C++ Precompiler Programmer's Guide
Release 8.0

A58233-01

Library

Product

Contents

Index

Prev Next

F
Embedded SQL Commands and Directives

This appendix contains descriptions of both the SQL92 embedded commands and directives, as well as the Oracle embedded SQL extensions.

Note: Only statements which differ in syntax from non-embedded SQL are described in this appendix. For details of the non-embedded SQL statements, see the Oracle8 SQL Reference.

This appendix has the following sections:

Summary of Precompiler Directives and Embedded SQL Commands

Embedded SQL commands place DDL, DML, and Transaction Control statements within a Pro*C/C++ program. Table 0-3 provides a functional summary of the embedded SQL commands and directives.

The Source/Type column in Table 0-3 is displayed in the format:

Source

 

is either SQL92 standard SQL (S) or an Oracle extension (O).

 

Type

 

is either an executable (E) statement or a directive (D).

 

Table 0-3 Precompiler Directives and Embedded SQL Commands and Clauses
EXEC SQL Statement   Source/Type   Purpose  

ALLOCATE

 

O/E

 

To allocate memory for a cursor variable or an Object type.

 

CACHE FREE ALL

 

O/E

 

Frees all allocated object cache memory.

 

CLOSE

 

S/E

 

To disable a cursor, releasing the resources it holds.

 

COMMIT

 

S/E

 

To end the current transaction, making all database change permanent (optionally frees resources and disconnects from the database)

 

CONNECT

 

O/E

 

To log on to an Oracle8 instance.

 

CONTEXT ALLOCATE

 

O/E

 

To allocate memory for a SQLLIB runtime context.

 

CONTEXT FREE

 

O/E

 

To free memory for a SQLLIB runtime context.

 

CONTEXT OBJECT OPTION GET

 

O/E

 

To determine how options are set.

 

CONTEXT OBJECT OPTION SET

 

O/E

 

To set options.

 

CONTEXT USE

 

O/D

 

To specify which SQLLIB runtime context to use for subsequent executable SQL statements when multiple threads are used.

 

DECLARE CURSOR

 

S/D

 

To declare a cursor, associating it with a query.

 

DECLARE DATABASE

 

O/D

 

To declare an identifier for a non-default database to be accessed in subsequent embedded SQL statements.

 

DECLARE STATEMENT

 

S/D

 

To assign a SQL variable name to a SQL statement.

 

DECLARE TABLE

 

O/D

 

To declare the table structure for semantic checking of embedded SQL statements by Pro*C/C++.

 

DECLARE TYPE

 

O/D

 

To declare the type structure for semantic checking of embedded SQL statements by Pro*C/C++.

 

DELETE

 

S/E

 

To remove rows from a table or from a view's base table.

 

DESCRIBE

 

S/E

 

To initialize a descriptor, a structure holding host variable descriptions.

 

ENABLE THREADS

 

O/E

 

To initialize a process that supports multiple threads.

 

EXECUTE...END-EXEC

 

O/E

 

To execute an anonymous PL/SQL block.

 

EXECUTE

 

S/E

 

To execute a prepared dynamic SQL statement.

 

EXECUTE IMMEDIATE

 

S/E

 

To prepare and execute a SQL statement with no host variables.

 

FETCH

 

S/E

 

To retrieve rows selected by a query.

 

FREE

 

O/E

 

To free memory allocated in the object cache

 

INSERT

 

S/E

 

To add rows to a table or to a view's base table.

 

OBJECT CREATE

 

O/E

 

To create a referenceable object in the cache.

 

OBJECT DELETE

 

O/E

 

To mark an object as deleted.

 

OBJECT DEREF

 

O/E

 

To dereference an object.

 

OBJECT FLUSH

 

O/E

 

To transmit persistent objects to server.

 

OBJECT GET

 

O/E

 

To convert an object attribute to a C type.

 

OBJECT RELEASE

 

O/E

 

To "unpin" an object in the cache.

 

OBJECT SET

 

O/E

 

To update object attributes in the cache.

 

OBJECT UPDATE

 

O/E

 

To mark an object in the cache as updated.

 

OPEN

 

S/E

 

To execute the query associated with a cursor.

 

PREPARE

 

S/E

 

To parse a dynamic SQL statement.

 

ROLLBACK

 

S/E

 

To end the current transaction, discard all changes in the current transaction, and release all locks (optionally release resources and disconnect from the database).

 

SAVEPOINT

 

S/E

 

To identify a point in a transaction to which you can later roll back.

 

SELECT

 

S/E

 

To retrieve data from one or more tables, views, or snapshots, assigning the selected values to host variables.

 

TYPE

 

O/D

 

To assign an Oracle8 external datatype to a whole class of host variables by equivalencing the external datatype to a user-defined datatype.

 

UPDATE

 

S/E

 

To change existing values in a table or in a view's base table.

 

VAR

 

O/D

 

To override the default datatype and assign a specific Oracle8 external datatype to a host variable.

 

WHENEVER

 

S/D

 

To specify handling for error and warning conditions.

 

About The Command Descriptions

The directives, commands, and clauses appear alphabetically. The description of each contains the following sections:

Purpose

 

describes the basic uses of the command.

 

Prerequisites

 

lists privileges you must have and steps that you must take before using the command. Unless otherwise noted, most commands also require that the database be open by your instance.

 

Syntax

 

shows the keywords and parameters of the command.

 

Keywords and Parameters

 

describes the purpose of each keyword and parameter.

 

Usage Notes

 

discusses how and when to use the command.

 

Examples

 

shows example statements of the command.

 

Related Topics

 

lists related commands, clauses, and sections of this manual.

 

How to Read Syntax Diagrams

Syntax diagrams are used to illustrate embedded SQL syntax. They are drawings that depict valid syntax paths.

Trace each diagram from left to right, in the direction shown by the arrows.

Commands and other keywords appear in UPPER CASE inside rectangles. Type them exactly as shown in the rectangles. Parameters appear in lower case inside ovals. Substitute variables for the parameters in statements you write. Operators, delimiters, and terminators appear in circles. Following the conventions defined in the Preface, a semicolon terminates statements.

If the syntax diagram has more than one path, you can choose any path to travel.

If you have the choice of more than one keyword, operator, or parameter, your options appear in a vertical list. In the following example, you can travel down the vertical line as far as you like, then continue along any horizontal line:

According to the diagram, all of the following statements are valid:

EXEC SQL WHENEVER NOT FOUND ... 
EXEC SQL WHENEVER SQLERROR ... 
EXEC SQL WHENEVER SQLWARNING ... 

Required Keywords and Parameters

Required keywords and parameters can appear singly or in a vertical list of alternatives. Single required keywords and parameters appear on the main path, that is, on the horizontal line you are currently traveling. In the following example, cursor is a required parameter:

If there is a cursor named emp_cursor, then, according to the diagram, the following statement is valid:

EXEC SQL CLOSE emp_cursor; 

If any of the keywords or parameters in a vertical list appears on the main path, one of them is required. That is, you must choose one of the keywords or parameters, but not necessarily the one that appears on the main path. In the following example, you must choose one of the four actions:

Optional Keywords and Parameters

If keywords and parameters appear in a vertical list above the main path, they are optional. In the following example, instead of traveling down a vertical line, you can continue along the main path:

If there is a database named oracle2, then, according to the diagram, all of the following statements are valid:

EXEC SQL ROLLBACK; 
EXEC SQL ROLLBACK WORK; 
EXEC SQL AT oracle2 ROLLBACK; 

Syntax Loops

Loops let you repeat the syntax within them as many times as you like. In the following example, column_name is inside a loop. So, after choosing one column name, you can go back repeatedly to choose another, separating the column names by a comma.

If DEBIT, CREDIT, and BALANCE are column names, then, according to the diagram, all of the following statements are valid:

EXEC SQL SELECT DEBIT INTO ... 
EXEC SQL SELECT CREDIT, BALANCE INTO ... 
EXEC SQL SELECT DEBIT, CREDIT, BALANCE INTO ... 

Multi-part Diagrams

Read a multi-part diagram as if all the main paths were joined end-to-end. The following example is a two-part diagram:

According to the diagram, the following statement is valid:

EXEC SQL PREPARE statement_name FROM string_literal; 

Database Objects

The names of Oracle objects, such as tables and columns, must not exceed 30 characters in length. The first character must be a letter, but the rest can be any combination of letters, numerals, dollar signs ($), pound signs (#), and underscores (_).

However, if an Oracle identifier is enclosed by quotation marks ("), it can contain any combination of legal characters, including spaces but excluding quotation marks.

Oracle identifiers are not case-sensitive except when enclosed by quotation marks.

Statement Terminator

In all embedded SQL diagrams, each statement is understood to end with the statement terminator ";".

ALLOCATE (Executable Embedded SQL Extension)

Purpose

To allocate a cursor variable to be referenced in a PL/SQL bloc, or to allocate space in the object cache.

Prerequisites

A cursor variable (see Chapter 3, "Developing a Pro*C/C++ Application") of type SQL_CURSOR must be declared before allocating memory for the cursor variable.

Pointers to a host struct and, optionally, an indicator struct must be declared before allocating memory in the object cache.

An active connection to a database is required.

Syntax

Keywords and Parameters

dbname

 

a null-terminated string containing the database connection name, as established previously in a CONNECT statement. If omitted, or if an empty string, the default database connection is assumed.

 

cursor_variable

 

is the cursor variable to be allocated.

 

host_ptr

 

a pointer to a host struct generated by OTT for object types

 

Usage Notes

Whereas a cursor is static, a cursor variable is dynamic because it is not tied to a specific query. You can open a cursor variable for any type-compatible query.

For more information on this command, see PL/SQL User's Guide and Reference and Oracle8 SQL Reference.

Example

This partial example illustrates the use of the ALLOCATE command in a Pro*C/C++ embedded SQL program:

EXEC SQL BEGIN DECLARE SECTION;
   SQL_CURSOR emp_cv;
   struct{ ... } emp_rec;
EXEC SQL END DECLARE SECTION;
EXEC SQL ALLOCATE :emp_cv;
EXEC SQL EXECUTE
   BEGIN
      OPEN :emp_cv FOR SELECT * FROM emp;
   END;
END-EXEC;
for (;;)
   {
   EXEC SQL FETCH :emp_cv INTO :emp_rec;
   ...
   }

Related Topics

CACHE FREE ALL command on F-10.

CLOSE command on F-11

EXECUTE command on F-38

FETCH command on F-41

FREE command on F-44.

CACHE FREE ALL (Executable Embedded SQL Extension)

Purpose

To free all memory in the object cache.

Prerequisites

An active database connection must exist.

Syntax

Keywords and Parameters

db_name

 

a null-terminated string containing the database connection name, as established previously in a CONNECT statement. If omitted, or if an empty string, the default database connection is assumed.

 

Usage Notes

When the connection count drops to zero, SQLLIB automatically frees all object cache memory.For more information, see "CACHE FREE ALL" on page 8-7.

Example

EXEC SQL AT mydb CACHE FREE ALL ;

Related Topics

ALLOCATE command on F-8.

FREE command on F-44.

CLOSE (Executable Embedded SQL)

Purpose

To disable a cursor, freeing the resources acquired by opening the cursor, and releasing parse locks.

Prerequisites

The cursor or cursor variable must be open if MODE=ANSI.

Syntax

Keywords and Parameters

cursor

 

is a cursor to be closed.

 

cursor_variable

 

is a cursor variable to be closed.

 

Usage Notes

Rows cannot be fetched from a closed cursor. A cursor need not be closed to be reopened. The HOLD_CURSOR and RELEASE_CURSOR precompiler options alter the effect of the CLOSE command. For information on these options, see Chapter 9, "Running the Pro*C/C++ Precompiler".

Example

This example illustrates the use of the CLOSE command:

EXEC SQL CLOSE emp_cursor;

Related Topics

PREPARE command on F-60

DECLARE CURSOR command on F-22

OPEN command on F-58

COMMIT (Executable Embedded SQL)

Purpose

To end your current transaction, making permanent all its changes to the database and optionally freeing all resources and disconnecting from the Oracle8 Server.

Prerequisites

To commit your current transaction, no privileges are necessary.

To manually commit a distributed in-doubt transaction that you originally committed, you must have FORCE TRANSACTION system privilege. To manually commit a distributed in-doubt transaction that was originally committed by another user, you must have FORCE ANY TRANSACTION system privilege.

If you are using Trusted Oracle in DBMS MAC mode, you can only commit an in-doubt transaction if your DBMS label matches the transaction's label and the creation label of the user who originally committed the transaction; or, if you satisfy one of the following criteria:

Syntax

Keyword and Parameters

AT

 

identifies the database to which the COMMIT statement is issued. The database can be identified by either:

 

 

db_name

 

is a database identifier declared in a previous DECLARE DATABASE statement.

 

 

:host_variable

 

is a host variable whose value is a db_name.

 

 

If you omit this clause, Oracle8 issues the statement to your default database.

 

WORK

 

is supported only for compliance with standard SQL. The statements COMMIT and COMMIT WORK are equivalent.

 

COMMENT

 

specifies a Comment to be associated with the current transaction. The 'text' is a quoted literal of up to 50 characters that Oracle8 stores in the data dictionary view DBA_2PC_PENDING along with the transaction ID if the transaction becomes in-doubt.

 

RELEASE

 

frees all resources and disconnects the application from the Oracle8 Server.

 

FORCE

 

manually commits an in-doubt distributed transaction. The transaction is identified by the 'text' containing its local or global transaction ID. To find the IDs of such transactions, query the data dictionary view DBA_2PC_PENDING. You can also use the optional integer to explicitly assign the transaction a system change number (SCN). If you omit the integer, the transaction is committed using the current SCN.

 

Usage Notes

Always explicitly commit or rollback the last transaction in your program by using the COMMIT or ROLLBACK command and the RELEASE option. Oracle8 automatically rolls back changes if the program terminates abnormally.

The COMMIT command has no effect on host variables or on the flow of control in the program. For more information on this command, see Chapter 10, "Defining and Controlling Transactions".

Example

This example illustrates the use of the embedded SQL COMMIT command:

EXEC SQL AT sales_db COMMIT RELEASE;

Related Topics

ROLLBACK command on F-61

SAVEPOINT command on F-64

CONNECT (Executable Embedded SQL Extension)

Purpose

To log on to an Oracle8 database.

Prerequisites

You must have CREATE SESSION system privilege in the specified database.

If you are using Trusted Oracle in DBMS MAC mode, your operating system label must dominate both your creation label and the label at which you were granted CREATE SESSION system privilege. Your operating system label must also fall between the operating system equivalents of DBHIGH and DBLOW, inclusive.

If you are using Trusted Oracle in OS MAC mode, your operating system label must match the label of the database to which you are connecting.

Syntax

Keyword and Parameters

:user

:password

 

specifies your username and password separately.

 

:user_password

 

is a single host variable containing the Oracle8 username and password separated by a slash (/).

 

 

To allow Oracle8 to verify your connection through your operating system, specify "/" as the :user_password value.

 

AT

 

identifies the database to which the connection is made. The database can be identified by either:

 

 

db_name

 

is a database identifier declared in a previous DECLARE DATABASE statement.

 

 

:host_variable

 

is a host variable whose value is a previously declared db_name.

 

USING

 

specifies the Net8 database specification string used to connect to a non-default database. If you omit this clause, you are connected to your default database.

 

ALTER AUTHORIZATION

 

Change password to the following string.

 

:new_password

 

New password string.

 

Usage Notes

A program can have multiple connections, but can only connect once

to your default database. For more information on this command, see "Connecting to Oracle8" on page 4-21.

Example

The following example illustrate the use of CONNECT:

EXEC SQL CONNECT :username 
    IDENTIFIED BY :password 

You can also use this statement in which the value of :userid is the value of :username and :password separated by a "/" such as 'SCOTT/TIGER':

EXEC SQL CONNECT :userid 

Related Topics

COMMIT command on F-12

DECLARE DATABASE command on F-24

ROLLBACK command on F-61

CONTEXT ALLOCATE (Executable Embedded SQL Extension)

Purpose

To initialize a SQLLIB runtime context that is referenced in an EXEC SQL CONTEXT USE statement.

Prerequisites

The runtime context must be declared of type sql_context.

Syntax

Keywords and Parameters

:context

 

is the SQLLIB runtime context for which memory is to be allocated.

 

Usage Notes

In a multi-threaded application, execute this function once for each runtime context.

For more information on this command, see "Developing Multi-threaded Applications" on page 4-37.

Example

This example illustrates the use of a CONTEXT ALLOCATE command in a Pro*C/C++ embedded SQL program:

EXEC SQL CONTEXT ALLOCATE :ctx1;

Related Topics

CONTEXT FREE on F-17

CONTEXT USE on F-18

ENABLE THREADS on F-36

CONTEXT FREE (Executable Embedded SQL Extension)

Purpose

To free all memory associated with a runtime context and place a null pointer in the host program variable.

Prerequisites

The CONTEXT ALLOCATE command must be used to allocate memory for the specified runtime context before the CONTEXT FREE command can free the memory allocated for it.

Syntax

Keywords and Parameters

context

 

is the allocated runtime context for which the memory is to be deallocated.

 

Usage Notes

For more information on this command, see "Developing Multi-threaded Applications" on page 4-37.

Example

This example illustrates the use of a CONTEXT FREE command in a Pro*C/C++ embedded SQL program:

EXEC SQL CONTEXT FREE :ctx1;

Related Topics

CONTEXT ALLOCATE on F-16

CONTEXT USE on F-18

ENABLE THREADS on F-36

CONTEXT OBJECT OPTION GET (Executable Embedded SQL Extension)

Purpose

To determine the values of options set by CONTEXT OBJECT OPTION SET for the context in use.

Prerequisites

Precompiler option OBJECTS must be set to YES.

Syntax

Keywords and Parameters

option   DATEFORMAT (format for Date conversion) or DATELANG (language for conversion)  
expr   output of type STRING, VARCHAR, or CHARZ, in the same order as the option list.  

Usage Notes

See "CONTEXT OBJECT OPTION SET" on page 8-18.

Example

char EuroFormat[50];
...
EXEC SQL CONTEXT OPTION GET DATEFORMAT INTO :EuroFormat ;
printf("Date format is ", %s\n");

Related Topics

CONTEXT ALLOCATE on page F-8.

CONTEXT FREE on page F-17

CONTEXT USE on page F-18

CONTEXT OBJECT OPTION SET on page F-19.

CONTEXT OBJECT OPTION SET (Executable Embedded SQL Extension)T

Purpose

To set options to specified values of Date attributes: DATEFORMAT, DATELANG for the context in use..

Prerequisites

Precompiler option OBJECTS must be set to YES.

Syntax

Keywords and Parameters

option   DATEFORMAT (format for Date conversion) or DATELANG (language for Date conversion)  
expr   input of type STRING, VARCHAR, or CHARZ. In the same order as the option list.  

Usage Notes

See "CONTEXT OBJECT OPTION GET" on page 8-19.

Example

char *new_format = "DD-MM-YYY";
cgar *new_lang = "French";
...
EXEC SQL CONTEXT OBJECT SET DATEFORMAT, DATELANG to :new_format, :new_lang;

Related Topics

CONTEXT ALLOCATE on page F-8.

CONTEXT FREE on page F-17.

CONTEXT USE on page F-18

CONTEXT OBJECT OPTION SET on page F-18

CONTEXT USE (Oracle Embedded SQL Directive)

Purpose

To instruct the precompiler to use the specified SQLLIB runtime context on subsequent executable SQL statements.

Prerequisites

The runtime context specified by the CONTEXT USE directive must be previously allocated using the CONTEXT ALLOCATE command.

Syntax

Keywords and Parameters

context

 

is the allocated runtime context to use for subsequent executable SQL statements that follow it. For example, after specifying in your source code which context to use (multiple contexts can be allocated), you can connect to the Oracle Server and perform database operations within the scope of that context.

 

Usage Notes

This statement has no effect on declarative statements such as EXEC SQL INCLUDE or EXEC ORACLE OPTION. It works similarly to the EXEC SQL WHENEVER directive in that it affects all executable SQL statements which positionally follow it in a given source file without regard to standard C scope rules.

For more information on this command, see "Developing Multi-threaded Applications" on page 4-37.

Example

This example illustrates the use of a CONTEXT USE directive in a Pro*C/C++ embedded SQL program:

EXEC SQL CONTEXT USE :ctx1; 

Related Topics

CONTEXT ALLOCATE on F-16

CONTEXT FREE on F-17

ENABLE THREADS on F-36

DECLARE CURSOR (Embedded SQL Directive)

Purpose

To declare a cursor, giving it a name and associating it with a SQL statement or a PL/SQL block.

Prerequisites

If you associate the cursor with an identifier for a SQL statement or PL/SQL block, you must have declared this identifier in a previous DECLARE STATEMENT statement.

Syntax

Keywords and Parameters

AT

 

identifies the database on which the cursor is declared. The database can be identified by either:

 

 

db_name

 

is a database identifier declared in a previous DECLARE DATABASE statement.

 

 

:host_variable

 

is a host variable whose value is a previously declared db_name.

 

 

 

If you omit this clause, Oracle8 declares the cursor on your default database.

 

cursor

 

is the name of the cursor to be declared.

 

SELECT command

 

is a SELECT statement to be associated with the cursor. The following statement cannot contain an INTO clause.

 

statement_name block_name

 

identifies a SQL statement or PL/SQL block to be associated with the cursor. The statement_name or block_name must be previously declared in a DECLARE STATEMENT statement.

 

Usage Notes

You must declare a cursor before referencing it in other embedded SQL statements. The scope of a cursor declaration is global within its precompilation unit and the name of each cursor must be unique in its scope. You cannot declare two cursors with the same name in a single precompilation unit.

You can reference the cursor in the WHERE clause of an UPDATE or DELETE statement using the CURRENT OF syntax, provided that the cursor has been opened with an OPEN statement and positioned on a row with a FETCH statement. For more information on this command, see "Using Cursors" on page 6-19.

Example

This example illustrates the use of a DECLARE CURSOR statement:

EXEC SQL DECLARE emp_cursor CURSOR 
    FOR SELECT ename, empno, job, sal 
        FROM emp 
        WHERE deptno = :deptno 
        FOR UPDATE OF sal; 

Related Topics

CLOSE command on F-11

DECLARE DATABASE command on F-24

DECLARE STATEMENT command on F-25

DELETE command on F-30

FETCH command on F-41

OPEN command on F-58

PREPARE command on F-60

SELECT command on F-66

UPDATE command on F-71

DECLARE DATABASE (Oracle Embedded SQL Directive)

Purpose

To declare an identifier for a non-default database to be accessed in subsequent embedded SQL statements.

Prerequisites

You must have access to a username on the non-default database.

Syntax

Keywords and Parameters

db_name

 

is the identifier established for the non-default database.

 

Usage Notes

You declare a db_name for a non-default database so that other embedded SQL statements can refer to that database using the AT clause. Before issuing a CONNECT statement with an AT clause, you must declare a db_name for the non-default database with a DECLARE DATABASE statement.

For more information on this command, see "Single Explicit Connections" on page 4-25.

Example

This example illustrates the use of a DECLARE DATABASE directive:

EXEC SQL DECLARE oracle3 DATABASE 

Related Topics

COMMIT command on F-12

CONNECT command on F-14

DECLARE CURSOR command on F-22

DECLARE STATEMENT command on F-25

DELETE command on F-30

EXECUTE command on F-38

EXECUTE IMMEDIATE command on F-40

INSERT command on F-45

SELECT command on F-66

UPDATE command on F-71

DECLARE STATEMENT (Embedded SQL Directive)

Purpose

To declare an identifier for a SQL statement or PL/SQL block to be used in other embedded SQL statements.

Prerequisites

None.

Syntax

Keywords and Parameters

AT

 

identifies the database on which the SQL statement or PL/SQL block is declared. The database can be identified by either:

 

 

db_name

 

is a database identifier declared in a previous

DECLARE DATABASE statement.

 

 

:host_variable

 

is a host variable whose value is a db_name.

 

 

If you omit this clause, Oracle8 declares the SQL statement or PL/SQL block on your default database.

 

statement_name, block_name

 

is the declared identifier for the statement.

 

Usage Notes

You must declare an identifier for a SQL statement or PL/SQL block with a DECLARE STATEMENT statement only if a DECLARE CURSOR statement referencing the identifier appears physically (not logically) in the embedded SQL program before the PREPARE statement that parses the statement or block and associates it with its identifier.

The scope of a statement declaration is global within its precompilation unit, like a cursor declaration. For more information on this command, see Chapter 3, "Developing a Pro*C/C++ Application" and Chapter 13, "Using Dynamic SQL".

Example I

This example illustrates the use of the DECLARE STATEMENT statement:

EXEC SQL AT remote_db DECLARE my_statement STATEMENT; 
EXEC SQL PREPARE my_statement FROM :my_string; 
EXEC SQL EXECUTE my_statement; 

Example II

In this example from a Pro*C/C++ embedded SQL program, the DECLARE STATEMENT statement is required because the DECLARE CURSOR statement precedes the PREPARE statement:

EXEC SQL DECLARE my_statement STATEMENT; 
EXEC SQL DECLARE emp_cursor CURSOR FOR my_statement; 
EXEC SQL PREPARE my_statement FROM :my_string; 
...

Related Topics

CLOSE command on F-11

DECLARE DATABASE command on F-24

FETCH command on F-41

PREPARE command on F-60

OPEN command on F-58

DECLARE TABLE (Oracle Embedded SQL Directive)

Purpose

To define the structure of a table or view, including each column's datatype, default value, and NULL or NOT NULL specification for semantic checking by the Oracle Precompilers.

Prerequisites

None.

Syntax

For relational tables, the syntax is:

For object tables, the syntax is:.

Keywords and Parameters

table

 

is the name of the declared table.

 

column

 

is a column of the table.

 

datatype

 

is the datatype of a column. For information on Oracle8 datatypes, see "Oracle Datatypes" on page 3-17.

If the datatype is a user-defined object, a size may be entered in parentheses. The size cannot be a macro or a complex C expression. The size can be omitted. See examples.

 

DEFAULT

 

specifies the default value of a column.

 

expr

 

is any expression usable as a default column value in a CREATE TABLE statement.

 

NULL

 

specifies that a column can contain nulls.

 

NOT NULL

 

specifies that a column cannot contain nulls.

 

WITH DEFAULT

 

is supported for compatibility with the IBM DB2 database.

 

Usage Notes

For information on using this command, see "Using DECLARE TABLE" on page D-5.

Examples

The following statement declares the PARTS table with the PARTNO, BIN, and QTY columns:

EXEC SQL DECLARE parts TABLE 
     (partno NUMBER  NOT NULL, 
      bin    NUMBER, 
      qty    NUMBER); 

For an object datatype, the use of size in a parenthesis is optional:

EXEC SQL DECLARE person TYPE AS OBJECT (name VARCHAR2(20), age INT);
...
EXEC SQL DECLARE odjtab1 TABLE OF person;

Alternatively, the equivalent table declaration can be (size cannot be given as a macro or a complex C expression):

EXEC SQL DECLARE odjtab2 TABLE OF person(20);

Related Topics

See DECLARE TYPE on F-29.

DECLARE TYPE (Oracle Embedded SQL Directive)

Purpose

To define the attributes of a type for a semantics check by the precompiler.

Prerequisites

None.

Syntax

Keywords and Parameters

type

 

is the name of the declared type

 

column

 

is a column

 

datatype

 

is the datatype of the column

 

Usage Notes

For information on using this command, see "Using DECLARE TYPE" on page D-5.

Example

EXEC SQL DECLARE project_type TYPE (
              pno           CHAR(5),
              pname         CHAR(20),
              budget        NUMBER);

Related Topics

DECLARE TABLE directive on F-27.

DELETE (Executable Embedded SQL)

Purpose

To remove rows from a table or from a view's base table.

Prerequisites

For you to delete rows from a table, the table must be in your own schema or you must have DELETE privilege on the table.

For you to delete rows from the base table of a view, the owner of the schema containing the view must have DELETE privilege on the base table. Also, if the view is in a schema other than your own, you must be granted DELETE privilege on the view.

The DELETE ANY TABLE system privilege also allows you to delete rows from any table or any view's base table.

If you are using Trusted Oracle in DBMS MAC mode, your DBMS label must dominate the creation label of the table or view or you must meet one of the following criteria:

In addition, for each row to be deleted, your DBMS label must match the row's label or you must meet one of the following criteria:

Syntax

Keywords and Parameters

AT

 

identifies the database to which the DELETE statement is issued. The database can be identified by either:

 

 

db_name

 

is a database identifier declared in a previous

DECLARE DATABASE statement.

 

 

:host_integer

 

is a host variable whose value is a previously declared db_name

 

 

If you omit this clause, the DELETE statement is issued to your default database.

 

FOR :host_integer

 

limits the number of times the statement is executed if the WHERE clause contains array host variables. If you omit this clause, Oracle8 executes the statement once for each component of the smallest array.

 

schema

 

is the schema containing the table or view. If you omit schema, Oracle8 assumes the table or view is in your own schema.

 

table view

 

is the name of a table from which the rows are to be deleted. If you specify view, Oracle8 deletes rows from the view's base table.

 

db_link

 

is the complete or partial name of a database link to a remote database where the table or view is located. For information on referring to database links, see the Oracle8 SQL Reference . You can only delete rows from a remote table or view if you are using Oracle8 with the distributed option.

 

 

If you omit dblink, Oracle8 assumes that the table or view is located on the local database.

 

part_name

 

is a partition within the table

 

alias

 

is an alias assigned to the table. Aliases are generally used in DELETE statements with correlated queries.

 

WHERE

 

specifies which rows are deleted:

 

 

condition

 

deletes only rows that satisfy the condition. This condition can contain host variables and optional indicator variables. See the syntax description of condition in Oracle8 SQL Reference .

 

 

CURRENT OF

 

deletes only the row most recently fetched by the cursor. The cursor cannot be associated with a SELECT statement that performs a join, unless its FOR UPDATE clause specifically locks only one table.

 

 

If you omit this clause entirely, Oracle8 deletes all rows from the table or view.

 

Usage Notes

The host variables in the WHERE clause must be either all scalars or all arrays. If they are scalars, Oracle8 executes the DELETE statement only once. If they are arrays, Oracle8 executes the statement once for each set of array components. Each execution may delete zero, one, or multiple rows.

Array host variables in the WHERE clause can have different sizes. In this case, the number of times Oracle8 executes the statement is determined by the smaller of the following values:

If no rows satisfy the condition, no rows are deleted and the SQLCODE returns a NOT_FOUND condition.

The cumulative number of rows deleted is returned through the SQLCA. If the WHERE clause contains array host variables, this value reflects the total number of rows deleted for all components of the array processed by the DELETE statement.

If no rows satisfy the condition, Oracle8 returns an error through the SQLCODE of the SQLCA. If you omit the WHERE clause, Oracle8 raises a warning flag in the fifth component of SQLWARN in the SQLCA. For more information on this command and the SQLCA, see Chapter 11, "Handling Runtime Errors".

You can use Comments in a DELETE statement to pass instructions, or hints, to the Oracle8 optimizer. The optimizer uses hints to choose an execution plan for the statement. For more information on hints, see Oracle8 Tuning.

Example

This example illustrates the use of the DELETE statement within a Pro*C/C++ embedded SQL program:

EXEC SQL DELETE FROM emp 
    WHERE deptno = :deptno 
    AND job = :job; ... 
EXEC SQL DECLARE emp_cursor CURSOR 
    FOR SELECT empno, comm 
        FROM emp; 
EXEC SQL OPEN emp_cursor; 
EXEC SQL FETCH c1 
    INTO :emp_number, :commission; 
EXEC SQL DELETE FROM emp 
    WHERE CURRENT OF emp_cursor; 

Related Topics

DECLARE DATABASE command on F-24

DECLARE STATEMENT command on F-25

DESCRIBE (Executable Embedded SQL)

Purpose

To initialize a descriptor to hold descriptions of host variables for a dynamic SQL statement or PL/SQL block.

Prerequisites

You must have prepared the SQL statement or PL/SQL block in a previous embedded SQL PREPARE statement.

Syntax

Keywords and Parameters

BIND VARIABLES

 

initializes the descriptor to hold information about the input variables for the SQL statement or PL/SQL block.

 

SELECT LIST FOR

 

initializes the descriptor to hold information about the select list of a SELECT statement.

 

 

The default is SELECT LIST FOR.

 

statement_name block_name

 

identifies a SQL statement or PL/SQL block previously prepared with a PREPARE statement.

 

descriptor

 

is the name of the descriptor to be initialized.

 

Usage Notes

You must issue a DESCRIBE statement before manipulating the bind or select descriptor within an embedded SQL program.

You cannot describe both input variables and output variables into the same descriptor.

The number of variables found by a DESCRIBE statement is the total number of place-holders in the prepare SQL statement or PL/SQL block, rather than the total number of uniquely named place-holders. For more information on this command, see Chapter 13, "Using Dynamic SQL".

Example

This example illustrates the use of the DESCRIBE statement in a Pro*C/C++ embedded SQL program:

EXEC SQL PREPARE my_statement FROM :my_string; 
EXEC SQL DECLARE emp_cursor 
    FOR SELECT empno, ename, sal, comm 
            FROM emp 
            WHERE deptno = :dept_number;
EXEC SQL DESCRIBE BIND VARIABLES FOR my_statement 
    INTO bind_descriptor; 
EXEC SQL OPEN emp_cursor 
    USING bind_descriptor; 
EXEC SQL DESCRIBE SELECT LIST FOR my_statement 
    INTO select_descriptor; 
EXEC SQL FETCH emp_cursor 
    INTO select_descriptor; 

Related Topics

PREPARE command on F-60

ENABLE THREADS (Executable Embedded SQL Extension)

Purpose

To initialize a process that supports multiple threads.

Prerequisites

You must be developing a precompiler application for and compiling it on a platform that supports multi-threaded applications, and THREADS=YES must be specified on the command line.

Syntax

Keywords and Parameters

None.

Usage Notes

The ENABLE THREADS command must be executed before any other executable SQL statement and before spawning any thread. This command does not require a host-variable specification.

For more information on this command, see "Developing Multi-threaded Applications" on page 4-37.

Example

This example illustrates the use of the ENABLE THREADS command in a Pro*C/C++ embedded SQL program:

EXEC SQL ENABLE THREADS;

Related Topics

CONTEXT ALLOCATE on F-16

CONTEXT FREE on F-17

CONTEXT USE on F-18

EXECUTE ... END-EXEC (Executable Embedded SQL Extension)

Purpose

To embed an anonymous PL/SQL block into an Oracle Precompiler program.

Prerequisites

None.

Syntax

Keywords and Parameters

AT

 

identifies the database on which the PL/SQL block is executed. The database can be identified by either:

 

 

db_name

 

is a database identifier declared in a previous

DECLARE DATABASE statement.

 

 

:host_variable

 

is a host variable whose value is a previously declared db_name.

 

 

If you omit this clause, the PL/SQL block is executed on your default database.

 

pl/sql_block

 

For information on PL/SQL, including how to write PL/SQL blocks, see the PL/SQL User's Guide and Reference.

 

END-EXEC

 

must appear after the embedded PL/SQL block, regardless of which programming language your Oracle Precompiler program uses. The keyword END-EXEC must be followed by the C/C++ statement terminator, ";".

 

Usage Notes

Since the Oracle Precompilers treat an embedded PL/SQL block like a single embedded SQL statement, you can embed a PL/SQL block anywhere in an Oracle Precompiler program that you can embed a SQL statement. For more information on embedding PL/SQL blocks in Oracle Precompiler programs, see Chapter 6, "Using Embedded PL/SQL".

Example

Placing this EXECUTE statement in a Pro*C/C++ program embeds a PL/SQL block in the program:

EXEC SQL EXECUTE 
    BEGIN 
        SELECT ename, job, sal 
            INTO :emp_name:ind_name, :job_title, :salary 
            FROM emp 
            WHERE empno = :emp_number; 
        IF :emp_name:ind_name IS NULL 
            THEN RAISE name_missing; 
        END IF; 
    END; 
END-EXEC; 

Related Topics

EXECUTE IMMEDIATE embedded SQL command on F-40

EXECUTE (Executable Embedded SQL)

Purpose

To execute a DELETE, INSERT, or UPDATE statement or a PL/SQL block that has been previously prepared with an embedded SQL PREPARE statement.

Prerequisites

You must first prepare the SQL statement or PL/SQL block with an embedded SQL PREPARE statement.

Syntax

Keywords and Parameters

FOR :host_integer

 

limits the number of times the statement is executed when the USING clause contains array host variables If you omit this clause, Oracle8 executes the statement once for each component of the smallest array.

 

statement_id

 

is a precompiler identifier associated with the SQL statement or PL/SQL block to be executed. Use the embedded SQL PREPARE command to associate the precompiler identifier with the statement or PL/SQL block.

 

USING

 

specifies a list of host variables with optional indicator variables that Oracle8 substitutes as input variables into the statement to be executed. The host and indicator variables must be either all scalars or all arrays.

 

Usage Notes

For more information on this command, see Chapter 13, "Using Dynamic SQL".

Example

This example illustrates the use of the EXECUTE statement in a Pro*C/C++ embedded SQL program:

EXEC SQL PREPARE my_statement 
    FROM :my_string; 
EXEC SQL EXECUTE my_statement
    USING :my_var; 

Related Topics

DECLARE DATABASE command on F-24

PREPARE command on F-60

EXECUTE IMMEDIATE (Executable Embedded SQL)

Purpose

To prepare and execute a DELETE, INSERT, or UPDATE statement or a PL/SQL block containing no host variables.

Prerequisites

None.

Syntax

Keywords and Parameters

AT

 

identifies the database on which the SQL statement or PL/SQL block is executed. The database can be identified by either:

 

 

db_name

 

is a database identifier declared in a previous DECLARE DATABASE statement.

 

 

:host_variable

 

is a host variable whose value is a previously declared db_name.

 

 

If you omit this clause, the statement or block is executed on your default database.

 

text

 

is a quoted text literal containing the SQL statement or PL/SQL block to be executed.

 

 

The SQL statement can only be a DELETE, INSERT, or UPDATE statement.

 

Usage Notes

When you issue an EXECUTE IMMEDIATE statement, Oracle8 parses the specified SQL statement or PL/SQL block, checking for errors, and executes it. If any errors are encountered, they are returned in the SQLCODE component of the SQLCA.

For more information on this command, see Chapter 13, "Using Dynamic SQL".

Example

This example illustrates the use of the EXECUTE IMMEDIATE statement:

EXEC SQL EXECUTE IMMEDIATE 'DELETE FROM emp WHERE empno = 9460' 

Related Topics

PREPARE command on F-60

EXECUTE command on F-38

FETCH (Executable Embedded SQL)

Purpose

To retrieve one or more rows returned by a query, assigning the select list values to host variables.

Prerequisites

You must first open the cursor with an the OPEN statement.

Syntax

Keywords and Parameters

FOR :host_integer

 

limits the number of rows fetched if you are using array host variables. If you omit this clause, Oracle8 fetches enough rows to fill the smallest array.

 

cursor

 

is a cursor that is declared by a DECLARE CURSOR statement. The FETCH statement returns one of the rows selected by the query associated with the cursor.

 

:cursor_variable

 

is a cursor variable is allocated an ALLOCATE statement. The FETCH statement returns one of the rows selected by the query associated with the cursor variable.

 

INTO

 

specifies a list of host variables and optional indicator variables into which data is fetched. These host variables and indicator variables must be declared within the program.

 

USING

 

specifies the descriptor referenced in a previous DESCRIBE statement. Only use this clause with dynamic embedded SQL, method 4. Also, the USING clause does not apply when a cursor variable is used.

 

Usage Notes

The FETCH statement reads the rows of the active set and names the output variables which contain the results. Indicator values are set to -1 if their associated host variable is null. The first FETCH statement for a cursor also sorts the rows of the active set, if necessary.

The number of rows retrieved is specified by the size of the output host variables and the value specified in the FOR clause. The host variables to receive the data must be either all scalars or all arrays. If they are scalars, Oracle8 fetches only one row. If they are arrays, Oracle8 fetches enough rows to fill the arrays.

Array host variables can have different sizes. In this case, the number of rows Oracle8 fetches is determined by the smaller of the following values:

Of course, the number of rows fetched can be further limited by the number of rows that actually satisfy the query.

If a FETCH statement does not retrieve all rows returned by the query, the cursor is positioned on the next returned row. When the last row returned by the query has been retrieved, the next FETCH statement results in an error code returned in the SQLCODE element of the SQLCA.

Note that the FETCH command does not contain an AT clause. You must specify the database accessed by the cursor in the DECLARE CURSOR statement.

You can only move forward through the active set with FETCH statements. If you want to revisit any of the previously fetched rows, you must reopen the cursor and fetch each row in turn. If you want to change the active set, you must assign new values to the input host variables in the cursor's query and reopen the cursor. For more information, see "Using the FETCH Statement" on page 5-13.

Example

This example illustrates the FETCH command:

EXEC SQL DECLARE emp_cursor CURSOR FOR 
    SELECT job, sal FROM emp WHERE deptno = 30; 
EXEC SQL OPEN emp_cursor;
... 
EXEC SQL WHENEVER NOT FOUND GOTO ... 
for(;;)
    {
    EXEC SQL FETCH emp_cursor INTO :job_title1, :salary1; 
    EXEC SQL FETCH emp_cursor INTO :job_title2, :salary2; 
    ... 
    }

Related Topics

PREPARE command on F-60

DECLARE CURSOR command on F-22

OPEN command on F-58

CLOSE command on F-11

FREE (Executable Embedded SQL Extension)

Purpose

To free memory in the object cache.

Prerequisites

The memory has to have been already allocated.

An active database connection must exist.

Syntax

Keywords and Parameters

dbname

 

a null-terminated string containing the database connection name, as established previously in a CONNECT statement. If omitted, or if an empty string, the default database connection is assumed.

 

host_ptr

 

a pointer to a host struct generated by OTT for an object type in the object cache

 

Usage Notes

Any memory in the object cache will be freed automatically when the connection is terminated. See "FREE" on page 8-6 for more information.

Example

EXEC SQL FREE :ptr;

Related Topics

ALLOCATE command on F-8.

CACHE FREE ALL command on F-10.

INSERT (Executable Embedded SQL)

Purpose

To add rows to a table or to a view's base table.

Prerequisites

For you to insert rows into a table, the table must be in your own schema or you must have INSERT privilege on the table.

For you to insert rows into the base table of a view, the owner of the schema containing the view must have INSERT privilege on the base table. Also, if the view is in a schema other than your own, you must have INSERT privilege on the view.

The INSERT ANY TABLE system privilege also allows you to insert rows into any table or any view's base table.

If you are using Trusted Oracle in DBMS MAC mode, your DBMS label must match the creation label of the table or view:

Syntax

Keywords and Parameters

AT

 

identifies the database on which the INSERT statement is executed. The database can be identified by either:

 

 

db_name

 

is a database identifier declared in a previous DECLARE DATABASE statement.

 

 

:host_variable

 

is a host variable whose value is a db_name

 

 

If you omit this clause, the INSERT statement is executed on your default database.

 

FOR :host_integer

 

limits the number of times the statement is executed if the VALUES clause contains array host variables. If you omit this clause, Oracle8 executes the statement once for each component in the smallest array.

 

schema

 

is the schema containing the table or view. If you omit schema, Oracle8 assumes the table or view is in your own schema.

 

table

view

 

is the name of the table into which rows are to be inserted. If you specify view, Oracle8 inserts rows into the view's base table.

 

db_link

 

is a complete or partial name of a database link to a remote database where the table or view is located. For information on referring to database links, see the Oracle8 SQL Reference . You can only insert rows into a remote table or view if you are using Oracle8 with the distributed option.

 

 

If you omit dblink, Oracle8 assumes that the table or view is on the local database.

 

part_name

 

is a partition in the table

 

column

 

is a column of the table or view. In the inserted row, each column in this list is assigned a value from the VALUES clause or the query.

 

part_name

 

is the name of partition in the table

 

 

If you omit one of the table's columns from this list, the column's value for the inserted row is the column's default value as specified when the table was created. If you omit the column list altogether, the VALUES clause or query must specify values for all columns in the table.

 

VALUES

 

specifies a row of values to be inserted into the table or view. See the syntax description in Oracle8 SQL Reference. Note that the expressions can be host variables with optional indicator variables. You must specify an expression in the VALUES clause for each column in the column list.

 

subquery

 

is a subquery that returns rows that are inserted into the table. The select list of this subquery must have the same number of columns as the column list of the INSERT statement. For the syntax description of a subquery, see "SELECT" in Oracle8 SQL Reference.

 

Usage Notes

Any host variables that appear in the WHERE clause must be either all scalars or all arrays. If they are scalars, Oracle8 executes the INSERT statement once. If they are arrays, Oracle8 executes the INSERT statement once for each set of array components, inserting one row each time.

Array host variables in the WHERE clause can have different sizes. In this case, the number of times Oracle8 executes the statement is determined by the smaller of the following values:

For more information on this command, see "Using the INSERT Statement" on page 5-9.

Example I

This example illustrates the use of the embedded SQL INSERT command:

EXEC SQL 
     INSERT INTO emp (ename, empno, sal) 
     VALUES (:ename, :empno, :sal); 

Example II

This example shows an embedded SQL INSERT command with a subquery:

EXEC SQL 
   INSERT INTO new_emp (ename, empno, sal) 
   SELECT ename, empno, sal FROM emp
   WHERE deptno = :deptno; 

Related Topics

DECLARE DATABASE command on F-24

OBJECT CREATE (Executable Embedded SQL Extension)

Purpose

To create a referenceable object in the object cache.

Prerequisites

Precompiler option OBJECTS must be set to YES. The INTYPE option must specify the OTT-generated type files. Include OTT-generated header files in your program.

Syntax

where tab is:

Keywords and Parameters

Usage Notes

For usage notes as well as keywords and parameters, see "OBJECT CREATE" on page 8-10.

Example

person *pers_p;
person_ind *pers_ind;
person_ref *pers_ref;
...
EXEC SQL CREATE :pers_p:pers_ind TABLE PERSON_TAB
          RETURNING REF INTO :pers_ref;

Related Topics

See all other OBJECT statements in this appendix.

OBJECT DELETE (Executable Embedded SQL Extension)

Purpose

To mark a persistent object or array of objects as deleted in the object cache.

Prerequisites

Precompiler option OBJECTS must be set to YES. The INTYPE option must specify the OTT-generated type files. Include OTT-generated header files in your program.

Syntax

Keywords and Parameters

Usage Notes

For usage notes as well as keywords and parameters, see "OBJECT DELETE" on page 8-12.

Example

customer *cust_p;
...
EXEC SQL OBJECT DELETE :cust_p;


Related Topics

See all other OBJECT statements in this Appendix.For persistent objects,this statement marks an object or array of objects as deleted in the object cache.

OBJECT DEREF (Executable Embedded SQL Extension)

Purpose

To pin an object or array of objects in the object cache.

Prerequisites

Precompiler option OBJECTS must be set to YES. The INTYPE option must specify the OTT-generated type files. Include OTT-generated header files in your program.

Syntax

Keywords and Parameters

Usage Notes

For usage notes as well as keywords and parameters, see "OBJECT DEREF" on page 8-11.

Example

person *pers_p;
person_ref *pers_ref;
...
/* Pin the person REF, returning a pointer to the person object */
EXEC SQL OBJECT DEREF :pers_ref INTO :pers_p;

Related Topics

See all other OBJECT statements in this Appendix. See "ALLOCATE (Executable Embedded SQL Extension)" on page F-8.

OBJECT FLUSH (Executable Embedded SQL Extension)

Purpose

To flush persistent objects that have been marked as updated, deleted, or created, to the server.

Prerequisites

Precompiler option OBJECTS must be set to YES. The INTYPE option must specify the OTT-generated type files. Include OTT-generated header files in your program.

Syntax

Keywords and Parameters

Usage Notes

For usage notes as well as keywords and parameters, see "OBJECT FLUSH" on page 8-12.

Example

person *pers_p;
...
EXEC SQL OBJECT DELETE :pers_p;
/* Flush the changes, effectively deleting the person object */
EXEC SQL OBJECT FLUSH :pers_p;

/* Finally, free all object cache memory and logoff */
EXEC SQL OBJECT CACHE FREE ALL;
EXEC SQL COMMIT WORK RELEASE;

Related Topics

See all other OBJECT statements in this Appendix.

OBJECT GET (Executable Embedded SQL Extension)

Purpose

To convert attributes of an object type to native C types

Prerequisites

Precompiler option OBJECTS must be set to YES. The INTYPE option must specify the OTT-generated type files. Include OTT-generated header files in your program.

Syntax

Keywords and Parameters

Usage Notes

For usage notes as well as keywords and parameters, see "OBJECT GET" on page 8-17.

Example

person *pers_p;
struct { char lname[21], fname[21]; int age; } pers;
...
/* Convert object types to native C types */
EXEC SQL OBJECT GET lastname, firstname, age FROM :pers_p INTO :pers;
printf("Last Name: %s\nFirstName: %s\nAge: %d\n",
        pers.lname, pers.fname, pers.age );

Related Topics

See all other OBJECT statements in this Appendix.

OBJECT RELEASE (Executable Embedded SQL Extension)

Purpose

To unpin an object in the object cache. When an object is not pinned and not updated, it is eligible for implicit freeing.

Prerequisites

Precompiler option OBJECTS must be set to YES. The INTYPE option must specify the OTT-generated type files. Include OTT-generated header files in your program.

Syntax

Keywords and Parameters

Usage Notes

For usage notes as well as keywords and parameters, see "OBJECT RELEASE" on page 8-12.

Example

person *pers_p;
...
EXEC SQL OBJECT RELEASE :pers_p;

Related Topics

See all other OBJECT statements in this Appendix.

OBJECT SET (Executable Embedded SQL Extension)

Purpose

To update attributes of persistent objects, marking them eligible for writing to the server when the object is flushed or the cache is flushed.

To update the attributes of a transient object.

Prerequisites

Precompiler option OBJECTS must be set to YES. The INTYPE option must specify the OTT-generated type files. Include OTT-generated header files in your program.

Syntax

Keywords and Parameters

Usage Notes

For usage notes as well as keywords and parameters, see "OBJECT SET" on page 8-15.

Example

person *pers_p;
struct {int num; char street[61], city[31], state[3], zip[11];} addr1;
...
addr1.num = 500;
strcpy((char *)addr1.street , (char *)"Oracle Parkway");
strcpy((char *)addr1.city,    (char *)"Redwood Shores");
strcpy((char *)addr1.state,   (char *)"CA");
strcpy((char *)addr1.zip,     (char *)"94065");

/* Convert native C types to object types */
EXEC SQL OBJECT SET :pers_p->addr TO :addr1;

Related Topics

See all other OBJECT statements in this Appendix.

OBJECT UPDATE (Executable Embedded SQL Extension)

Purpose

Prerequisites

Precompiler option OBJECTS must be set to YES. The INTYPE option must specify the OTT-generated type files. Include OTT-generated header files in your program.

Syntax

Keywords and Parameters

Usage Notes

For usage notes as well as keywords and parameters, see OBJECT UPDATE.

Example

person *pers_p;
...
/* Mark as updated */
EXEC SQL OBJECT UPDATE :pers_p;

Related Topics

See all other OBJECT statements in this Appendix.

OPEN (Executable Embedded SQL)

Purpose

To open a cursor, evaluating the associated query and substituting the host variable names supplied by the USING clause into the WHERE clause of the query.

Prerequisites

You must declare the cursor with a DECLARE CURSOR embedded SQL statement before opening it.

Syntax

Keywords and Parameters

cursor

 

is the cursor to be opened.

 

USING

 

specifies the host variables to be substituted into the WHERE clause of the associated query.

 

:host_variable

 

specifies a host variable with an optional indicator variable to be substituted into the statement associated with the cursor.

 

DESCRIPTOR

 

specifies a descriptor that describes the host variables to be substituted into the WHERE clause of the associated query. The descriptor must be initialized in a previous DESCRIBE statement.

 

 

The substitution is based on position. The host variable names specified in this statement can be different from the variable names in the associated query.

 

Usage Notes

The OPEN command defines the active set of rows and initializes the cursor just before the first row of the active set. The values of the host variables at the time of the OPEN are substituted in the statement. This command does not actually retrieve rows; rows are retrieved by the FETCH command.

Once you have opened a cursor, its input host variables are not reexamined until you reopen the cursor. To change any input host variables and therefore the active set, you must reopen the cursor.

All cursors in a program are in a closed state when the program is initiated or when they have been explicitly closed using the CLOSE command.

You can reopen a cursor without first closing it. For more information on this command, see "Using the INSERT Statement" on page 5-9.

Example

This example illustrates the use of the OPEN command in a Pro*C/C++ embedded SQL program:

EXEC SQL DECLARE emp_cursor CURSOR FOR 
    SELECT ename, empno, job, sal 
    FROM emp 
    WHERE deptno = :deptno; 
EXEC SQL OPEN emp_cursor; 

Related Topics

PREPARE command on F-60

DECLARE CURSOR command on F-22

FETCH command on F-41

CLOSE command on F-11

PREPARE (Executable Embedded SQL)

Purpose

To parse a SQL statement or PL/SQL block specified by a host variable and associate it with an identifier.

Prerequisites

None.

Syntax

Keywords and Parameters

statement_id

 

is the identifier to be associated with the prepared SQL statement or PL/SQL block. If this identifier was previously assigned to another statement or block, the prior assignment is superseded.

 

:host_string

 

is a host variable whose value is the text of a SQL statement or PL/SQL block to be prepared.

 

text

 

is a string literal containing a SQL statement or PL/SQL block to be prepared.

 

select_command

 

is a select command

 

Usage Notes

Any variables that appear in the :host_string or text are placeholders. The actual host variable names are assigned in the USING clause of the OPEN command (input host variables) or in the INTO clause of the FETCH command (output host variables).

A SQL statement is prepared only once, but can be executed any number of times. For more information, see "PREPARE" on page 13-19.

Example

This example illustrates the use of a PREPARE statement in a Pro*C/C++ embedded SQL program:

EXEC SQL PREPARE my_statement FROM :my_string; 
EXEC SQL EXECUTE my_statement;

Related Topics

DECLARE CURSOR command on F-22

OPEN command on F-58

FETCH command on F-41

CLOSE command on F-11

ROLLBACK (Executable Embedded SQL)

Purpose

To undo work done in the current transaction.

You can also use this command to manually undo the work done by an in-doubt distributed transaction.

Prerequisites

To roll back your current transaction, no privileges are necessary.

To manually roll back an in-doubt distributed transaction that you originally committed, you must have FORCE TRANSACTION system privilege. To manually roll back an in-doubt distributed transaction originally committed by another user, you must have FORCE ANY TRANSACTION system privilege.

Syntax

Keywords and Parameters

WORK

 

is optional and is provided for ANSI compatibility.

 

TO

 

rolls back the current transaction to the specified savepoint. If you omit this clause, the ROLLBACK statement rolls back the entire transaction.

 

FORCE

 

manually rolls back an in-doubt distributed transaction. The transaction is identified by the text containing its local or global transaction ID. To find the IDs of such transactions, query the data dictionary view DBA_2PC_PENDING.

 

 

ROLLBACK statements with the FORCE clause are not supported in PL/SQL.

 

RELEASE

 

frees all resources and disconnects the application from the Oracle8 Server. The RELEASE clause is not allowed with SAVEPOINT and FORCE clauses.

 

Usage Notes

A transaction (or a logical unit of work) is a sequence of SQL statements that Oracle8 treats as a single unit. A transaction begins with the first executable SQL statement after a COMMIT, ROLLBACK or connection to the database. A transaction ends with a COMMIT statement, a ROLLBACK statement, or disconnection (intentional or unintentional) from the database. Note that Oracle8 issues an implicit COMMIT statement before and after processing any Data Definition Language statement.

Using the ROLLBACK command without the TO SAVEPOINT clause performs the following operations:

Using the ROLLBACK command with the TO SAVEPOINT clause performs the following operations:

It is recommended that you explicitly end transactions in application programs using either a COMMIT or ROLLBACK statement. If you do not explicitly commit the transaction and the program terminates abnormally, Oracle8 rolls back the last uncommitted transaction.

Example I

The following statement rolls back your entire current transaction:

EXEC SQL ROLLBACK; 

Example II

The following statement rolls back your current transaction to savepoint SP5:

EXEC SQL ROLLBACK TO SAVEPOINT sp5;
Distributed Transactions

Oracle8 with the distributed option allows you to perform distributed transactions, or transactions that modify data on multiple databases. To commit or roll back a distributed transaction, you need only issue a COMMIT or ROLLBACK statement as you would any other transaction.

If there is a network failure during the commit process for a distributed transaction, the state of the transaction may be unknown, or in-doubt. After consultation with the administrators of the other databases involved in the transaction, you may decide to manually commit or roll back the transaction on your local database. You can manually roll back the transaction on your local database by issuing a ROLLBACK statement with the FORCE clause.

For more information on when to roll back in-doubt transactions, see Oracle8 Distributed Database Systems.

You cannot manually roll back an in-doubt transaction to a savepoint.

A ROLLBACK statement with a FORCE clause only rolls back the specified transaction. Such a statement does not affect your current transaction.

Example III

The following statement manually rolls back an in-doubt distributed transaction:

EXEC SQL
    ROLLBACK WORK
    FORCE '25.32.87';

Related Topics

COMMIT command on F-12

SAVEPOINT command on

SAVEPOINT (Executable Embedded SQL)

Purpose

To identify a point in a transaction to which you can later roll back.

Prerequisites

None.

Syntax

Keywords and Parameters

AT

 

identifies the database on which the savepoint is created. The database can be identified by either:

 

 

db_name

 

is a database identifier declared in a previous DECLARE DATABASE statement.

 

 

:host_variable

 

is a host variable whose value is a previously declared db_name.

 

 

If you omit this clause, the savepoint is created on your default database.

 

savepoint

 

is the name of the savepoint to be created.

 

Usage Notes

For more information on this command, see "Using the SAVEPOINT Statement" on page 10-5.

Example

This example illustrates the use of the embedded SQL SAVEPOINT command:

EXEC SQL SAVEPOINT save3;

Related Topics

COMMIT command on F-12

ROLLBACK command on F-61

SELECT (Executable Embedded SQL)

Purpose

To retrieve data from one or more tables, views, or snapshots, assigning the selected values to host variables.

Prerequisites

For you to select data from a table or snapshot, the table or snapshot must be in your own schema or you must have SELECT privilege on the table or snapshot.

For you to select rows from the base tables of a view, the owner of the schema containing the view must have SELECT privilege on the base tables. Also, if the view is in a schema other than your own, you must have SELECT privilege on the view.

The SELECT ANY TABLE system privilege also allows you to select data from any table or any snapshot or any view's base table.

If you are using Trusted Oracle in DBMS MAC mode, your DBMS label must dominate the creation label of each queried table, view, or snapshot or you must have READUP system privileges.

Syntax

Keywords and Parameters

AT

 

Identifies the database to which the SELECT statement is issued. The database can be identified by either:

 

 

db_name

 

A database identifier declared in a previous DECLARE DATABASE statement.

 

 

:host_variable

 

Host variable whose value is a previously declared db_name.

 

 

If you omit this clause, the SELECT statement is issued to your default database.

 

select_list

 

Identical to the non-embedded SELECT command except that a host variables can be used in place of literals.

 

INTO

 

Specifies output host variables and optional indicator variables to receive the data returned by the SELECT statement. Note that these variables must be either all scalars or all arrays, but arrays need not have the same size.

 

WHERE

 

Restricts the rows returned to those for which the condition is TRUE. See the syntax description of condition in Oracle8 SQL Reference . The condition can contain host variables, but cannot contain indicator variables. These host variables can be either scalars or arrays.

 

All other keywords and parameters are identical to the non-embedded SQL SELECT command. ASC, ascending, is the default for the ORDER BY clause.

 

Usage Notes

If no rows meet the WHERE clause condition, no rows are retrieved and Oracle8 returns an error code through the SQLCODE component of the SQLCA.

You can use Comments in a SELECT statement to pass instructions, or hints, to the Oracle8 optimizer. The optimizer uses hints to choose an execution plan for the statement. For more information on hints, see Oracle8 Tuning.

Example

This example illustrates the use of the embedded SQL SELECT command:

EXEC SQL SELECT ename, sal + 100, job 
    INTO :ename, :sal, :job 
    FROM emp 
    WHERE empno = :empno; 

Related Topics

DECLARE DATABASE command on F-24

DECLARE CURSOR command on F-22

EXECUTE command on F-38

FETCH command on F-41

PREPARE command on F-60

TYPE (Oracle Embedded SQL Directive)

Purpose

To perform user-defined type equivalencing, or to assign an Oracle8 external datatype to a whole class of host variables by equivalencing the external datatype to a user-defined datatype.

Prerequisites

The user-defined datatype must be previously declared in an embedded SQL program.

Syntax

Keywords and Parameters

type

 

is the user-defined datatype to be equivalenced with an Oracle8 external datatype.

 

datatype

 

is an Oracle8 external datatype recognized by the Oracle Precompilers (not an Oracle8 internal datatype). The datatype may include a length, precision, or scale. This external datatype is equivalenced to the user-defined type and assigned to all host variables assigned the type. For a list of external datatypes, see "Oracle Datatypes" on page 3-17.

 

REFERENCE

 

makes the equivalenced type a pointer type.

 

Usage Notes

User defined type equivalencing is one kind of datatype equivalencing. You can only perform user-defined type equivalencing with the embedded SQL TYPE command in a Pro*C/C++ program. You may want to use datatype equivalencing for one of the following purposes:

Pro*C/C++ also supports the embedded SQL VAR command for host variable equivalencing. For more information, see "User-Defined Type Equivalencing" on page 3-60.

Example I

This example shows an embedded SQL TYPE statement in a Pro*C/C++ Precompiler program:

struct screen {
    short len; 
    char  buff[4002];
}; 

typedef struct screen graphics; 

    EXEC SQL TYPE graphics IS VARRAW(4002); 
    graphics crt;  -- host variable of type graphics 
    ... 

Related Topics

VAR directive on page F-75

UPDATE (Executable Embedded SQL)

Purpose

To change existing values in a table or in a view's base table.

Prerequisites

For you to update values in a table or snapshot, the table must be in your own schema or you must have UPDATE privilege on the table.

For you to update values in the base table of a view, the owner of the schema containing the view must have UPDATE privilege on the base table. Also, if the view is in a schema other than your own, you must have UPDATE privilege on the view.

The UPDATE ANY TABLE system privilege also allows you to update values in any table or any view's base table.

If you are using Trusted Oracle in DBMS MAC mode, your DBMS label must match the creation label of the table or view:

Syntax

Keywords and Parameters

AT

 

identifies the database to which the UPDATE statement is issued. The database can be identified by either:

 

 

dbname

 

is a database identifier declared in a previous DECLARE DATABASE statement.

 

 

:host_variable

 

is a host variable whose value is a previously declared db_name.

 

 

If you omit this clause, the UPDATE statement is issued to your default database.

 

FOR :host_integer

 

limits the number of times the UPDATE statement is executed if the SET and WHERE clauses contain array host variables. If you omit this clause, Oracle8 executes the statement once for each component of the smallest array.

 

schema

 

is the schema containing the table or view. If you omit schema, Oracle8 assumes the table or view is in your own schema.

 

table, view

 

is the name of the table to be updated. If you specify view, Oracle8 updates the view's base table.

 

dblink

 

is a complete or partial name of a database link to a remote database where the table or view is located. For information on referring to database links, see the Oracle8 SQL Reference. You can only use a database link to update a remote table or view if you are using Oracle8 with the distributed option.

 

part_name

 

name of partition

 

alias

 

is a name used to reference the table, view, or subquery elsewhere in the statement.

 

column

 

is the name of a column of the table or view that is to be updated. If you omit a column of the table from the SET clause, that column's value remains unchanged.

 

expr

 

is the new value assigned to the corresponding column. This expression can contain host variables and optional indicator variables. See the syntax of expr in the Oracle8 SQL Reference.

 

subquery_1

 

is a subquery that returns new values that are assigned to the corresponding columns. For the syntax of a subquery, see "SELECT" in Oracle8 SQL Reference. .

 

subquery_2

 

is a subquery that return a new value that is assigned to the corresponding column. For the syntax of a subquery, see "SELECT" in the Oracle8 SQL Reference. .

 

WHERE

 

specifies which rows of the table or view

are updated:

 

 

condition

 

updates only rows for which this condition is true. This condition can contain host variables and optional indicator variables. See the syntax of condition in the Oracle8 SQL Reference. .

 

 

CURRENT OF

 

updates only the row most recently fetched by the cursor. The cursor cannot be associated with a SELECT statement that performs a join unless its FOR UPDATE clause explicitly locks only one table.

 

 

If you omit this clause entirely, Oracle8 updates all rows of the table or view.

 

Usage Notes

Host variables in the SET and WHERE clauses must be either all

scalars or all arrays. If they are scalars, Oracle8 executes the UPDATE statement only once. If they are arrays, Oracle8 executes the statement once for each set of array components. Each execution may update zero, one, or multiple rows.

Array host variables can have different sizes. In this case, the number of times Oracle8 executes the statement is determined by the smaller

of the following values:

The cumulative number of rows updated is returned through the third element of the SQLERRD component of the SQLCA. When arrays are used as input host variables, this count reflects the total number of updates for all components of the array processed in the UPDATE statement. If no rows satisfy the condition, no rows are updated and Oracle8 returns an error message through the SQLCODE element of the SQLCA. If you omit the WHERE clause, all rows are updated and Oracle8 raises a warning flag in the fifth component of the SQLWARN element of the SQLCA.

You can use Comments in an UPDATE statement to pass instructions, or hints, to the Oracle8 optimizer. The optimizer uses hints to choose an execution plan for the statement. For more information on hints, see Oracle8 Tuning.

For more information on this command, see Chapter 5, "Using Embedded SQL", and Chapter 10, "Defining and Controlling Transactions".

Examples

The following examples illustrate the use of the embedded SQL UPDATE command:

EXEC SQL UPDATE emp 
    SET sal = :sal, comm = :comm INDICATOR :comm_ind 
    WHERE ename = :ename; 
 
EXEC SQL UPDATE emp 
    SET (sal, comm) = 
        (SELECT AVG(sal)*1.1, AVG(comm)*1.1 
         FROM emp) 
    WHERE ename = 'JONES'; 

Related Topics

DECLARE DATABASE command on F-24

VAR (Oracle Embedded SQL Directive)

Purpose

To perform host variable equivalencing, or to assign a specific Oracle8 external datatype to an individual host variable, overriding the default datatype assignment. Also has an optional CONVBUFSZ clause that specifies the size of a buffer for character set conversion.

Prerequisites

The host variable must be previously declared in the Pro*C/C++ program.

Syntax

Keywords and Parameters

host_variable

 

is the host variable to be assigned an Oracle8 external datatype.

 

dtyp

 

is an Oracle8 external datatype recognized by the Oracle Precompilers (not an Oracle8 internal datatype). The datatype may include a length, precision, or scale. This external datatype is assigned to the host_variable. For a list of external datatypes, see Chapter 3.

 

len

 

length of the datatype

 

prec

 

precision

 

scale

 

scale

 

size

 

is the size in bytes of a buffer in the Oracle8 runtime library used to perform conversion between character sets of the host_variable

 

Usage Notes

len, prec, scale and buf can be constant expressions.

Host variable equivalencing is one kind of datatype equivalencing. Datatype equivalencing is useful for any of the following purposes:

The Pro*C/C++ Precompiler also supports the precompiler TYPE directive for user-defined type equivalencing. See also "Host Variable Equivalencing" on page 3-59.

Example

This example equivalences the host variable DEPT_NAME to the datatype STRING and the host variable BUFFER to the datatype RAW(200):

EXEC SQL BEGIN DECLARE SECTION; 
    ... 
    char dept_name[15];               -- default datatype is CHAR 
    EXEC SQL VAR dept_name IS STRING; -- reset to STRING 
    ... 
    char buffer[200];                 -- default datatype is CHAR 
    EXEC SQL VAR buffer IS RAW(200); -- refer to RAW 
    ...
EXEC SQL END DECLARE SECTION; 

Related Topics

TYPE directive on page F-69

WHENEVER (Embedded SQL Directive)

Purpose

To specify the action to be taken when an error or warning results from executing an embedded SQL program.

Prerequisites

None.

Syntax

Keywords and Parameters

NOT FOUND

 

Identifies any exception condition that returns an error code of +1403 to SQLCODE (or a +100 code when MODE=ANSI).

 

SQLERROR

 

Identifies a condition that results in a negative return code.

 

SQLWARNING

 

Identifies a non-fatal warning condition.

 

CONTINUE

 

Indicates that the program should progress to the next statement.

 

GOTO

 

Indicates that the program should branch to the statement named by label.

 

STOP

 

Stops program execution.

 

DO

 

Indicates that the program should call a function that is named routine

 

DO BREAK

 

Performs a break statement from a loop when the condition is met.

 

DO CONTINUE

 

Performs a continue statement from a loop when the condition is met.

 

Usage Notes

The WHENEVER command allows your program to transfer control to an error handling routine in the event an embedded SQL statement results in an error or warning.

The scope of a WHENEVER statement is positional, rather than logical. A WHENEVER statement applies to all embedded SQL statements that textually follow it in the source file, not in the flow of the program logic. A WHENEVER statement remains in effect until it is superseded by another WHENEVER statement checking for the same condition.

For more information on this command, see "Using the WHENEVER Statement" on page 11-24.

Do not confuse the WHENEVER embedded SQL command with the WHENEVER SQL*Plus command.

Example

The following example illustrates the use of the WHENEVER command in an embedded SQL program:

EXEC SQL WHENEVER NOT FOUND CONTINUE;
... 
EXEC SQL WHENEVER SQLERROR GOTO sql_error; 
... 
sql_error: 
    EXEC SQL WHENEVER SQLERROR CONTINUE; 
    EXEC SQL ROLLBACK RELEASE;
... 

Related Topics

None




Prev

Next
Oracle
Copyright © 1997 Oracle Corporation.

All Rights Reserved.

Library

Product

Contents

Index