SQL Programming Manual for Pascal

NonStop SQL Statements and Directives
HP NonStop SQL Programming Manual for Pascal528614-001
3-16
EXECUTE IMMEDIATE
The following code uses the EXECUTE statement in the case where the parameter
descriptions are unknown:
{ Declare the SQLDA and name it SDAI; declare the }
{ names buffer and name it NAMESINPUT. }
EXEC SQL INCLUDE SQLDA (SDAI, 5, NAMESINPUT, 39);
{ 5 and 39 are arbitrary values for sqlda-size and }
{ name-string-size. Your program can use different values, }
{ or it can allocate memory dynamically. }
{ Declare host variables for the PARTS table and statement: }
EXEC SQL BEGIN DECLARE SECTION;
TYPE
PARTS_TYPE : RECORD
IN_PARTNUM : INT16;
IN_PRICE : INT32;
IN_PARTDESC : FSTRING (18)
END;
VAR IN_PARTS_REC : PARTS_TYPE;
DYNAMIC_INSERT_STATEMENT : FSTRING(180);
EXEC SQL END DECLARE SECTION;
PROCEDURE HANDLE_INSERT;
BEGIN
{ Get the statement from the user: }
READLN (DYNAMIC_INSERT_STATEMENT);
{ Dynamically compile the statement: }
EXEC SQL PREPARE OPERATION FROM :DYNAMIC_INSERT_STATEMENT;
{ Retrieve the parameter descriptions using DESCRIBE INPUT:}
EXEC SQL DESCRIBE INPUT OPERATION INTO :SDAI;
{ Set a pointer in SDAI (VAR_PTR field) to the storage }
{ allocated for the parameter values. }
{ Prompt the user for the values and read them. }
{ Execute the statement using the parameter descriptions in}
{ SDAI, which also points to the values: }
EXEC SQL EXECUTE OPERATION USING DESCRIPTOR :SDAI;
END; { HANDLE_INSERT }
EXECUTE IMMEDIATE
The EXECUTE IMMEDIATE statement compiles and executes an SQL statement
whose text is contained in a host variable.
For the most efficient use of memory, particularly if the program uses many dynamic
SQL statements, the host variable that contains the statement text should be a varying
character item. When you use a varying character item, you must ensure that the
program sets the length of the LEN item to the number of characters in the dynamic
SQL statement and sets the VAL item to the text of the dynamic SQL statement.