SQL Programming Manual for Pascal

Using Dynamic SQL
HP NonStop SQL Programming Manual for Pascal528614-001
7-9
Overview of a Dynamic SQL Program
OUTPUT_SQLDA_PTR... { pointer to output SQLDA }
INPUT_NAMESBUF_PTR... { pointer to input names buffer }
OUTPUT_NAMESBUF_PTR... { pointer to output names
buffer }
STATEMENT_BUFFER... { buffer for the statement
typed }
{ in by the user }
STATEMENT_HOSTVAR... { name of the statement that
was }
{ entered --for use with
PREPARE }
{ DESCRIBE, OPEN, FETCH, CLOSE }
CURSOR_HOSTVAR... { name of the cursor to use for }
{ processing the SELECT
statement}
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.
7. Forward declare any error handling procedures:
PROCEDURE HANDLE_ERROR; FORWARD;
8. Specify WHENEVER directives for error handling:
EXEC SQL WHENEVER SQLERROR CALL :HANDLE_ERROR;
EXEC SQL WHENEVER SQLWARNING CONTINUE;
You can put the WHENEVER directives anywhere, but you must declare or forward
declare the error handling procedures before you declare the WHENEVER
directives.
Reading and Compiling the Statement
1. Read the SQL statement you want to execute.
2. 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;
Use the information returned to the SQLSA structure after the PREPARE statements
executes in the following steps.
Handling the Input Parameters
Perform Steps 1 through 6 if SQLSA.PREPARE.INPUT_NUM > 0.