EXEC SQL BEGIN DECLARE SECTION; short stock_num; char description[16]; dec_t unit_price; char manu_code[4]; EXEC SQL END DECLARE SECTION;
printf("RSETNULL Sample ESQL Program running.\n\n"); EXEC SQL connect to 'stores7'; /* connect to stores7 */ exp_chk("Connect to stores7", NOWARNNOTIFY);
printf("This program selects all rows for a given manufacturer\n"); printf("from the stock table and allows you to set the unit_price\n"); printf("\nTo begin, enter a manufacturer code - for example: 'HSK'\n"); printf("\nEnter Manufacturer code: "); /* prompt for mfr. code */ gets(manu_code); /* get mfr. code */ EXEC SQL declare upcurs cursor for /* declare cursor */ select stock_num, description, unit_price from stock where manu_code = :manu_code for update of unit_price; rupshift(manu_code); /* Make mfr code upper case */ EXEC SQL open upcurs; /* open select cursor */ if(exp_chk("Open cursor", WARNNOTIFY) == 1) exit(1);
/* * Display Column Headings
GBase 8s ESQL/C 编程指南 南大通用数据技术股份有限公司 - 896 -
*/ printf("\nStock # \tDescription \t\tUnit Price"); while(1) { /* get a row */ EXEC SQL fetch upcurs into :stock_num, :description, :unit_price; if ((ret = exp_chk("fetch", WARNNOTIFY)) == 100) /* if end of rows */ break; if(ret == 1) exit(1); if(risnull(CDECIMALTYPE, (char *) &unit_price)) /* unit_price NULL? */ continue; /* skip to next row */ rfmtdec(&unit_price, format, decdsply); /* format unit_price */ /* display item */ printf("\n\t%d\t%15s\t%s", stock_num, description, decdsply); ans = ' '; /* Set unit_price to NULL? y(es) or n(o) */ while((ans = LCASE(ans)) != 'y' && ans != 'n') { printf("\n. . . Set unit_price to NULL ? (y/n) "); scanf("%1s", &ans); } if (ans == 'y') /* if yes, NULL to unit_price */ { rsetnull(CDECIMALTYPE, (char *) &unit_price); EXEC SQL update stock set unit_price = :unit_price where current of upcurs; /* and update current row */ if(exp_chk("UPDATE", WARNNOTIFY) == 1) exit(1); }
GBase 8s ESQL/C 编程指南 南大通用数据技术股份有限公司 - 897 -
} printf("\nRSETNULL Sample Program over.\n\n"); }
/* * The exp_chk() file contains the exception handling functions to check the SQLSTATE status variable to see if an error has occurred following an SQL statement. If a warning or an error has occurred, exp_chk() executes the GET DIAGNOSTICS statement and prints the detail for each exception that is returned. */
EXEC SQL include exp_chk.ec
输出 RSETNULL Sample ESQL Program running.
This program selects all rows for a given manufacturer from the stock table and allows you to set the unit_price to NULL.
To begin, enter a manufacturer code - for example: 'HSK'
Enter Manufacturer code: HSK
Stock # Description Unit Price 1 baseball gloves $800.00 . . . Set unit_price to NULL ? (y/n) n
3 baseball bat $240.00 . . . Set unit_price to NULL ? (y/n) y
4 football $960.00 . . . Set unit_price to NULL ? (y/n) n
GBase 8s ESQL/C 编程指南 南大通用数据技术股份有限公司 - 898 -
110 helmet $600.00 . . . Set unit_price to NULL ? (y/n) y