Dynamic SQL Operations
HP NonStop SQL/MP Programming Manual for C—429847-008
10-64
Detailed Dynamic SQL Program
1214 /* --------------------------------------------- */
1215 /* Define a cursor name for the statement S1, to be */
1216 /* used later in OPEN, FETCH and CLOSE statements */
1217 /* --------------------------------------------- */
1218 exec sql DECLARE C1 CURSOR for S1 ;
1219
1220 /************************************************ */
1221 /* Open the cursor. By this point, all input */
1222 /* parameters must have valid values */
1223 /************************************************ */
1224 if (in_numvars > 0)
1225 exec sql OPEN C1 USING DESCRIPTOR :*sda_i ;
1226 else
1227 exec sql OPEN C1;
1228
1229 if (sqlcode != 0)
1230 { /* display error/warnings */
1231 printf ("\n"); fflush( stdout );
1232 SQLCADISPLAY ( (int *) &sqlca );
1233 if (sqlcode < 0) /* errors present */
1234 {
1235 exec sql rollback work; /* abort transaction */
1236 goto enter_input; /* try again */
1237 }
1238 }
1239
1240 /*****************************************************/
1241 /* FETCH loop */
1242 /*****************************************************/
1243 sqlcode = 0;
1244 num_fetches = 0;
1245
1246 while (sqlcode >= 0) {
1247 exec sql fetch C1 USING DESCRIPTOR :*sda_o ;
1248
1249 if (sqlcode == 100) /* eof */
1250 { printf( "\n--- %lu row(s) selected.\n", num_fetches);
1251 fflush (stdout);
1252 exec sql close C1 ; /* close cursor */
1253 exec sql commit work;
1254 goto enter_input;
1255 }
1256
1257 /* -------------------------------------------------------- */
1258 /* Successful FETCH. Display results */
1259 /* -------------------------------------------------------- */
1260 if (sqlcode >= 0)
1261 {
1262 display_result( sda_o, (char *) cname_o );
1263 num_fetches++; /* increment counter */
1264 }
1265 } /* while loop */
1266
1267 /* ----------------------------------------------------------- */
1268 /* FETCH error. Close cursor. Get next request */
1269 /* ----------------------------------------------------------- */
1270 if (sqlcode < 0)
1271 {
Example 10-8. Detailed Dynamic SQL Program (page 21 of 22)