SQL/MP Programming Manual for COBOL85
Dynamic SQL Operations
HP NonStop SQL/MP Programming Manual for COBOL85—429326-004
10-2
Using Dynamic SQL
Using Dynamic SQL
Figure 10-1 shows part of a COBOL program (top) that contains an embedded static
SQL statement and a part of another COBOL program (bottom) that accepts a request
from a user to execute a dynamic SQL statement. The steps used for the dynamic SQL
statements are:
1. Define a character string host variable to hold the dynamic SQL statement.
2. Display a prompt to the user, requesting the SQL statement.
3. Accept the SQL statement into the host variable and determine its length.
4. Execute the SQL statement using an EXECUTE IMMEDIATE statement.
Figure 10-1. Static and Dynamic SQL Programs
3
4
* COBOL program with dynamic SQL statement.
...
* Declare host variables:
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
01 STATEMENT-BUFFER PIC x(256)
...
EXEC SQL END DECLARE SECTION END-EXEC.
...
DISPLAY "Enter SQL statement to be executed".
ACCEPT STATEMENT-VALUE OF STATEMENT-BUFFER.
...
* Determine length of statement (STATEMENT-VALUE).
...
* Execute the dynamic SQL statement:
EXEC SQL
EXECUTE IMMEDIATE :STATEMENT-BUFFER END-EXEC.
...
User enters this SQL command:
"INSERT INTO EMP VALUES ('BROWN', 6400)"
* COBOL program with static SQL statement.
...
EXEC SQL
INSERT INTO EMP VALUES ('BROWN', 6400)" END-
EXEC
...
VST012.vsd
1
2