Random update - Bristol Community College

Download Report

Transcript Random update - Bristol Community College

Random update
Please use speaker notes for
additional information!
Update
Random Update
• EDIT: The transactions that will be used in the update must be as error free as possible to
prevent corrupting the master file. To assure the integrity of the data, the transactions can
be processed in an edit program and only valid transactions should be allowed to update the
master file. The output of the edit file includes the valid (good) transaction file which will
be processed in the next step. Frequently transaction come in via a screen and the analyst
decides that the editing will be incorporated in the update program to provide interactivity.
• UPDATE: The update program uses the valid transaction file to update the master file
itself. No new master file is created, the changes are made to the existing master file.
Reports become extremely important to provide a paper trail of records processed and also
a trail of records that contained errors and were not processed. In a maintenance update
there can be add, change or delete transactions so records can be added to the existing
master, changed on the existing master, or deleted from the existing master. The method of
physically handling deletes varies depending on the version of COBOL being used.. The
update program completes the updating maintenance cycle. Note that back up does not
happen automatically in a random update. Copies of the master file and the transaction file
need to be made. Frequently the master is backed up directly after the update. It
absolutely has to be backed up before the next update.
Systems flowchart
Data
Edit
Program
Errors can be put
on disk, printed,
interactively
corrected or any
combination of
solutions.
Valid
Trans
Update
Program
Errors
Master
File
Trail
Note: Errors can be written to disk, printed,
interactively corrected or any combination of
solutions. An optional paper trail can also be
produced on disk, paper or another medium.
Random Update
Screen
Transactions
OR
Transactions
If transactions are coming in
as a group from a disk file,
they should be edited. If the
transactions are coming from
a screen, interactive editing
should take place in the
Random Update Program.
The user will frequently try to
interactively handle errors on
the screen.
Random Update
Program
Errors
Trail
Master
Random Update Logic
Record on the
indexed file
Record not on the
indexed file
Trans = A
(Add)
Error condition
WRITE the record
on the indexed file new record
Trans = C
(Change)
Make changes in
memory
REWRITE the
record on the
indexed file
Error condition
Trans = D
(Delete)
DELETE the record Error condtion
from the indexed
file
Random Update
ERROR /TRAIL
MASTER (before)
TRANSACTIONS
MASTER (after)
121...
121…
C
121...
123…
124…
A
123…
222...
222…
C
222...
234…
222…
C
234…
333...
333…
D
333...
345…
350…
C
345…
444...
444…
A
444...
456…
450…
D
456…
512…
999...
512…
The transaction is read
and then there is an attempt made to find the matching record
identification number on the Master. If a match is found,
changes are made to the record on the master and the record is put
back on the master with a REWRITE.
A trail of the change is also made.
REPORT
121 (trail)
Random Update
MASTER (after)
ERROR /TRAIL
MASTER (before)
TRANSACTIONS
121...
121…
C
121...
121 (trail)
123…
124…
A
123…
124 (trail)
222...
222…
C
124...
234…
222…
C
222...
333...
333…
D
234…
345…
350…
C
333...
444...
444…
A
345…
456…
450…
D
444...
512…
999...
456…
512…
The transaction is read
and then there is an attempt made to find the matching record
identification number on the Master. Since no match is found and this
is an add,
the new record is written on the Master using a WRITE.
A trail of the add is also made.
REPORT
Random Update
ERROR /TRAIL
MASTER (before)
TRANSACTIONS
MASTER (after)
121...
121…
C
121...
121 (trail)
123…
124…
A
123…
124 (trail)
222...
222…
C
124...
222 (trail)
234…
222…
C
222...
333...
333…
D
234…
345…
350…
C
333...
444...
444…
A
345…
456…
450…
D
444...
512…
999...
REPORT
456…
512…
The transaction is read
and then there is an attempt made to find the matching record
identification number on the Master. Since a match is found and this is
an change,
the change is made and the changed Master replaces the original
record on the master using the REWRITE.
A trail of the change is also made.
Random Update
ERROR /TRAIL
MASTER (before)
TRANSACTIONS
MASTER (after)
121...
121…
C
121...
121 (trail)
123…
124…
A
123…
124 (trail)
222...
222…
C
124...
222 (trail)
234…
222…
C
222...
222 (trail)
333...
333…
D
234…
345…
350…
C
333...
444...
444…
A
345…
456…
450…
D
444...
512…
999...
REPORT
456…
512…
The transaction is read
and then there is an attempt made to find the matching record
identification number on the Master. Since a match is found and this is
an change,
the change is made and the changed Master replaces the original
record on the master using the REWRITE.
A trail of the change is also made.
Random Update
ERROR /TRAIL
MASTER (before)
TRANSACTIONS
MASTER (after)
121...
121…
C
121...
121 (trail)
123…
124…
A
123…
124 (trail)
222...
222…
C
124...
222 (trail)
234…
222…
C
222...
222 (trail)
333...
333…
D
234…
333 (trail)
345…
350…
C
345…
444...
444…
A
444...
456…
450…
D
456…
512…
999...
REPORT
512…
The transaction is read
and then there is an attempt made to find the matching record
identification number on the Master. Since a match is found and this is
an delete,
the delete is made by removing the record from the Master using the
DELETE verb.
A trail of the delete is also made.
Random Update
MASTER (after)
ERROR /TRAIL
MASTER (before)
TRANSACTIONS
121...
121…
C
121...
121 (trail)
123…
124…
A
123…
124 (trail)
222...
222…
C
124...
222 (trail)
234…
222…
C
222...
222 (trail)
333...
333…
D
234…
333 (trail)
345…
350…
C
345…
350 (error)
444...
444…
A
444...
456…
450…
D
456…
512…
999...
512…
The transaction is read
and then there is an attempt made to find the matching record
identification number on the Master. Since no match is found and this
is a change,
no change is made to the master.
A report of the change error is also made.
REPORT
Random Update
ERROR /TRAIL
MASTER (before)
TRANSACTIONS
121...
121…
C
121...
121 (trail)
123…
124…
A
123…
124 (trail)
222...
222…
C
124...
222 (trail)
234…
222…
C
222...
222 (trail)
333...
333…
D
234…
333 (trail)
345…
350…
C
345…
350 (error)
444...
444…
A
444...
444 (error)
456…
450…
D
456…
512…
MASTER (after)
REPORT
512…
The transaction is read
and then there is an attempt made to find the matching record
identification number on the Master. Since a match is found and this is
a add,
no change is made to the master.
A report of the add error is also made.
Random Update
TRANSACTIONS
121...
121…
C
121...
121 (trail)
123…
124…
A
123…
124 (trail)
222...
222…
C
124...
222 (trail)
234…
222…
C
222...
222 (trail)
333...
333…
D
234…
333 (trail)
345…
350…
C
345…
350 (error)
444...
444…
A
444...
444 (error)
456…
450…
D
456…
450 (error)
512…
MASTER (after)
ERROR /TRAIL
MASTER (before)
512…
The transaction is read
and then there is an attempt made to find the matching record
identification number on the Master. Since no match is found and this
is a delete,
no change is made to the master.
A report of the delete error is also made.
REPORT
Indexed file
maintenance
ADD
WRITE record-name
INVALID KEY
…
NOT INVALID KEY
…
END-WRITE
When writing to an INDEXED file with RANDOM access, the
INVALID KEY clause will be triggered if you attempt to write a
duplicate record and the record will not be written. Therefore an
ADD can be made without prior checking, any trail of the
successful ADD can be put into the NOT INVALID KEY clause.
Indexed file
maintenance
CHANGE
REWRITE record-name
INVALID KEY
…
NOT INVALID KEY
…
END-WRITE
The REWRITE statement replaces an existing record on the file.
If you have made changes to a record in memory based on
information from a transaction, these changes would now be
written in place of the original record because of the REWRITE.
A rewrite can change everything in the record except for the
primary/record key field. An attempt to put the record back with
a different key will trigger the INVALID KEY clause.
Indexed file
maintenance
DELETE
DELETE file-name
INVALID KEY
…
NOT INVALID KEY
…
END-WRITE
The DELETE statement deletes a record from the file. In the
record does not exist, the INVALID KEY clause is triggered. IF
the DELETE is successful the NOT INVALID KEY clause can be
used. Note that a DELETE can be issue without a prior READ,
however then the DELETE will just happen without any chance
to view the record and make sure that it should be deleted. Many
programmers read, check the record be either displaying it for
user confirmation or checking to make sure that a second field
such as name on the transaction is the same as the name on the
record . This is a prudent thing to do to prevent deleting of a
record because the wrong identification number was keyed in.
myranupd.cbl
IDENTIFICATION DIVISION.
PROGRAM-ID. RANUPDT.
AUTHOR. GROCER.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
SELECT MASTER-FILE
ASSIGN TO "C:\PCOBWIN\VSAM\MSTRVSAM.DAT"
ORGANIZATION IS INDEXED
ACCESS IS RANDOM
RECORD KEY IS MID.
SELECT TRAN-FILE
ASSIGN TO "C:\PCOBWIN\VSAM\TRANSN.DAT".
SELECT PRINT-FILE ASSIGN TO PRINTER.
DATA DIVISION.
FILE SECTION.
FD MASTER-FILE
DATA RECORD IS MASTER-REC.
01 MASTER-REC.
05 MID
PIC 9(3).
05 FILLER
PIC X(33).
myranupd.cbl
FD
01
TID and TCODE are the
two fields used in
determining the
processing to be done.
TRAN-FILE
DATA RECORD IS TRAN-REC.
TRAN-REC.
05 TRAN-DATA.
10 TID
PIC 9(3).
10 TITEM-NAME.
15 FILLER
PIC X(19).
15 LAST-ITEM-NAME
PIC X.
10 TVENDOR-CODE.
15 FILLER
PIC XX.
15 LAST-VENDOR-CODE PIC X.
10 TCOST.
15 FILLER
PIC XXXX.
15 LAST-COST PIC X.
10 RDF-TCOST REDEFINES TCOST PIC 999V99.
10 TPRICE.
15 FILLER
PIC XXXX.
15 LAST-PRICE PIC X.
10 RDF-TPRICE REDEFINES TPRICE PIC 999V99.
05 TCODE
PIC X.
myranupd.cbl
FD
01
PRINT-FILE
DATA RECORD IS PRINTZ.
PRINTZ.
05 FILLER
PIC
05 ID-PR
PIC
05 FILLER
PIC
05 ITEM-NAME-PR
PIC
05 FILLER
PIC
05 VENDOR-CODE-PR PIC
05 FILLER
PIC
05 COST-PR
PIC
05 FILLER
PIC
05 PRICE-PR
PIC
05 FILLER
PIC
05 MSG-PR
PIC
05 FILLER
PIC
X.
9(3).
X(3).
X(20).
X(3).
XXX.
X(3).
$ZZZ.99.
X(3).
$ZZZ.99.
X(3).
X(22).
XX.
myranupd.cbl
This is the definition of the master
record in working storage. This is
the area where the data will be
manipulated.
WORKING-STORAGE SECTION.
01 MASTER-REC-WS.
05 ID-WS
PIC
05 ITEM-NAME-WS
PIC
05 VENDOR-CODE-WS PIC
05 COST-WS
PIC
05 PRICE-WS
PIC
9(3).
X(20).
XXX.
999V99.
999V99.
myranupd.cbl
Because the master file will
be read and written on, it
must be opened for both
input and output. That
means it must be opened as
an I-O file.
PROCEDURE DIVISION.
MAINLINE.
PERFORM A-100-INITIALIZE.
PERFORM B-100-PROCESS.
PERFORM C-100-WRAPUP.
Because processing is
STOP RUN.
transaction driven, the
A-100-INITIALIZE.
transaction is read and
OPEN I-O MASTER-FILE
then used to find the
INPUT TRAN-FILE
correct master or confirm
OUTPUT PRINT-FILE.
the absence of a master.
B-100-PROCESS.
PERFORM U-100-READ-TRAN-FILE.
PERFORM B-200-LOOP
UNTIL TID = 999.
Processing is transaction driven. That
means that when there are no more
transactions on the transaction file
processing will be terminated. The
read of the transaction file moves 999
to TID when end of file is reached.
myranupd.cbl
Establish the key by moving
B-200-LOOP.
MOVE SPACES TO PRINTZ. TID to MID and then read the
If the read is not successful,
master record to locate the
MOVE TID TO MID.
invalid key will be triggered and
record.
READ MASTER-FILE
B-300-NO-MSTR-THERE will be
INVALID KEY
processed. If the read is
PERFORM B-300-NO-MSTR-THERE
successful, not invalid key will be
NOT INVALID KEY
triggered and B-310-MSTRPERFORM B-310-MSTR-THERE.
THERE will be processed.
PERFORM U-100-READ-TRAN-FILE.
B-300-NO-MSTR-THERE.
Read the next
IF TCODE = "A"
If the read was unsuccessful
transaction - remember
PERFORM B-420-ADD-ROUT transactions control the
the only valid transaction is
ELSE
an ADD.
processing.
IF TCODE = "C"
PERFORM B-440-CHANGE-ERROR-ROUT
ELSE
It the read was successful,
PERFORM B-450-DELETE-ERROR-ROUT.
the CHANGE and the
B-310-MSTR-THERE.
DELETE are valid
MOVE MASTER-REC TO MASTER-REC-WS.
transactions. The ADD is
IF TCODE = "C"
an invalid transaction.
PERFORM B-400-CHANGE-ROUT
ELSE
IF TCODE = "D"
PERFORM B-410-DELETE-ROUT
ELSE
PERFORM B-430-ADD-ERROR-ROUT.
myranupd.cbl
B-400-CHANGE-ROUT.
MOVE SPACES TO PRINTZ.
MOVE ID-WS TO ID-PR.
MOVE ITEM-NAME-WS TO ITEM-NAME-PR.
MOVE VENDOR-CODE-WS TO VENDOR-CODE-PR.
MOVE COST-WS TO COST-PR.
MOVE PRICE-WS TO PRICE-PR.
MOVE "BEFORE CHANGES" TO MSG-PR.
WRITE PRINTZ
AFTER ADVANCING 2 LINES.
IF TITEM-NAME NOT = SPACES
IF LAST-ITEM-NAME = "-"
MOVE SPACES TO ITEM-NAME-WS, ITEM-NAME-PR
ELSE
MOVE TITEM-NAME TO ITEM-NAME-WS, ITEM-NAME-PR.
IF TVENDOR-CODE NOT = SPACES
IF LAST-VENDOR-CODE = "-"
MOVE SPACES TO VENDOR-CODE-WS, VENDOR-CODE-PR
ELSE
MOVE TVENDOR-CODE TO VENDOR-CODE-WS,
VENDOR-CODE-PR.
myranupd.cbl
Note that the REWRITE
is taking what is in
MASTER-REC-WS and
writing it out on
MASTER-REC. Notice
also that REWRITE uses
the record name.
IF TCOST = SPACES OR RDF-TCOST = 0
NEXT SENTENCE
ELSE
IF LAST-COST = "-"
MOVE 0 TO COST-WS, COST-PR
ELSE
MOVE RDF-TCOST TO COST-WS, COST-PR.
IF TPRICE = SPACES OR RDF-TPRICE = 0
NEXT SENTENCE
ELSE
IF LAST-PRICE = "-"
MOVE 0 TO PRICE-WS, PRICE-PR
ELSE
MOVE RDF-TPRICE TO PRICE-WS, PRICE-PR.
REWRITE MASTER-REC FROM MASTER-REC-WS
INVALID KEY
MOVE "CHANGES REJECTED" TO MSG-PR
NOT INVALID KEY
MOVE "AFTER CHANGES" TO MSG-PR.
WRITE PRINTZ
AFTER ADVANCING 1 LINES.
myranupd.cbl
The DELETE
writes the file (the
FD name).
B-410-DELETE-ROUT.
MOVE SPACES TO PRINTZ.
MOVE MID TO ID-PR.
MOVE ITEM-NAME-WS TO ITEM-NAME-PR.
MOVE VENDOR-CODE-WS TO VENDOR-CODE-PR.
MOVE COST-WS TO COST-PR.
MOVE PRICE-WS TO PRICE-PR.
DELETE MASTER-FILE
INVALID KEY
MOVE "DELETED REJECTED" TO MSG-PR
NOT INVALID KEY
MOVE "RECORD DELETED" TO MSG-PR.
WRITE PRINTZ
AFTER ADVANCING 2 LINES.
myranupd.cbl
The WRITE statement uses
the record name as always.
B-420-ADD-ROUT.
MOVE TRAN-DATA TO MASTER-REC.
MOVE TID TO ID-PR.
MOVE TITEM-NAME TO ITEM-NAME-PR.
MOVE TVENDOR-CODE TO VENDOR-CODE-PR.
MOVE RDF-TCOST TO COST-PR.
MOVE RDF-TPRICE TO PRICE-PR.
WRITE MASTER-REC
INVALID KEY
MOVE "ADD REJECTED" TO MSG-PR
NOT INVALID KEY
MOVE "RECORD ADDED" TO MSG-PR.
WRITE PRINTZ
AFTER ADVANCING 2 LINES.
myranupd.cbl
B-430-ADD-ERROR-ROUT.
MOVE SPACES TO PRINTZ.
MOVE TID TO ID-PR.
MOVE TITEM-NAME TO ITEM-NAME-PR.
MOVE TVENDOR-CODE TO VENDOR-CODE-PR.
MOVE RDF-TCOST TO COST-PR.
MOVE RDF-TPRICE TO PRICE-PR.
MOVE "**RECORD NOT ADDED**" TO MSG-PR.
WRITE PRINTZ
AFTER ADVANCING 2 LINES.
B-440-CHANGE-ERROR-ROUT.
MOVE SPACES TO PRINTZ.
MOVE TID TO ID-PR.
MOVE TITEM-NAME TO ITEM-NAME-PR.
MOVE TVENDOR-CODE TO VENDOR-CODE-PR.
MOVE RDF-TCOST TO COST-PR.
MOVE RDF-TPRICE TO PRICE-PR.
MOVE "**RECORD NOT CHANGED**" TO MSG-PR.
WRITE PRINTZ
AFTER ADVANCING 2 LINES.
B-450-DELETE-ERROR-ROUT.
MOVE SPACES TO PRINTZ.
MOVE TID TO ID-PR.
MOVE "**RECORD NOT DELETED**" TO MSG-PR.
WRITE PRINTZ
AFTER ADVANCING 2 LINES.
myranupd.cbl
C-100-WRAPUP.
CLOSE MASTER-FILE
TRAN-FILE
PRINT-FILE.
U-100-READ-TRAN-FILE.
READ TRAN-FILE
AT END
MOVE 999 TO TID.
When end of file is
reached on the
transaction file, 999 is
moved to TID. This
will terminate
processing.