删除特定记录键后,从以下所有记录键中减去 1
Subtract 1 from all the following record key after deleting a specific one
我想删除一个特定的项目,然后从以下所有项目 ID 中减去 1,这样它看起来像这样:
item-id item-name qty price
[01] Item 1 10 99
[02] Item 2 10 99
[03] Item 3 10 99
[04] Item 4 33 23
[05] Item 5 22 33
-删除 item-id 03
新输出:
item-id item-name qty price
[01] Item 1 10 99
[02] Item 2 10 99
[03] Item 4 33 23
[04] Item 5 22 33
我试过这样的操作,删除工作正常我只是不知道如何从以下所有项目 ID 中减去 1
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INVENTORY
ASSIGN TO 'C:\Users\User\Desktop\FINALS\inventory.dat'
ORGANIZATION IS INDEXED
ACCESS MODE IS RANDOM
RECORD KEY IS ITEM-ID.
FILE SECTION.
FD INVENTORY.
01 FILE-INVENTORY.
02 ITEM-ID PIC 9(2).
02 ITEM-NAME PIC X(25).
02 QTY PIC Z99.
02 PRICE PIC ZZZ9.00.
WORKING-STORAGE SECTION.
01 WS-INVENTORY.
02 WS-ID PIC 9(2).
02 WS-GA PIC X(25).
02 WS-QTY PIC 999.
02 WS-PRC PIC 9999.00.
01 WS-EOF PIC A(4).
PROCEDURE DIVISION.
DEL-ITEM.
OPEN I-O INVENTORY
DISPLAY " ENTER ID OF THE PRODUCT YOU WANT TO DELETE"
DISPLAY " ITEM ID: " WITH NO ADVANCING
ACCEPT ITEM-ID
MOVE ITEM-ID TO WS-ID
DELETE INVENTORY
INVALID KEY
DISPLAY "ITEM DOES NOT EXIST"
END-DELETE
MOVE WS-ID TO ITEM-ID
READ INVENTORY NEXT INTO WS-INVENTORY
NOT AT END PERFORM ID-CHECK UNTIL WS-EOF = 'TRUE'
AT END MOVE 'TRUE' TO WS-EOF
CLOSE INVENTORY
ID-CHECK.
SUBTRACT 1 FROM WS-ID GIVING ITEM-ID REWRITE FILE-INVENTORY.
当使用DELETE
删除记录时,DELETE
语句之前必须有一个成功的READ
语句。例如,
READ INVENTORY KEY ITEM-ID
INVALID KEY
SET some-error-condition TO TRUE
NOT INVALID KEY
DELETE INVENTORY
more tests for errors
END-DELETE
END-READ
删除记录成功后,需要循环修改剩余记录的prime record key。
注意更改prime record key时,不能使用REWRITE
。对于 ACCESS MODE IS RANDOM
,使用类似下面的内容作为更完整的示例,
01 WS-DELETE-KEY PIC 99.
01 WS-DELETE-STATUS PIC 9 VALUE 0.
88 RECORD-NOT-FOUND VALUE 1.
88 RECORD-DELETED VALUE 2.
88 DELETE-FAILED VALUE 3.
88 NO-MORE-RECORDS VALUE 4.
ACCEPT WS-DELETE-KEY
PERFORM DELETE-RECORD
IF RECORD-DELETED
ADD 1 TO WS-DELETE-KEY
PERFORM CHANGE-PRIMARY-KEY *> for the remaining records
VARYING WS-DELETE-KEY FROM WS-DELETE-KEY BY 1
UNTIL NO-MORE-RECORDS
END-PERFORM
END-IF
.
DELETE-RECORD.
MOVE WS-DELETE-KEY TO ITEM-ID
READ INVENTORY KEY ITEM-ID
INVALID KEY
SET RECORD-NOT-FOUND TO TRUE
NOT INVALID KEY
DELETE INVENTORY
INVALID KEY
SET DELETE-FAILED TO TRUE
NOT INVALID KEY
SET RECORD-DELETED TO TRUE
END-DELETE
END-READ
.
CHANGE-PRIMARY-KEY.
MOVE WS-DELETE-KEY TO ITEM-ID
READ INVENTORY KEY ITEM-ID
INVALID KEY
SET NO-MORE-RECORDS TO TRUE
NOT INVALID KEY
DELETE INVENTORY
SUBTRACT 1 FROM ITEM-ID
WRITE FILE-INVENTORY
END-READ
这假设所有剩余的记录都按顺序编号,并且读取记录失败意味着没有更多的记录。
在 CHANGE-PRIMARY-KEY
中为 DELETE
和 WRITE
语句之后的其他错误添加额外的检查可能是明智的。
对于 ACCESS MODE IS DYNAMIC
,代码类似,但可以使用 START
和 READ ... NEXT
语句。
我想删除一个特定的项目,然后从以下所有项目 ID 中减去 1,这样它看起来像这样:
item-id item-name qty price
[01] Item 1 10 99
[02] Item 2 10 99
[03] Item 3 10 99
[04] Item 4 33 23
[05] Item 5 22 33
-删除 item-id 03 新输出:
item-id item-name qty price
[01] Item 1 10 99
[02] Item 2 10 99
[03] Item 4 33 23
[04] Item 5 22 33
我试过这样的操作,删除工作正常我只是不知道如何从以下所有项目 ID 中减去 1
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INVENTORY
ASSIGN TO 'C:\Users\User\Desktop\FINALS\inventory.dat'
ORGANIZATION IS INDEXED
ACCESS MODE IS RANDOM
RECORD KEY IS ITEM-ID.
FILE SECTION.
FD INVENTORY.
01 FILE-INVENTORY.
02 ITEM-ID PIC 9(2).
02 ITEM-NAME PIC X(25).
02 QTY PIC Z99.
02 PRICE PIC ZZZ9.00.
WORKING-STORAGE SECTION.
01 WS-INVENTORY.
02 WS-ID PIC 9(2).
02 WS-GA PIC X(25).
02 WS-QTY PIC 999.
02 WS-PRC PIC 9999.00.
01 WS-EOF PIC A(4).
PROCEDURE DIVISION.
DEL-ITEM.
OPEN I-O INVENTORY
DISPLAY " ENTER ID OF THE PRODUCT YOU WANT TO DELETE"
DISPLAY " ITEM ID: " WITH NO ADVANCING
ACCEPT ITEM-ID
MOVE ITEM-ID TO WS-ID
DELETE INVENTORY
INVALID KEY
DISPLAY "ITEM DOES NOT EXIST"
END-DELETE
MOVE WS-ID TO ITEM-ID
READ INVENTORY NEXT INTO WS-INVENTORY
NOT AT END PERFORM ID-CHECK UNTIL WS-EOF = 'TRUE'
AT END MOVE 'TRUE' TO WS-EOF
CLOSE INVENTORY
ID-CHECK.
SUBTRACT 1 FROM WS-ID GIVING ITEM-ID REWRITE FILE-INVENTORY.
当使用DELETE
删除记录时,DELETE
语句之前必须有一个成功的READ
语句。例如,
READ INVENTORY KEY ITEM-ID
INVALID KEY
SET some-error-condition TO TRUE
NOT INVALID KEY
DELETE INVENTORY
more tests for errors
END-DELETE
END-READ
删除记录成功后,需要循环修改剩余记录的prime record key。
注意更改prime record key时,不能使用REWRITE
。对于 ACCESS MODE IS RANDOM
,使用类似下面的内容作为更完整的示例,
01 WS-DELETE-KEY PIC 99.
01 WS-DELETE-STATUS PIC 9 VALUE 0.
88 RECORD-NOT-FOUND VALUE 1.
88 RECORD-DELETED VALUE 2.
88 DELETE-FAILED VALUE 3.
88 NO-MORE-RECORDS VALUE 4.
ACCEPT WS-DELETE-KEY
PERFORM DELETE-RECORD
IF RECORD-DELETED
ADD 1 TO WS-DELETE-KEY
PERFORM CHANGE-PRIMARY-KEY *> for the remaining records
VARYING WS-DELETE-KEY FROM WS-DELETE-KEY BY 1
UNTIL NO-MORE-RECORDS
END-PERFORM
END-IF
.
DELETE-RECORD.
MOVE WS-DELETE-KEY TO ITEM-ID
READ INVENTORY KEY ITEM-ID
INVALID KEY
SET RECORD-NOT-FOUND TO TRUE
NOT INVALID KEY
DELETE INVENTORY
INVALID KEY
SET DELETE-FAILED TO TRUE
NOT INVALID KEY
SET RECORD-DELETED TO TRUE
END-DELETE
END-READ
.
CHANGE-PRIMARY-KEY.
MOVE WS-DELETE-KEY TO ITEM-ID
READ INVENTORY KEY ITEM-ID
INVALID KEY
SET NO-MORE-RECORDS TO TRUE
NOT INVALID KEY
DELETE INVENTORY
SUBTRACT 1 FROM ITEM-ID
WRITE FILE-INVENTORY
END-READ
这假设所有剩余的记录都按顺序编号,并且读取记录失败意味着没有更多的记录。
在 CHANGE-PRIMARY-KEY
中为 DELETE
和 WRITE
语句之后的其他错误添加额外的检查可能是明智的。
对于 ACCESS MODE IS DYNAMIC
,代码类似,但可以使用 START
和 READ ... NEXT
语句。