When binding occurs

By default, binding occurs each time an Embedded SQL statement executes, using a host variable. When an Embedded SQL statement executes more than once, as in a loop, binding occurs at each execution. For example, in the following loop, each execution of the insert statement associates its host variables with the same Adaptive Server values. Yet, by default, binding occurs for each execution:

for (i = 1; i <= 3; i++)
 {
  exec sql insert into titles (title_id, title)
  values (:bk_id, :bk_title);
  /* 
  ** Binding occurs here at each execution. 
  ** When a statement undergoes binding, all 
  ** its host variables get bound. 
  */
 }

For most statements, bindings do not persist from one statement to the next, even if you request persistent binding. For example, the following insert statements, though identical and consecutive, share no bindings:

exec sql insert into titles (title_id, title)
     values (:bk_id, :bk_title);
 /* Binding occurs for the first statement. */
 
 exec sql insert into titles (title_id, title)
     values (:bk_id, :bk_title);
 /* Binding occurs for the second statement. */
 
 exec sql insert into titles (title_id, title)
     values (:bk_id, :bk_title);
 /* Binding occurs for the third statement. */

For Embedded SQL statements that execute more than once—such as the insert statement in the preceding for loop—you can specify whether binding should occur only at the first execution or at each subsequent execution as well.

To control persistent binding, you use precompiler options to specify the binding behavior of all the statements in a file. Precompiler options do not let you control the binding behavior of individual statements. The precompiler options that control binding are explained later in this chapter.