DB2 10.5 for Linux, UNIX, and Windows

Compound SQL (embedded) statement

Combines one or more other SQL statements (sub-statements) into an executable block.

Invocation

This statement can only be embedded in an application program. The entire compound SQL (embedded) statement construct is an executable statement that cannot be dynamically prepared. The statement is not supported in REXX.

Authorization

No privileges are required to invoke an compound SQL (embedded). However, the privileges held by the authorization ID of the statement must include all necessary privileges to invoke the SQL statements that are embedded in the compound statement.

Syntax

Read syntax diagramSkip visual syntax diagram
>>-BEGIN COMPOUND--+-ATOMIC-----+--STATIC----------------------->
                   '-NOT ATOMIC-'           

>--+---------------------------------------------+-------------->
   '-STOP AFTER FIRST--host-variable--STATEMENTS-'   

   .----------------------.                 
   V                      |                 
>----+------------------+-+--END COMPOUND----------------------><
     '-sql-statement--;-'                   

Description

ATOMIC
Specifies that, if any of the sub-statements within the compound SQL (embedded) statement fail, then all changes made to the database by any of the sub-statements, including changes made by successful sub-statements, are undone.
NOT ATOMIC
Specifies that, regardless of the failure of any sub-statements, the compound SQL (embedded) statement will not undo any changes made to the database by the other sub-statements.
STATIC
Specifies that input variables for all sub-statements retain their original value. For example, if
  SELECT ... INTO :abc ...
is followed by:
  UPDATE T1 SET C1 = 5 WHERE C2 = :abc
the UPDATE statement will use the value that :abc had at the start of the execution of the compound SQL (embedded) statement, not the value that follows the SELECT INTO.

If the same variable is set by more than one sub-statement, the value of that variable following the compound SQL (embedded) statement is the value set by the last sub-statement.

Note: Non-static behavior is not supported. This means that the sub-statements should be viewed as executing non-sequentially and sub-statements should not have interdependencies.
STOP AFTER FIRST
Specifies that only a certain number of sub-statements will be executed.
host-variable
A small integer that specifies the number of sub-statements to be executed.
STATEMENTS
Completes the STOP AFTER FIRST host-variable clause.
sql-statement
All executable statements except the following can be contained within an embedded static compound SQL (embedded) statement:
  CALL                FETCH
  CLOSE               OPEN
  CONNECT             PREPARE
  Compound SQL        RELEASE (Connection)
  DESCRIBE            ROLLBACK
  DISCONNECT          SET CONNECTION
  EXECUTE IMMEDIATE   SET variable
Note: INSERT, UPDATE, and DELETE are not supported in compound SQL for use with nicknames.

If a COMMIT statement is included, it must be the last sub-statement. If COMMIT is in this position, it will be issued even if the STOP AFTER FIRST host-variable STATEMENTS clause indicates that not all of the sub-statements are to executed. For example, suppose COMMIT is the last sub-statement in a compound SQL block of 100 sub-statements. If the STOP AFTER FIRST STATEMENTS clause indicates that only 50 sub-statements are to be executed, then COMMIT will be the 51st sub-statement.

An error will be returned if COMMIT is included when using CONNECT TYPE 2 or running in an XA distributed transaction processing environment (SQLSTATE 25000).

Rules

Notes

Examples