Example: dispcust.4gl (function query_cust)
This function retrieves a row from the customer
table and displays
it in a form.
Function
query_cust
: 1 FUNCTION query_cust() RETURNS ()
2 DEFINE l_custrec RECORD
3 cust_num LIKE customer.cust_num,
4 cust_name LIKE customer.cust_name,
5 addr LIKE customer.addr,
6 city LIKE customer.city,
7 state LIKE customer.state,
8 zipcode LIKE customer.zipcode,
9 contact_name LIKE customer.contact_name,
10 phone LIKE customer.phone
11 END RECORD
12
13 SELECT cust_num, cust_name, addr,
14 city, state,zipcode,
15 contact_name, phone
16 INTO l_custrec.*
17 FROM customer
18 WHERE cust_num = 101
19
20 DISPLAY BY NAME l_custrec.*
21
22 MESSAGE SFMT("Customer num %1 retrieved.", l_custrec.cust_num)
23
24 END FUNCTION
Note:
- Line
1
is the beginning of the functionquery_cust
. No variables are passed to the function. Not values are returned, therefore we specifyRETURNS ()
. - Lines
2
thru11
DEFINE
a recordl_custrec
asLIKE
columns in thecustomer
database table, listing each record member separately. - Line
13
thru18
SELECT ... INTO
is used, since the statement will retrieve only one row from the database. TheSELECT
statement lists each column name to be retrieved, rather than usingSELECT *
. This allows for the possibility that additional columns might be added to a table at a future date. Since theSELECT
list retrieves values for all the variables in the program record, in the order listed in theDEFINE
statement, the shorthandINTO l_custrec.*
can be used. - Line
20
: The names in the program recordl_custrec
match the names of screen fields on the form, soDISPLAY BY NAME
can be used.l_custrec.*
indicates that all of the members of the program record are to be displayed. - Lines
22
: TheSFMT()
function is used to build a string that is used by theMESSAGE
instruction to display the information to the user.
There are no additional statements in the function, so the program returns to the
MENU
statement, awaiting the user's next action.