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 FUNCTION
Note:
- Line
1
: The functioncust_select
accepts as a parameter thewhere_clause
, storing it in the local variablep_where_clause
. A boolean value is returned by the function. - Line
2
defines thesql_text
variable as aSTRING
. - Lines
3
thru5
concatenate the entire text of the SQL statement into the localSTRING
variablesql_text
. - Line
6
to12
define aTRY/CATCH
block, to handle SQL errors when theSELECT
statement is executed. - Line
7
declares aSCROLL
cursor with the identifiercust_curs
, using theSTRING
variablesql_text
. - Line
8
opens the cursor, positioning before the first row of the result set. These statements are physically in the correct order within the module. - Line
13
calls the functionfetch_cust
, passing as a parameter the literal value1
, and returning a boolean value. Passing the value1
tofetch_cust
will result in theNEXT
row of the result set being fetched, which is this case would be the first row. - Line
14
displays a message to the user if theFETCH
was 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
15
returnsTRUE
to the calling function. - Line
17
returns FALSE to the calling function, indicating that no rows were found byfetch_cust
.