Example: dispcust.4gl (function query_cust)
This function retrieves a row from the customer table and displays
it in a form.
Function
query_cust:01 FUNCTION query_cust() -- displays one row
02 DEFINE l_custrec RECORD
03 store_num LIKE customer.store_num,
04 store_name LIKE customer.store_name,
05 addr LIKE customer.addr,
06 addr2 LIKE customer.addr2,
07 city LIKE customer.city,
08 state LIKE customer.state,
09 zip_code LIKE customer.zip_code,
10 contact_name LIKE customer.contact_name,
11 phone LIKE customer.phone
12 END RECORD
13
14 SELECT store_num,
15 store_name,
16 addr,
17 addr2,
18 city,
19 state,
20 zip_code,
21 contact_name,
22 phone
23 INTO l_custrec.*
24 FROM customer
25 WHERE store_num = 101
26
27 DISPLAY BY NAME l_custrec.*
28 MESSAGE "Customer " || l_custrec.store_num ||
29 " displayed."
30 END FUNCTION
Note:
- Line
01is the beginning of the functionquery_cust. No variables are passed to the function. - Lines
02thru12DEFINEa recordl_custrecasLIKEcolumns in thecustomerdatabase table, listing each variable separately. - Line
14thru25SELECT ... INTOcan be used, since the statement will retrieve only one row from the database. TheSELECTstatement 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 theSELECTlist retrieves values for all the variables in the program record, in the order listed in theDEFINEstatement, the shorthandINTO l_custrec.*can be used. - Line
27The names in the program recordl_custrecmatch the names of screen fields on the form, soDISPLAY BY NAMEcan be used.l_custrec.*indicates that all of the members of the program record are to be displayed. - Lines
28and29A string for theMESSAGEstatement is concatenated together using the double pipe ( || ) operator and displayed. The message consists of the string "Customer", the value ofl_custrec.store_num, and the string "displayed".
There are no additional statements in the function, so the program returns to the
MENU statement, awaiting the user's next action.