SQL/MP Programming Manual for COBOL

Dynamic SQL Operations
HP NonStop SQL/MP Programming Manual for COBOL529758-003
10-18
Preparing the SQL Statement
03 PDECIMAL REDEFINES P PIC S9(9)V9(3) DISPLAY.
03 PLARGINT REDEFINES P PIC 9(18) COMP.
03 PSMLINT REDEFINES P PIC 9(4) COMP.
Declare buffers in working storage. Use redefines to store differing data types.
Preparing the SQL Statement
Before preparing the statement, specify WHENEVER directives for error handling in
the Procedure Division:
EXEC SQL
WHENEVER SQLERROR PERFORM :100-HANDLE-ERROR END-EXEC.
EXEC SQL WHENEVER SQLWARNING CONTINUE END-EXEC.
If you are obtaining the SQL statement from a user, read the statement. In the
Procedure Division, prepare the SQL statement. This example shows the sequence
you can follow for the PREPARE statement, which assigns the name S1 to the SQL
statement:
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
01 STATEMENT-BUFFER PIC X(256).
...
EXEC SQL END DECLARE SECTION END-EXEC.
...
DISPLAY "Enter a new SQL statement:"
ACCEPT STATEMENT-BUFFER.
...
* Check the contents of the statement (The sample program
* checks for permutations of "END" or "SAME")
...
EXEC SQL PREPARE S1 FROM :STATEMENT-BUFFER END-EXEC.
Checking for Parameters
After you have prepared the statement, you can check to see if there are any input or
output parameters (unless you know there are not any). Table 10-4
lists the information
that the PREPARE statement stores in the SQLSA structure.
Table 10-4. SQLSA Contents after a Prepare Operation (page1of2)
SQLSA Field Description
INPUT-NUM Number of input parameters in the SQL statement. Use this
information to decide how many parameter values to solicit
from the user.
INPUT-NAMES-LEN Length of the buffer required to contain the names of the input
parameters.
OUTPUT-NUM Number of output parameters in the statement. Use this
information to decide how many column values to report.