Example: custquery.4gl (function fetch_cust)
This function is designed so that it can be reused each time a row is to be fetched
from the customer database table; a variable is passed to indicate whether
the cursor should move forward one row or backward one row.
Function
fetch_cust:01 FUNCTION fetch_cust(p_fetch_flag)
02 DEFINE p_fetch_flag SMALLINT,
03 fetch_ok SMALLINT
04
05 LET fetch_ok = FALSE
06 IF (p_fetch_flag = 1) THEN
07 FETCH NEXT cust_curs
08 INTO mr_custrec.*
09 ELSE
10 FETCH PREVIOUS cust_curs
11 INTO mr_custrec.*
12 END IF
13
14 IF (SQLCA.SQLCODE = NOTFOUND) THEN
15 LET fetch_ok = FALSE
16 ELSE
17 LET fetch_ok = TRUE
18 END IF
19
20 RETURN fetch_ok
21
22 END FUNCTIONNote:
- Line
01The functionfetch_custaccepts a parameter and stores it in the local variablep_fetch_flag. - Line
03defines a variable,fetch_ok, to serve as an indicator whether theFETCHwas successful. - Lines
06thru12tests the value ofp_fetch_flag, moving the cursor forward withFETCH NEXTif the value is1, and backward withFETCH PREVIOUSif the value is-1. The values of the row in thecustomerdatabase table are fetched into the program variables of themr_custrecrecord. TheINTOmr_custrec.*syntax requires that the program variables in the recordmr_custrecare in the same order as the columns are listed in theSELECTstatement. - Lines
14thru15testsSQLCA.SQLCODEand sets the value offetch_oktoFALSEif the fetch did not return a row. If theFETCHwas successful,fetch_okis set toTRUE. - Line
20returns the value offetch_okto the calling function. - Line
22is the end of the functionfetch_cust.