SQL/MP Programming Manual for COBOL

Dynamic SQL Operations
HP NonStop SQL/MP Programming Manual for COBOL529758-003
10-3
Using Dynamic SQL
Dynamic SQL can be less efficient than static SQL because more work is deferred until
run time. This table compares stages of processing for static and dynamic SQL:
Figure 10-1. Static and Dynamic SQL Programs
SQL Operation Static SQL Operation Dynamic SQL Operation
Parse the SQL statement At compile time (or as invoked
by COBOL)
Run time
Validate the statement At compile time (or as invoked
by COBOL)
Run time
Optimize the statement SQLCOMP Run time
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