SQL Programming Manual for TAL

Dynamic NonStop SQL Operations
HP NonStop SQL Programming Manual for TAL527887-001
7-9
Overview of a Dynamic SQL Program
5. Declare the SQLCODE variable and any host variables:
INT sqlcode; -- required for error processing
EXEC SQL BEGIN DECLARE SECTION;
! Pointers to input and output SQLDAs. (The SQLDAs will
! be dynamically allocated later.)
INT .isqlda^ptr (sqlda^templ);
INT .osqlda^ptr (sqlda^templ);
! Pointers to input and output names buffers.
INT .inamesbuf^ptr (namesbuf^templ);
INT .onamesbuf^ptr (namesbuf^templ);
! Buffer for the statement entered by the user.
! MAXLEN is a literal you define for the statement length.
STRING statement^buffer[0:MAXLEN];
! Variable for the statement name. Use with PREPARE,
! DESCRIBE, OPEN, FETCH, CLOSE. LEN is a literal you
! define for the length of the name.
STRING statement^hostvar[0:LEN];
! Variable for the name of the cursor to use for
! processing the statement.
STRING cursor^hostvar[0:LEN];
EXEC SQL END DECLARE SECTION;
The variables STATEMENT^HOSTVAR and CURSOR^HOSTVAR are optional. You
can code statement and cursor names in the PREPARE and DECLARE CURSOR
statements.
Reading and Compiling the Statement
1. Specify WHENEVER directives for error handling:
EXEC SQL WHENEVER SQLERROR CALL :handle^error;
EXEC SQL WHENEVER SQLWARNING CONTINUE;
You can place the WHENEVER directives anywhere; however, you must declare or
forward declare the error handling procedures before you declare the WHENEVER
directives.
2. Read the SQL statement you want to execute from input from a user.
3. Prepare the SQL statement:
! Using a statement name:
EXEC SQL PREPARE s1 FROM :statement^buffer;
! Using a statement host variable:
statement^hostvar ':=' "s1";
EXEC SQL PREPARE :statement^hostvar
FROM :statement^buffer;