COBOL Report Writer lets you declare the shape of a report once and then simply GENERATElines. It prints page headings, details, footings, and control breaks for you.
Think of a bakery template for cake boxes. You design the template (REPORT SECTION), then every cake (record) is packaged identically by the machine (Report Writer).
12345678910111213141516171819202122DATA DIVISION. REPORT SECTION. RD SALES-RPT CONTROLS ARE CUST-ID PAGE LIMIT IS 60 LINES HEADING 1. 01 PAGE-HEADING TYPE PAGE HEADING. 05 FILLER PIC X(40) VALUE 'SALES REPORT'. 01 CUST-HEADING TYPE CONTROL HEADING CUST-ID. 05 FILLER PIC X(12) VALUE 'CUSTOMER: '. 05 CUST-ID-OUT PIC X(10). 01 DETAIL-LINE TYPE DETAIL. 05 ITEM PIC X(20). 05 QTY PIC 9(5). 05 AMT PIC 9(7)V99 SUM. 01 CUST-FOOT TYPE CONTROL FOOTING CUST-ID. 05 FILLER PIC X(12) VALUE 'CUST TOTAL:'. 05 CUST-TOTAL PIC 9(9)V99. 01 PAGE-FOOT TYPE PAGE FOOTING. 05 FILLER PIC X(10) VALUE 'Page:'. 05 PAGE-NO PIC 9(5).
RD defines the report. Groups (PAGE HEADING/FOOTING, CONTROL HEADING/FOOTING, DETAIL) describe printed lines.
12345678910111213141516PROCEDURE DIVISION. OPEN INPUT SALES-FILE OPEN OUTPUT REPORT-FILE INIT-REPORT. MOVE 0 TO PAGE-NO READ-LOOP. READ SALES-FILE AT END GO TO FINISH. MOVE CUST-ID-IN TO CUST-ID-OUT MOVE ITEM-IN TO ITEM MOVE QTY-IN TO QTY MOVE AMT-IN TO AMT GENERATE SALES-RPT GO TO READ-LOOP. FINISH. CLOSE SALES-FILE REPORT-FILE STOP RUN.
GENERATE triggers the next logical group based on control breaks and page limits.
Accumulate AMT with SUM, print in CONTROL FOOTING when CUST-ID changes.
Mistake | Problem | Fix |
---|---|---|
Manual totals everywhere | Duplicated logic | Use SUM and control break groups |
Ignoring page size | Truncated lines | Match PAGE LIMIT to device |
Feature | Usage | Example |
---|---|---|
Report definition | REPORT SECTION / RD | RD SALES-RPT ... |
Generate output | GENERATE report-name | GENERATE SALES-RPT |
Control breaks | CONTROLS ARE field | CONTROLS ARE CUST-ID |
1. What COBOL division contains Report Writer definitions?
2. Which statement produces output for a report?
3. Which groups commonly exist in a report?
4. How are control breaks handled?
5. Which counters are frequently used alongside Report Writer?