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 FUNCTIONNote:
- Line
1thru4declares the function, with two parameters and a boolean return value. - Line
2defines the first parametercust_namewith the same data type than thecustomer.cust_nameSQL column. - Line 5 defines a local function variable that will be used in the
INTOclause of theSELECT. - Line
6and7executes theSELECTstatement checking for an existing customer name that matches the name passed as parameter. - Line 8 performs a boolean expression by comparing
SQLCA.SQLCODEto zero, and returns the result of this boolean expression. We expectSQLCA.SQLCODEto equalNOTFOUND(100), so that the function returnsFALSE.