MainframeMaster

COBOL Tutorial

COBOL GET Clause - Quick Reference

Progress0 of 0 lessons

Overview

The GET clause is used in COBOL to retrieve data from various sources including files, databases, external systems, and other data repositories. It provides a standardized way to access and retrieve information in COBOL programs.

Purpose and Usage

  • Data retrieval - Retrieve data from various sources
  • File operations - Read records from files
  • Database access - Retrieve data from databases
  • External systems - Access data from APIs and external systems
  • Standardized interface - Consistent data access method

Data Retrieval Flow

Source: [File] [Database] [API] [External System]
GET Operation: [Specify Source] → [Retrieve Data] → [Handle Result]
Error Handling: [INVALID KEY] [ON EXCEPTION] [Status Check]
GET provides unified data access interface

GET operations follow a consistent pattern regardless of data source.

Syntax

The GET clause follows specific syntax patterns for different types of data retrieval.

Basic Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
* Basic GET clause syntax GET file-name [INVALID KEY imperative-statement] [NOT INVALID KEY imperative-statement] [END-GET] GET data-name FROM source [ON EXCEPTION imperative-statement] [NOT ON EXCEPTION imperative-statement] [END-GET] * Examples GET CUSTOMER-FILE INVALID KEY DISPLAY 'Error reading file' STOP RUN END-GET GET CUSTOMER-DATA FROM DATABASE-TABLE ON EXCEPTION DISPLAY 'Database error' PERFORM ERROR-HANDLING NOT ON EXCEPTION DISPLAY 'Data retrieved successfully' END-GET * Complete example GET API-RESPONSE FROM WEB-SERVICE ON EXCEPTION MOVE 'ERROR' TO STATUS-CODE PERFORM LOG-ERROR NOT ON EXCEPTION MOVE 'SUCCESS' TO STATUS-CODE PERFORM PROCESS-RESPONSE END-GET

GET can be used for file operations and external data retrieval.

Error Handling Options

ClausePurposeUse Case
INVALID KEYHandle file access errorsFile operations
ON EXCEPTIONHandle general exceptionsExternal systems
NOT INVALID KEYHandle successful file operationsFile operations
NOT ON EXCEPTIONHandle successful external operationsExternal systems

Data Source Types

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
* Different data source types for GET * File operations GET CUSTOMER-FILE INVALID KEY PERFORM ERROR-HANDLING END-GET * Database operations GET CUSTOMER-RECORD FROM CUSTOMER-TABLE WHERE CUSTOMER-ID = 'CUST001' ON EXCEPTION PERFORM DB-ERROR-HANDLING END-GET * API operations GET API-RESPONSE FROM REST-API USING 'https://api.example.com/data' ON EXCEPTION PERFORM API-ERROR-HANDLING END-GET * External system operations GET SYSTEM-DATA FROM LEGACY-SYSTEM USING CONNECTION-PARAMETERS ON EXCEPTION PERFORM SYSTEM-ERROR-HANDLING END-GET

GET supports various data source types with appropriate error handling.

Common Use Cases

GET is commonly used in specific scenarios where data retrieval from various sources is needed.

File Record Retrieval

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
* Retrieve records from sequential, indexed, or relative files IDENTIFICATION DIVISION. PROGRAM-ID. FILE-READER. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO 'CUSTOMER.DAT' ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC X(10). 05 CUSTOMER-NAME PIC X(50). 05 CUSTOMER-ADDRESS PIC X(100). PROCEDURE DIVISION. OPEN INPUT CUSTOMER-FILE PERFORM UNTIL EOF GET CUSTOMER-FILE INVALID KEY DISPLAY 'Error reading file' STOP RUN END-GET DISPLAY 'Customer: ' CUSTOMER-NAME END-PERFORM CLOSE CUSTOMER-FILE STOP RUN.

GET is used to retrieve records from various file types.

Database Record Access

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
* Retrieve records from database tables IDENTIFICATION DIVISION. PROGRAM-ID. DB-READER. DATA DIVISION. WORKING-STORAGE SECTION. 01 DB-CONNECTION. 05 DB-NAME PIC X(20) VALUE 'CUSTOMERDB'. 05 DB-USER PIC X(20) VALUE 'ADMIN'. 05 DB-PASSWORD PIC X(20) VALUE 'SECRET'. 01 CUSTOMER-DATA. 05 CUST-ID PIC X(10). 05 CUST-NAME PIC X(50). 05 CUST-BALANCE PIC 9(8)V99. PROCEDURE DIVISION. CONNECT TO DATABASE DB-CONNECTION GET CUSTOMER-DATA FROM CUSTOMER-TABLE WHERE CUST-ID = 'CUST001' ON EXCEPTION DISPLAY 'Database error occurred' PERFORM ERROR-HANDLING NOT ON EXCEPTION DISPLAY 'Customer: ' CUST-NAME DISPLAY 'Balance: ' CUST-BALANCE END-GET DISCONNECT FROM DATABASE STOP RUN.

GET enables database record retrieval with error handling.

External System Data Retrieval

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
* Retrieve data from external systems, APIs, or web services IDENTIFICATION DIVISION. PROGRAM-ID. API-READER. DATA DIVISION. WORKING-STORAGE SECTION. 01 API-ENDPOINT. 05 API-URL PIC X(100) VALUE 'https://api.example.com/data'. 05 API-METHOD PIC X(10) VALUE 'GET'. 05 API-HEADERS PIC X(200). 01 RESPONSE-DATA. 05 STATUS-CODE PIC 9(3). 05 RESPONSE-BODY PIC X(1000). PROCEDURE DIVISION. SET API-HEADERS TO 'Content-Type: application/json' GET RESPONSE-DATA FROM API-ENDPOINT ON EXCEPTION DISPLAY 'API call failed' PERFORM ERROR-HANDLING NOT ON EXCEPTION IF STATUS-CODE = 200 DISPLAY 'Data retrieved successfully' PERFORM PROCESS-RESPONSE ELSE DISPLAY 'API returned error: ' STATUS-CODE END-IF END-GET STOP RUN.

GET provides access to external systems and APIs.

Best Practices and Tips

Following these best practices ensures effective use of the GET clause for data retrieval operations.

GET Design Principles

  • Always handle errors - Use INVALID KEY or ON EXCEPTION clauses
  • Validate retrieved data - Check data integrity after retrieval
  • Close connections properly - Ensure proper cleanup of resources
  • Use meaningful variable names - Clear naming for better readability
  • Consider performance implications - Optimize for large datasets
  • Implement proper logging - Log operations for debugging

Common Pitfalls to Avoid

PitfallProblemSolution
No error handlingProgram crashes on errorsAlways use INVALID KEY or ON EXCEPTION
Unclosed connectionsResource leaks and system issuesProperly close all connections
No data validationIncorrect data processingValidate retrieved data
Poor performanceSlow data retrievalOptimize queries and use indexing
No loggingDifficult debuggingImplement comprehensive logging

Performance Considerations

  • Use indexed access - For large files to improve retrieval speed
  • Consider buffering strategies - For frequently accessed data
  • Minimize network calls - When accessing external systems
  • Use appropriate data types - To reduce memory usage
  • Consider caching mechanisms - For repeated data access
  • Monitor and optimize - Database query performance

When to Use GET

Use CaseGET SuitabilityReasoning
File data retrievalExcellentNatural fit for file operations
Database accessExcellentEfficient for database operations
External system accessGoodProvides standardized interface
Simple data accessGoodStraightforward for basic operations
Complex data processingPoorUse specialized processing tools

GET Clause Quick Reference

UsageSyntaxExample
File retrievalGET file-name [INVALID KEY]GET CUSTOMER-FILE INVALID KEY PERFORM ERROR
Database retrievalGET data-name FROM sourceGET CUSTOMER-DATA FROM CUSTOMER-TABLE
External systemGET data-name FROM source [ON EXCEPTION]GET API-RESPONSE FROM WEB-SERVICE ON EXCEPTION
Error handling[INVALID KEY] or [ON EXCEPTION]INVALID KEY PERFORM ERROR-HANDLING
Success handling[NOT INVALID KEY] or [NOT ON EXCEPTION]NOT ON EXCEPTION PERFORM SUCCESS-HANDLING

Test Your Knowledge

1. What is the primary purpose of the GET clause in COBOL?

  • To write data to files
  • To retrieve data from various sources
  • To perform calculations
  • To control program flow

2. Which clause is used to handle file access errors in GET operations?

  • ON EXCEPTION
  • INVALID KEY
  • ERROR HANDLING
  • EXCEPTION HANDLER

3. What is the correct syntax for retrieving data from an external source?

  • GET data-name FROM source
  • RETRIEVE data-name FROM source
  • READ data-name FROM source
  • FETCH data-name FROM source

4. Which clause handles exceptions when retrieving data from external sources?

  • INVALID KEY
  • ON EXCEPTION
  • ERROR HANDLING
  • EXCEPTION HANDLER

5. What is the relationship between GET and READ in COBOL?

  • They are the same thing
  • GET is more general-purpose than READ
  • READ is more general-purpose than GET
  • They cannot be used together

Frequently Asked Questions