SQL/MP Programming Manual for C
Introduction
HP NonStop SQL/MP Programming Manual for C—429847-008
1-6
Dynamic SQL
Dynamic SQL
With static SQL statements, you code the actual SQL statement in the C source file. 
However, with dynamic SQL, a C program can construct, compile, and run an SQL 
statement at run time. You code a host variable as a placeholder for the dynamic SQL 
statement, which is usually unknown or incomplete until run time. 
A dynamic SQL statement requires some input, often from a user at a terminal, to 
construct the final statement. The statement is constructed at run time from the user’s 
input, compiled by the SQL compiler, and then run by an EXECUTE or EXECUTE 
IMMEDIATE statement. 
Example 1-2 shows a simple example of a dynamic INSERT statement (which is 
similar to the static SQL INSERT statement in Example 1-1 on page 1-4). This program 
example dynamically builds an INSERT statement that inserts information into the 
PARTS table from information entered by a user. 
At run time, the program prompts a user for information to build the INSERT statement. 
The user enters this information in the INTEXT variable:
INSERT INTO $vol5.sales.parts (partnum, price, partdesc) 
 VALUES (4120, 60000.00, "V8 DISK OPTION")
The program moves the statement to the host variable OPERATION. The program has 
declared OPERATION as a host variable so that it is available to both SQL and C 
statements. The program then uses the EXECUTE IMMEDIATE statement to compile 
and run the INSERT statement in OPERATION. (This program could also have used 
the PREPARE and EXECUTE statements to compile and run the statement.) 
For more information, see Section 10, Dynamic SQL Operations
.
Example 1-2. Dynamic SQL Statements in a C Program
/* C source file */ 
...
char intext[201];
EXEC SQL BEGIN DECLARE SECTION;
 char operation[201];
EXEC SQL END DECLARE SECTION;
void dynamic_insert_function(void) 
{
...
/* User enters INSERT statement in the intext variable. */ 
strncpy (operation, intext, 201);
EXEC SQL EXECUTE IMMEDIATE :operation;
}










