Example custquery.4gl (function cust_select)
This function is called by the function query_cust, if the row count
        returned by the function get_cust_cnt indicates that the criteria
        previously entered by the user and stored in the variable where_clause
        would produce an SQL SELECT result set.
Function
            
cust_select:01 FUNCTION cust_select(p_where_clause STRING) RETURNS BOOLEAN 
02   DEFINE  sql_text STRING,
03         fetch_ok BOOLEAN         
04
05   LET sql_text = "SELECT store_num, " ||
06      " store_name, addr, addr2, city, " ||
07      " state, zip_code, contact_name, phone " ||
08      " FROM customer WHERE " || p_where_clause ||
09      " ORDER BY store_num"
10 
11   DECLARE cust_curs SCROLL CURSOR FROM sql_text 
12   OPEN cust_curs 
13   
14   IF NOT ( fetch_ok := fetch_cust(1) ) THEN
15     MESSAGE "no rows in table."   
16   END IF
17
18   RETURN fetch_ok 
19 
20 END FUNCTIONNote: 
- Line 
01The functioncust_selectaccepts as a parameter thewhere_clause, storing it in the local variablep_where_clause. - Lines 
05thru09concatenate the entire text of the SQL statement into the localSTRINGvariablesql_txt. - Line 
11declares aSCROLL CURSORwith the identifiercust_curs, for theSTRINGvariablesql_text. - Line 
12opens the cursor, positioning before the first row of the result set. These statements are physically in the correct order within the module. - Line
14calls the functionfetch_cust, passing as a parameter the literal value1, and returning a value stored in the local variablefetch_ok. Passing the value1tofetch_custwill result in theNEXTrow of the result set being fetched, which is this case would be the first row. - Line 
15displays a message to the user if theFETCHwas not successful. Since this is the fetch of the first row in the result set, another user must have deleted the rows after the program selected the count. - Line 
18returns the value offetch_okto the calling function. This determines whether the functiondisplay_custis called. - Line 
20is the end of the functioncust_select.