Example: custdata.4gl (Function cust_name_exists)
The cust_name_exists
function verifies that the name passed as parameter
is no already used by a customer table row, with a number different from the second
parameter.
The
cust_name_exists
function in
custdata.4gl: 1 PRIVATE FUNCTION cust_name_exists(
2 curr_name LIKE customer.cust_name,
3 curr_cust t_cust_num)
4 RETURNS BOOLEAN
5 DEFINE name LIKE customer.cust_name
6 SELECT cust_name INTO name FROM customer
7 WHERE cust_name = curr_name AND cust_num != curr_cust
8 RETURN ( SQLCA.SQLCODE == 0 )
9 END FUNCTION
Note:
- Line
1
thru4
declares the function, with two parameters and a boolean return value. - Line
2
defines the first parametercust_name
with the same data type than thecustomer.cust_name
SQL column. - Line 5 defines a local function variable that will be used in the
INTO
clause of theSELECT
. - Line
6
and7
executes theSELECT
statement checking for an existing customer name that matches the name passed as parameter. - Line 8 performs a boolean expression by comparing
SQLCA.SQLCODE
to zero, and returns the result of this boolean expression. We expectSQLCA.SQLCODE
to equalNOTFOUND
(100), so that the function returnsFALSE
.