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)
02 DEFINE p_where_clause STRING,
03 sql_text STRING,
04 fetch_ok SMALLINT
05
06 LET sql_text = "SELECT store_num, " ||
07 " store_name, addr, addr2, city, " ||
08 " state, zip_code, contact_name, phone " ||
09 " FROM customer WHERE " || p_where_clause ||
10 " ORDER BY store_num"
11
12 DECLARE cust_curs SCROLL CURSOR FROM sql_text
13 OPEN cust_curs
14 CALL fetch_cust(1) -- fetch the first row
15 RETURNING fetch_ok
16 IF NOT (fetch_ok) THEN
17 MESSAGE "no rows in table."
18 END IF
19
20 RETURN fetch_ok
21
22 END FUNCTIONNote:
- Line
01The functioncust_selectaccepts as a parameter thewhere_clause, storing it in the local variablep_where_clause. - Lines
06thru10concatenate the entire text of the SQL statement into the localSTRINGvariablesql_txt. - Line
12declares aSCROLL CURSORwith the identifiercust_curs, for theSTRINGvariablesql_text. - Line
13opens the cursor, positioning before the first row of the result set. These statements are physically in the correct order within the module. - Lines
14and15call 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 (see the logic in the functionfetch_cust), which is this case would be the first row. - Line
16Sincefetch_okis defined as aSMALLINT, it can be used as a flag containing the valuesTRUEorFALSE. The value returned from the functionfetch_custindicates whether the fetch was successful. - Line
17displays 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
20returns the value offetch_okto the calling function. This determines whether the functiondisplay_custis called. - Line
22is the end of the functioncust_select.