7 67 3 LEAVE Examples

LANSA Technical

7.67.3 LEAVE Examples

Using LEAVE within a SELECT Loop

Using LEAVE within a BEGIN_LOOP loop

Using LEAVE within a SELECT Loop

At times, when one record has been read in a SELECT loop, there is no need to read further. This example demonstrates how to  achieve this using the LEAVE command within a SELECT loop.

For example, assume you have a pricing file defined like this, sorted by these primary keys:

1.  #ITEMNO (ascending).

2.  #EFF_DATE (ascending).

Item No.
(Packed 7,0)

Effective Date
(Packed 8,0)

Price ($)
(Packed 9,2)

116

2000/01/01

4.00

116

2000/07/01

4.25

116

2001/01/01

4.50

116

2001/07/01

4.75

116

2002/01/01

5.00

 

 

For the purpose of an invoice, a price for item 116 as at 08/02/2001 is required.

DEFINE     FIELD(#REQITEM) REFFLD(#ITEMNO)
DEFINE     FIELD(#REQDATE) REFFLD(#EFF_DATE)
DEFINE     FIELD(#PRICEOUT) REFFLD(#PRICE)
           
BEGIN_LOOP 
REQUEST    FIELDS(#REQITEM #REQDATE)
CHANGE     FIELD(#PRICEOUT) TO(0)
EXECUTE    SUBROUTINE(GETPRICE) WITH_PARMS(#REQITEM #REQDATE #PRICEOUT)
DISPLAY    FIELDS(#REQITEM #PRICEOUT)
END_LOOP   
          
SUBROUTINE NAME(GETPRICE) PARMS((#REQITEM *RECEIVED) (#REQDATE *RECEIVED) (#PRICEOUT *RETURNED))
SELECT     FIELDS(#ITEMNO #EFF_DATE #PRICE) FROM_FILE(PRICING) WHERE('#ITEMNO = #REQITEM') WITH_KEY(#REQITEM #REQDATE) OPTIONS(*BACKWARDS *STARTKEY *ENDWHERE)
CHANGE     FIELD(#PRICEOUT) TO(#PRICE)
LEAVE      
ENDSELECT  
ENDROUTINE 
 

The SELECT positions to the pricing record with an effective date the same as or earlier than the requested date and reads it. If a record is found, the value of  #PRICE is moved into #PRICEOUT. As the required record has been found it is not necessary to keep reading pricing records, so the LEAVE is used to exit from the SELECT loop.

If no record is found for the requested item, prior to (or equal to ) the requested date, the value #PRICEOUT remains zero.

Alternatively, instead of requesting an 'as of' date today's date can be used automatically.

DEFINE     FIELD(#REQITEM) REFFLD(#ITEMNO)
DEFINE     FIELD(#PRICEOUT) REFFLD(#PRICE)
           
BEGIN_LOOP 
REQUEST    FIELDS(#REQITEM)
CHANGE     FIELD(#PRICEOUT) TO(0)
EXECUTE    SUBROUTINE(GETPRICE) WITH_PARMS(#REQITEM #PRICEOUT)
DISPLAY    FIELDS(#REQITEM #PRICEOUT)
END_LOOP   
           
SUBROUTINE NAME(GETPRICE) PARMS((#REQITEM *RECEIVED)(#PRICEOUT *RETURNED))
SELECT     FIELDS(#ITEMNO #EFF_DATE #PRICE) FROM_FILE(PRICING) WHERE('#ITEMNO = #REQITEM') WITH_KEY(#REQITEM *YYYYMMDD) OPTIONS(*BACKWARDS *STARTKEY *ENDWHERE)
CHANGE     FIELD(#PRICEOUT) TO(#PRICE)
LEAVE      
ENDSELECT  
ENDROUTINE
 

Using LEAVE within a BEGIN_LOOP loop

This example demonstrates how to use the LEAVE command within a BEGIN_LOOP loop. With the help of an additional user function key and a defined condition the LEAVE command causes the loop to end when the user presses the 'Finish' key.

DEF_COND   NAME(*FINISHED) COND('#IO$KEY = ''09''')
DEF_LIST   NAME(#EMPBROWSE) FIELDS(#EMPNO #SURNAME #GIVENAME)
 
BEGIN_LOOP 
REQUEST    FIELDS(#EMPNO #SURNAME #GIVENAME) BROWSELIST(#EMPBROWSE) USER_KEYS((09 'Finish'))
LEAVE      IF(*FINISHED)
ADD_ENTRY  TO_LIST(#EMPBROWSE)
END_LOOP   
MESSAGE MSGTXT('Input of Employees Completed')