Example: orders.4gl (Function get_stock_info)
This function fetches stock item information using the item number passed as parameter.
Function
get_stock_info
(orders.4gl): 1 PRIVATE FUNCTION get_stock_info(x INTEGER, num INTEGER) RETURNS BOOLEAN
2 DEFINE sql_text STRING
3
4 IF num IS NULL THEN
5 RETURN FALSE
6 END IF
7
8 LET sql_text = SFMT( "SELECT description, unit, %1 FROM stock"
9 || " WHERE stock_num = ? AND fac_code = ?",
10 IIF(order_rec.promo=="N","reg_price","promo_price") )
11
12 PREPARE get_stock_cursor FROM sql_text
13 EXECUTE get_stock_cursor
17 USING num, order_rec.fac_code
14 INTO orditems[x].description,
15 orditems[x].unit,
16 orditems[x].price
18
19 IF sqlca.sqlcode == NOTFOUND THEN
20 CALL comutils.mbox_ok(title1,SFMT(msg16,order_rec.fac_code))
21 LET orditems[x].description = NULL
22 LET orditems[x].unit = NULL
23 LET orditems[x].price = NULL
24 END IF
25
26 RETURN (sqlca.sqlcode == 0)
27
28 END FUNCTION
Note:
- Line
1defines the function, with an integer parameter (x) for theorditemarray index, and the stock item number (num) to fetch information. The function returns aTRUEon success,FALSEif the stock item is not found. - Line
2defines thesql_textstring variable to hold the SQL statement that is constructed in the next lines. - Lines
8thru10: Here we build theSELECTstatement, with a column name defining the price of the item, depending of the promotion flag. The string expression uses aSFMT()function to replace the%1place holder by the second parameter, which is using theIIF()operator, to check the value of the order_rec.promo variable, and use the appropriate column name"reg_price"or"promo_price". - Lines
13thru17execute the prepared SQL statement, using thenumandorder_rec.fac_codevalues for the SQL parameters of theWHEREclause, and fetching the values in theorditems[x]members. - Lines
19thru24check if a row was found and reset theorditems[x]members toNULLafter showing a warning message to the user.. - Line
26evaluates and returns the boolean expression checking thesqlca.sqlcoderegister.