Embedded SQL
The general format of embedded SQL statements is:
Where:
| Identifier | Description |
|---|---|
| statementList | is one or more COBOL statements or the continue statement |
| sqlStatement | is one of the following SQL statements |
| declareCursorStatement | is a DECLARE ... CURSOR statement as described below |
| connectStatement | is a CONNECT statement as described below |
| disconnectStatement | is a DISCONNECT statement as described below |
The WHENEVER statement declares a set of statements that will be executed whenever the specified condition occurs
as the result of an embedded SQL statement execution. It remains in effect until another WHENEVER statement is
encountered in the source programs lexical order. The actual effect of WHENEVER is to cause the generation of the
appropriate check for the specified condition after every subsequent SQL operation that could cause the condition to
occur.
DECLARE ... CURSOR
Where:
| Identifier | Description |
|---|---|
| sqlIdentifier | is an identifier beginning with a letter or an underscore (_), and containing only letters, numbers, _, $ |
| selectStatement | is a SELECT statement as described below |
| callStatement | is a CALL statement as described below |
This statement declares a cursor for later use by the program. It may appear in the data division or the procedure
division of the program. The scope of the defined cursor is lexical. That is, the cursor named is defined according
to the declaration until another DECLARE ... CURSOR statement with the same cursor name is encountered in the
source program.
The sensitivity, motion, and concurrency of the cursor are accepted as part of the cursor declaration. P3/COBOL maps the effective cursor declaration to the corresponding JDBC result-set type and concurrency when the cursor is opened.
A cursor is treated as updatable when its SELECT statement, or the prepared statement to which it refers, contains an
Oracle Pro*SQL-style FOR UPDATE [OF ...] phrase. A cursor is also treated as updatable when a positioned UPDATE or
DELETE later references it with WHERE CURRENT OF even if the cursor declaration did not contain FOR UPDATE. In that
case, P3/COBOL forces the cursor to an updatable, insensitive result set and reports a warning if this overrides an
explicit cursor sensitivity or concurrency declaration such as READ ONLY.
The database and JDBC driver must still support an updatable result set for the selected query. Some queries, joins, or driver combinations may not be updatable even though the cursor is requested as updatable.
exec sql
declare my-cursor cursor for select * from my-table
end execCONNECT
The syntax that may be used for the CONNECT statement is:
Where:
| Identifier | Description |
|---|---|
| hostVariable | is a bound COBOL data item with an optional indicator item (see Host Variable Binding for details) |
| literal | is an alphanumeric SQL literal delimited by "'" characters |
This statement (re)connects the active database to the specified database or data source.
Example of a DB2 style connect:
01 database-user.
05 pic x occurs 0 to 4 times depending on database-user-len.
77 database-user-len pic 9 value 2.
01 database-pass.
05 pic x occurs 0 to 4 times depending on database-pass-len.
77 database-pass-len pic 9 value 2.
...
move 4 to database-user-len
move "test" to database-user
move 4 to database-pass-len
move "pass" to database-pass
exec sql
connect user :database-user using :database-pass
end-exec
exec sql
connect reset
end-execExample of an Oracle style connect:
exec sql
connect :database-user identified by :database-pass
end-exec
exec sql
disconnect
end-exec
DISCONNECT
The syntax that may be used for the DISCONNECT statement is:
Example of a DISCONNECT:
exec sql
disconnect
end-execDECLARE ... STATEMENT
Where:
| Identifier | Description |
|---|---|
| sqlIdentifier | is an identifier beginning with a letter or an underscore (""), and containing only letters, numbers, "", "$" |
This statement is for documentation purposes only. It affects neither generated code nor runtime behavior.
DECLARE ... TABLE
Where:
| Identifier | Description |
|---|---|
| sqlIdentifier | is an identifier beginning with a letter or an underscore (""), and containing only letters, numbers, "", "$" |
This statement is for documentation purposes only. It affects neither generated code nor runtime behavior.
DELETE/INSERT/UPDATE
Where:
| Identifier | Description |
|---|---|
| intMaxOccurence | is a bound reference to an integer data item that determines the maximum occurrence value to be used in this statement |
| statementBody | is the remainder of the DELETE/INSERT/UPDATE statement that is accepted by the underlying database |
This statement deletes, inserts, or updates the connected database according to the specified SQL DELETE, INSERT, or
UPDATE statement body. The body may be specified using any SQL syntax that is considered valid by the actual connected
database manager.
Positioned UPDATE and DELETE
P3/COBOL supports positioned UPDATE and DELETE statements using Oracle Pro*SQL-style WHERE CURRENT OF cursor
references.
exec sql
update customer
set status = :new-status
where current of customer-cursor
end-execexec sql
delete customer
where current of customer-cursor
end-execThe referenced cursor must be declared and open. When P3/COBOL detects a positioned UPDATE or DELETE, it marks the
referenced cursor as updatable. This allows common Pro*SQL-style code to omit FOR UPDATE from the cursor declaration
while still obtaining an updatable JDBC result set.
If the cursor's SELECT statement includes FOR UPDATE [OF ...], the cursor is also opened as updatable even before a
positioned statement is encountered. If both forms are absent, the cursor is opened read-only by default.
SELECT
The syntax that may be used for a SELECT statement is:
Where:
| Identifier | Description |
|---|---|
| statementBody | is the balance (after the "SELECT" keyword) of any select statement that is valid for the underlying database |
| hostVariable | is a bound COBOL data item with an optional indicator item (see Host Variable Binding for details) |
This statement selects a result rowset whose rows can be retrieved using the FETCH statement, or a single row bound
to the host variable(s) specified in the INTO clause, if present.
PREPARE
Where:
| Identifier | Description |
|---|---|
| sqlIdentifier | is an identifier beginning with a letter or an underscore (""), and containing only letters, numbers, "", "$" |
| hostVariable | is a bound COBOL data item with an optional indicator item (see Host Variable Binding for details) |
| literal | is an alphanumeric SQL literal delimited by "'" characters |
This statement prepares a SELECT statement query for later execution by OPENing a cursor on the prepared statement
name or by using the EXECUTE statement.
FETCH
Where:
| Identifier | Description |
|---|---|
| intMaxOccurence | is a bound reference to an integer data item that determines the maximum occurrence value to be used in this statement |
| cursorName | is the name of a declared cursor |
| hostVariable | is a bound COBOL data item with an optional indicator item (see Host Variable Binding for details) |
This statement fetches rows from the rowset defined by the specified declared and open cursor. The cursor direction
indicated is commentary only. That is, all cursors are fetched in the forward (NEXT) direction without regard to a
contrary specification in this statement.
An OPEN cursor_name statement must precede the FETCH statement, and the cursor must be open. The data type of the
host_variable values must be compatible with the data type of the corresponding database column.
The FOR clause, when present, causes the FETCH operation to be used for targeted host_variable occurrences up to
the indicated host_integer index value.
ALTER
Where:
| Identifier | Description |
|---|---|
| statementBody | is the balance (after the ALTER keyword) of any ALTER statement accepted by the underlying database |
This statement is passed through to the connected database unchanged.
EXECUTE
Where:
| Identifier | Description |
|---|---|
| sqlIdentifier | is an identifier beginning with a letter or an underscore (""), and containing only letters, numbers, "", "$" |
| hostVariable | is a bound COBOL data item with an optional indicator item (see Host Variable Binding for details) |
| literal | is an alphanumeric SQL literal delimited by "'" characters |
| sqlTail | is the balance of a PL/SQL block started by the "BEGIN" keyword |
This statement executes the indicated SQL statement and may produce a result set that can be retrieved by the FETCH
statement when the cursor form of this statement is used.
There are three variants of this statement:
- Immediate
- PL/SQL
- Prepared statement
CALL
Where:
| Identifier | Description |
|---|---|
| sqlTail | is the remainder of a stored procedure CALL statement that is accepted by the underlying database |
This statement is passed through to the connected database unchanged.
CLOSE
Where:
| Identifier | Description |
|---|---|
| cursorName | is the name of a declared cursor |
This statement closes an open cursor and discards any result set associated with the cursor.
COMMIT
Where:
| Identifier | Description |
|---|---|
| literal | is a literal commentary string |
This statement commits all uncommitted changes made since last commit.
LOCK
Where:
| Identifier | Description |
|---|---|
| tableName | is a valid reference to a table for the underlying database |
This statement is currently treated as commentary. It affects neither generated code nor runtime behavior.
OPEN
Where:
| Identifier | Description |
|---|---|
| cursorName | is the name of a declared cursor |
This statements opens the specified cursor an executes the prepared query. A parameter may be optionally provided, in which case the first parameter marker or host variable in the prepared statement is replaced with the value of the supplied parameter value.
ROLLBACK
Where:
| Identifier | Description |
|---|---|
| savepointName | is the name of a previously established savepoint |
| literal | is a literal commentary string |
SAVEPOINT
Where:
| Identifier | Description |
|---|---|
| savepointName | is the name of a savepoint to be used for a possible rollback |
SET
Where:
| Identifier | Description |
|---|---|
| segmentName | is the name of a rollback segment to be associated with this transaction |
| dbVariable | is the name of a variable known to the underlying database |
| sqlTail | is the remainder of a SET = command that is valid for the underlying database |