SELECT
We like to draw your attention to the exiting
features from "SELECT". With SELECT you are able to
substantially reduce the number of lines of program code . The following routine
shows a 'classical' way to read a file 'sale' (invoices) using a secondary key
customer number'. With the invoice number, we then read the invoice items from a
file 'sale_itm'.
0010 rem TT1A
0020 begin
0030 open (1)"sale"
0040 open (2)"sale_item"
0050 input (0,err=0050)'CS',@(10,10),'SB',"Customer number ",'SF',CUST$
0060 gosub GET_INVOICE
0070 goto 0050
1000 GET_INVOICE: ! ^1000 --------------------------------------------
1010 let K$=CUST$
1020 extract (1,key=CUST$,kno=2,dom=1030)iol=1140; goto 1150
Put the file pointer right in front of the invoice for the selected customer.
Read with kno=2'
1100 READ_INVOICE: rem ^100
1110 let K$=key(1,end=1180)
1120 if K$(1,5)>CUST$ then goto 1180
Read until the end of file or until the last invoice for the selected customer
number..
1130 read (1)iol=1140
1140 iolist INVOICE$,DATE$,EXPDATE$,CUSTOMER$,TOTAL,NET,TAX,DISCOUNT
1150 print INVOICE$," ",DATE$," ",CUSTOMER$,TOTAL:"###B###B##0.00-"
1160 gosub GET_DETAIL
1170 goto READ_INVOICE
1180 return
2000 GET_DETAIL: rem ^1000 ------------------------------------------
2010 let D$=INVOICE$
2020 extract (2,key=INVOICE$,dom=2030)iol=2140; goto 2150
Put file pointer to the correct invoice.
2100 READ_DETAIL: rem ^100
2110 let D$=key(2,end=2170)
2120 if D$(1,10)>INVOICE$ then goto 2170
The necessary checks to read the correct invoice items.
2130 read (2)iol=2140
2140 iolist D_INVOICE$,LINE$,PRODUCT$,D_TOTAL,D_NET,D_TAX,D_DISCOUNT
TOTAL' etc. may have been used in an earlier IOLIST. Therefore, we have
to choose new VARIABLES.
2150 print @(20),PRODUCT$,D_TOTAL:"###B###B##0.00-"
2160 goto READ_DETAIL
2170 return
For the next routine, we will use "SELECT" and
the embedded iolist. You immediately notice that we have substantially reduced
the number of lines of code . A lot of code has become obsolete. No more
annoying IF..THEN' and GOTO'. No more need to OPEN files at the beginning.
0010 rem TT1B
0020 begin
0030 !
0040 !
0050 input (0,err=0050)'CS',@(10,10),'SB',"Customer number ",'SF',CUST$
0060 gosub GET_INVOICE
0070 goto 0050
1000 GET_INVOICE: ! ^1000 --------------------------------------------
1010 !
1020 !
1100 ! ^100
1110 !
1120 !
1130 select * from "sale",kno=2 begin CUST$ end CUST$+$FF$
Please note how we access the invoice file with the alternate key.
1140 !
1150 print INVOICE$," ",DATE$," ",CUSTOMER$,TOTAL:"###B###B##0.00-"
1160 gosub GET_DETAIL
1170 next record
1180 return
2000 GET_DETAIL: rem ^1000 ------------------------------------------
2010 !
2020 !
2100 ! ^100
2110 !
2120 !
2130 select *,rec=D$ from "sale_item" begin INVOICE$ end INVOICE$+$FF$
To avoid VARIABLES with the same names, we use the prefix: ',rec=D$'
2140 !
2150 print @(20),D.PRODUCT$,D.TOTAL:"###B###B##0.00-"
2160 next record
2170 return
Other ways to use SELECT are:
SELECT A$,B$,C$ FROM "filename"
Use VARIABLES instead of 'embedded' IOLIST
SELECT iol=500 FROM "filename"
Use an IOLIST
SELECT * FROM 1
Use a file, already open on channel 1
SELECT * FROM "company" WHERE ( lang$="1" and ZIP$>"B")
Read records selectively.
Data fields without a key are selected with WHERE
X$="lang$="+quo+"1"+quo
SELECT * FROM "company" WHERE EVN(X$)
Read records selectively. Use EVN() to select.
SELECT ... NEXT RECORD routines can be interrupted with 'EXITTO'
Example:
In order to delete a customer from the file, we first need to check whether there
are related records in this file. In the event of only one such record the routine will
be interrupted.
SELECT * FROM "sale",kno=2 BEGIN cust$ END cust$+$FF$
PRINT "Do not delete this customer"
EXITTO DO_NOT_DELETE
NEXT RECORD
OKAY_TO_DELETE:
K$=KEC(LFO) returns the key just read with SELECT.
|