SQL Context
An SQL Context is the collection of contextual information relating to database connections, cursors and diagnostics that results from the execution of SQL statements by one (logical) user.
To illustrate this, consider a user of an SQL application. The user may connect to one database in order to compile a list of books on a particular subject. Then the user may connect to a second database to check whether a specific book is currently in stock in the warehouse. In making these database queries, the user has acquired an SQL Context that consists of two database connections plus information on the SQL cursors that were used to perform the searches. In addition, any diagnostic information, such as the success or failure of the SQL queries, will form part of the SQL Context.
So the SQL Context is the result of Embedded SQL statements that have been executed by a user and it also forms the basis for the execution of further statements in the future.
SQL Language Support for Embedded SQL Contexts
Embedded SQL provides language support for Embedded SQL Contexts by implementing the following Embedded SQL statements:
EXEC SQL ALLOCATE SQL CONTEXT AS :hv;
and
EXEC SQL DEALLOCATE CONTEXT AS :hv;
These statements instruct the Embedded SQL as to which SQL Context is in effect at any given time.
The ALLOCATE SQL CONTEXT statement identifies the SQL Context that is to be used for subsequent SQL statements. The host variable :hv must contain the address of a SAGContext structure (a data type defined by the client support libraries). Simple textual scoping rules apply in regards to Embedded SQL Contexts: the closest preceding ALLOCATE SQL CONTEXT statement for any given SQL statement defines the SQL Context that is to be used in the resulting call to the database.
Note:
The ALLOCATE SQL CONTEXT call itself allocates no memory; it merely specifies which SQL Context is active for subsequent SQL statements. The application must allocate whatever memory is required for the SAGContext structures that it uses. It is also essential that all SAGContext structures be initialized to zero before use.
The DEALLOCATECONTEXT statement signifies that the SQL Context referenced by the host variable :hv is no longer required, and that the resources associated with it can be released. (Note that an SQL Context can only be deallocated when all of its database connections have been disconnected.)