3. Embedded SQL for COBOL : Preprocessor Operation : Coding Requirements for Embedded SQL Programs : Embedded SQL Statements in IF and PERFORM Blocks
 
Share this page                  
Embedded SQL Statements in IF and PERFORM Blocks
The preprocessor may produce several COBOL statements for a single embedded SQL statement. In most circumstances, the statements can be simply nested in the scope of a COBOL IF or PERFORM statement.
There are some embedded SQL statements for which the preprocessor generates COBOL paragraphs and paragraph names. These statements are:
select-loop
display
formdata
unloadtable
submenu
These statements cannot be nested in the scope of a COBOL IF or PERFORM statement because of the paragraph names the preprocessor generates for them.
These statements must not contain labels.
Another consequence of these generated paragraphs is that they may terminate the scope of a local COBOL paragraph, thus modifying the intended flow of control. For example, a paragraph generated by the preprocessor in a source paragraph may cause the program to return prematurely to the statement following the PERFORM statement that called the source paragraph. To ensure that control does not return prematurely, you must use the THROUGH clause in the PERFORM statement.
The following example demonstrates the use of PERFORM‑THROUGH and an EXIT paragraph to force correct control flow.
      DATA DIVISION.
      WORKING-STORAGE SECTION.
      EXEC SQL BEGIN DECLARE SECTION END-EXEC.
       01 ENAME PIC X(20).
      EXEC SQL END DECLARE SECTION END-EXEC.
* Include SQLCA, declare program variables, etc.
      PROCEDURE DIVISION.
      BEGIN.
* Initialization of program
* Note the THROUGH clause to ensure correct
* control flow.
      PERFORM UNLOAD-TAB THROUGH END-UNLOAD.
* User code
      UNLOAD-TAB.
* This paragraph includes a paragraph generated
* by the preprocessor
      EXEC FRS UNLOADTABLE Empform Employee
         (:ENAME = Lastname) END-EXEC.
      EXEC FRS BEGIN END-EXEC.
            EXEC SQL INSERT into person (name)
                 VALUES (:ENAME)
                 END-EXEC.
      EXEC FRS END END-EXEC.
* This paragraph-name and EXIT statement cause
* control to pass back to the caller's scope
      END-UNLOAD.
            EXIT.
      USER-PARAGRAPH.
* Program continues