Host VariablesNext

Chapter 1: Introduction

Note: This book does not provide details about SQL syntax, error messages returned or any use of SQL outside of the COBOL environment. For details of these, refer to the documentation supplied by your database vendor.

1.1 Overview

Server Express includes a number of SQL preprocessors (OpenESQL, the DB2 ECM and COBSQL) which enable you to access relational databases by embedding SQL statements within your COBOL program:

1.2 Embedded SQL

Each of the preprocessors works by taking the SQL statements that you have embedded in your COBOL program and converting them to the appropriate function calls to the database.

Within your COBOL program, each embedded SQL statement must be preceded by the introductory keywords:


and followed by the keyword:


For example:

   SELECT au_lname INTO :lastname FROM authors
      WHERE au_id = '124-59-3864'

The embedded SQL statement can be broken over as many lines as necessary following the normal COBOL rules for continuation, but between the EXEC SQL and END-EXEC keywords you can only code an embedded SQL statement, you cannot include any ordinary COBOL code.

You can use any SQL statement between the EXEC SQL and END-EXEC keywords. A description of standard SQL is beyond the scope of this book and you should refer to the reference manuals supplied with your relational database or one of the many reference books on SQL that are available. However, Embedded SQL provides some extensions to standard SQL that either change the behavior of standard SQL statements or add new functionality. These extensions are summarized in the table below.

A full description of the syntax for each of these statements, together with examples of its use, is given in the appendix Embedded SQL Statements.

BEGIN DECLARE SECTION Marks the beginning of a host variable declaration section
BEGIN TRANSACTION3 Opens a transaction in AUTOCOMMIT mode
CALL3 Executes a stored procedure
CLOSE Ends row-at-a-time data retrieval initiated by the OPEN statement
COMMIT Commits a transaction
COMMIT WORK RELEASE4 Commits a transaction and disconnects from the database
CONNECT Connects to a database
DECLARE CURSOR Defines a cursor for row-at-a-time data retrieval
DECLARE DATABASE Identifies a database
DELETE (POSITIONED)1 Removes the row where the cursor is currently positioned
DELETE (SEARCHED) Removes table rows that meet the search criteria
DESCRIBE Populates an SQLDA data structure
DISCONNECT2 Closes connections to one or all databases
END DECLARE SECTION Marks the end of a host variable declaration section
EXECSP3 Executes a stored procedure
EXECUTE Runs a prepared SQL statement
EXECUTE IMMEDIATE Runs the SQL statement contained in the specified host variable
FETCH For a specified cursor, gets the next row from the results set
INCLUDE Defines a specific SQL data structure for use by an application
INSERT Adds data to a table or view
OPEN Begins row-at-a-time data retrieval for a specified cursor
PREPARE Associates an SQL statement with a name
QUERY ODBC3 Queries the ODBC data dictionary
ROLLBACK Rolls back the current transaction
ROLLBACK WORK RELEASE4 Rolls back a transaction and disconnects from the database
SELECT DISTINCT Associates a cursor name with an SQL statement
SELECT INTO1 Retrieves one row of results (also known as a singleton select)
SET CONCURRENCY3 Sets the concurrency option for standard-mode cursors
SET CONNECTION3 Specifies which database connection to use for subsequent SQL statements
SET OPTION3 Assigns values for query-processing options
SET SCROLLOPTION3 Sets the scrolling technique and row membership for standard-mode cursors
SET TRANSACTION ISOLATION3 Sets the transaction isolation level mode for a connection
UPDATE (POSITIONED)1 Changes data in the row where the cursor is currently positioned
UPDATE (SEARCHED) Changes data in existing rows, either by adding new data or by modifying existing data
WHENEVER Specifies the default action (CONTINUE, GOTO or PERFORM) to be taken after a SQL statement is run


In the table above:

  1. These statements have the same name as a standard SQL statement but the syntax given in the appendix augments the standard SQL syntax.

  2. The DISCONNECT statement is not supported when accessing an Oracle database via COBSQL.

  3. These statements might not work with all ODBC drivers and are not supported by COBSQL.

  4. These statements are supported by COBSQL only when accessing an Oracle Database.

1.2.1 Case

The case of embedded SQL keywords in your programs is ignored, for example:

EXEC SQL CONNECT exec sql connect Exec Sql Connect

are all equivalent.

The case of cursor names, statement names and connection names must match that used when the variable is declared. For example, if you declare a cursor as C1, you must always refer to it a C1 (and not as c1).

The settings for the particular database determine whether other words, such as table and column names, are case-sensitive.

Hyphens are not permitted in SQL identifiers (in table and column names, for example).

1.3 Building your Application

Once you have written your COBOL application containing embedded SQL, you must compile it specifying the appropriate Compiler directive such that the preprocessor converts the embedded SQL statements into function calls to the database:

1.4 Multiple Program Modules

Multiple embedded SQL source files, compiled separately and linked to a single executable file, can share the same database connection at run time. This is also true for programs that are compiled into separate callable shared objects. If subsequent program modules (in the same process) do not process a CONNECT statement, they share the same database connection with the module that included the CONNECT statement.

In a program that includes multiple, separately compiled modules, only one module should be compiled with the INIT option of the SQL Compiler directive. All other modules within the program should share that first automatic connection or make explicit connections using the CONNECT statement.

OpenESQL - DB2

With COBSQL, if the INIT directive is specified more than once, second and subsequent uses are ignored.

Copyright © 2000 MERANT International Limited. All rights reserved.
This document and the proprietary marks and names used herein are protected by international law.

Host VariablesNext