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:
  1 FUNCTION fetch_cust(p_fetch_flag SMALLINT) RETURNS BOOLEAN
  2   IF p_fetch_flag == 1 THEN
  3      FETCH NEXT cust_curs INTO mr_custrec.*
  4   ELSE
  5      FETCH PREVIOUS cust_curs INTO mr_custrec.*
  6   END IF
  7   RETURN (SQLCA.SQLCODE == 0)
  8 END FUNCTION
Note:
  • Line 1 The function fetch_cust accepts a parameter and stores it in the local variable p_fetch_flag. Then function returns a boolean value.
  • Line 2 tests the flag passed as parameter, to fetch forwards or backwards.
  • Line 3 fetches a new row in the forward direction, by using the cursor cust_curs.
  • Line 4 fetches a new row in the backwards direction, by using the cursor cust_curs.
  • Line 7 evaluates and returns a boolean expression based on the value of SQLCA.SQLCODE. If SQLCA.SQLCODE is equal to zero, the function returns TRUE.