Menu

COBOL - Exemplo de declaração de reescrita


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

Entrada:

1111 ABDUL       01

Codificação COBOL:

IDENTIFICATION DIVISION.
PROGRAM-ID. COBRWRTE.

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 CLASS 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.

   READ STUDENT
      KEY IS STUDENT-ID
      INVALID KEY DISPLAY ‘KEY IS NOT EXISTING’
   END-READ.

   MOVE 'ABDUL KALAM' TO NAME.
   REWRITE STUDENT-FILE
   END-REWRITE.
   CLOSE STUDENT.
   STOP RUN.

Resultado:

1111 ABDUL KALAM 01