Method 1 examples

The following two examples demonstrate using method 1, execute immediate. The first example prompts the user to enter a statement and then executes it:

exec sql begin declare section; 
 CS_CHAR statement_buffer[linesize]; 
 exec sql end declare section; 
 ... 
 printf("\nEnter statement\n"); 
 gets(statement_buffer); 
 
 exec sql [at connection] execute immediate :statement_buffer; 

The next example prompts the user to enter a search condition to specify rows in the titles table to update. Then, it concatenates the search condition to an update statement and sends the complete statement to Adaptive Server.

exec sql begin declare section; 
 CS_CHAR sqlstring[200]; 
 exec sql end declare section; 
 
 char     cond[150]; 
 
 exec sql whenever sqlerror call err_p(); 
 exec sql whenever sqlwarning call warn_p();

 strcpy(sqlstring, 
 "update titles set price=price*1.10 where "); 
 
 printf("Enter search condition:"); 
 scanf("%s", cond); 
 strcat(sqlstring, cond); 
 
 exec sql execute immediate :sqlstring; 
 
 exec sql commit work;