Menu

COBOL - Delete Statement Example


O exemplo a seguir mostra como excluir um registro existente no arquivo.

Entrada:

1111 ABDUL       01

Codificação COBOL:

IDENTIFICATION DIVISION.
PROGRAM-ID. COBLDELT.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT STUDENT ASSIGN TO STD-FILE
    ORGANIZATION IS INDEXED
    ACCESS IS RANDOM
    RECORD KEY IS STUDENT-ID
    FILE STATUS IS FS.

DATA DIVISION.
FILE SECTION.
FD STUDENT
01 STUDENT-FILE.
   05 STUDENT-ID PIC 9(4).
   05 NAME PIC A(12).
   05 RANK PIC X(3).

WORKING-STORAGE SECTION.
01 FILE STATUS  PIC X(02) VALUE SPACES.

PROCEDURE DIVISION.
   OPEN I-O STUDENT.
   MOVE '1111' TO STUDENT-ID.

   DELETE STUDENT RECORD
      INVALID KEY DISPLAY 'Invalid Key'
      NOT INVALID KEY DISPLAY 'Record Deleted'
   END-DELETE.

   CLOSE STUDENT.
   STOP RUN.

Saída: arquivo vazio.