Method 2 example

The following example demonstrates using prepare and execute in method 2. This example prompts the user to enter a where clause that determines which rows in the titles table to update and a multiplier to modify the price. According to what the user elects, the appropriate string is concatenated to the update statement stored in host variable “sqlstring”.

exec sql begin declare section;     CS_CHAR sqlstring[200];     CS_FLOAT multiplier; exec sql end declare section;
char    cond[150];exec sql whenever sqlerror perform err_p(); exec sql whenever sqlwarning perform warn_p();printf(“Enter search condition:”); scanf(“%s”, cond); printf(“Enter price multiplier: “); scanf(“%f”, &multiplier);strcpy(sqlstring,     “update titles set price = price * ? where “); strcat(sqlstring, cond);exec sql prepare update_statement from :sqlstring;exec sql execute update_statement using      :multiplier;exec sql commit;}