Example: custquery.4gl (function cust_select)
This function declares a scroll cursor from the SELECT statement
build with the where_clause passed as parameter.
Function
cust_select: 1 FUNCTION cust_select(p_where_clause STRING) RETURNS BOOLEAN
2 DEFINE sql_text STRING
3 LET sql_text = "SELECT * FROM customer WHERE "
4 || p_where_clause
5 || " ORDER BY cust_num "
6 TRY
7 DECLARE cust_curs SCROLL CURSOR FROM sql_text
8 OPEN cust_curs
9 CATCH
10 ERROR "SQL error: ", SQLERRMESSAGE
11 RETURN FALSE
12 END TRY
13 IF NOT fetch_cust(1) THEN
14 MESSAGE "No rows in table."
15 RETURN FALSE
16 ELSE
17 RETURN TRUE
18 END IF
19 END FUNCTIONNote:
- Line
1: The functioncust_selectaccepts as a parameter thewhere_clause, storing it in the local variablep_where_clause. A boolean value is returned by the function. - Line
2defines thesql_textvariable as aSTRING. - Lines
3thru5concatenate the entire text of the SQL statement into the localSTRINGvariablesql_text. - Line
6to12define aTRY/CATCHblock, to handle SQL errors when theSELECTstatement is executed. - Line
7declares aSCROLLcursor with the identifiercust_curs, using theSTRINGvariablesql_text. - Line
8opens the cursor, positioning before the first row of the result set. These statements are physically in the correct order within the module. - Line
13calls the functionfetch_cust, passing as a parameter the literal value1, and returning a boolean value. Passing the value1tofetch_custwill result in theNEXTrow of the result set being fetched, which is this case would be the first row. - Line
14displays 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
15returnsTRUEto the calling function. - Line
17returns FALSE to the calling function, indicating that no rows were found byfetch_cust.