SQL/MX Programming Manual for C and COBOL (G06.24+, H06.03+)
C Sample Programs
HP NonStop SQL/MX Programming Manual for C and COBOL—523627-004
A-2
Using a Static SQL Cursor
SQLSTATE[5]='\0'; 
SQLSTATE_OK[5]='\0'; 
SQLSTATE_NODATA[5]='\0'; 
printf("\n\nThis example uses a static cursor. \n\n");
EXEC SQL WHENEVER SQLERROR GOTO end_prog;
EXEC SQL DECLARE CATALOG 'samdbcat';
EXEC SQL DECLARE SCHEMA 'sales';
/* Declare the static cursor. */
EXEC SQL DECLARE get_by_partnum CURSOR FOR 
 SELECT partnum, partdesc, price, qty_available
 FROM parts
 WHERE partnum >= :in_partnum
 FOR UPDATE OF partdesc, price, qty_available;
/* Initialize the host variable in the WHERE clause. */
printf("Enter lowest part number to be retrieved: ");
scanf("%hu", &in_partnum);
EXEC SQL BEGIN WORK; /* Begin transaction. */
/* Open the cursor. */ 
EXEC SQL OPEN get_by_partnum; 
/* Fetch the first row of the result table. */
EXEC SQL FETCH get_by_partnum 
 INTO :hv_partnum,:hv_partdesc,:hv_price,:hv_qty_available;
while (strcmp (SQLSTATE, SQLSTATE_NODATA) != 0) { 
 /* If qty_available less than 1000, update qty_available. */
 if ( hv_qty_available < 1000 ) { 
 EXEC SQL UPDATE parts 
 SET qty_available = qty_available + 100
 WHERE CURRENT OF get_by_partnum;
 printf("\nUpdate of part number: %hu\n", hv_partnum);
 } 
 /* Fetch the next row of the result table. */
 EXEC SQL FETCH get_by_partnum 
 INTO :hv_partnum,:hv_partdesc,:hv_price,:hv_qty_available;
} /* end while */
/* Close the cursor. */
EXEC SQL CLOSE get_by_partnum;
/* Commit any changes. */
EXEC SQL COMMIT WORK;
Example A-1. Using a Static SQL Cursor (page2of3)
C










