MainframeMaster

COBOL Tutorial

COBOL SELECT Statement - Quick Reference

Progress0 of 0 lessons

Overview

The SELECT statement is used to define file connections in COBOL. It establishes the relationship between logical file names used in the program and physical file locations or devices.

Purpose and Usage

  • File connection - Connect logical names to physical files
  • Device assignment - Assign files to specific devices
  • Environment setup - Configure file environment
  • Portability - Enable program portability across systems
  • File organization - Define file organization and access methods

Syntax

The SELECT statement is used in the FILE-CONTROL paragraph of the ENVIRONMENT DIVISION.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
* Basic SELECT syntax ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT logical-file-name ASSIGN TO physical-file-name. * Complete example IDENTIFICATION DIVISION. PROGRAM-ID. SELECT-EXAMPLE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INPUT-FILE ASSIGN TO 'INPUT.DAT' ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS WS-FILE-STATUS. SELECT OUTPUT-FILE ASSIGN TO 'OUTPUT.DAT' ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS WS-OUTPUT-STATUS. DATA DIVISION. FILE SECTION. FD INPUT-FILE. 01 INPUT-RECORD. 05 INPUT-DATA PIC X(80). FD OUTPUT-FILE. 01 OUTPUT-RECORD. 05 OUTPUT-DATA PIC X(80). WORKING-STORAGE SECTION. 01 WS-FILE-STATUS PIC XX. 01 WS-OUTPUT-STATUS PIC XX. PROCEDURE DIVISION. MAIN-LOGIC. OPEN INPUT INPUT-FILE OUTPUT OUTPUT-FILE READ INPUT-FILE WRITE OUTPUT-RECORD FROM INPUT-RECORD CLOSE INPUT-FILE OUTPUT-FILE STOP RUN.

SELECT establishes file connections in the ENVIRONMENT DIVISION.

Practical Examples

Examples of using the SELECT statement in different scenarios.

Sequential File Selection

cobol
1
2
3
4
5
6
* Sequential file selection SELECT SEQUENTIAL-FILE ASSIGN TO 'DATA/SEQUENTIAL.DAT' ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS WS-FILE-STATUS.

SELECT for sequential file processing.

Indexed File Selection

cobol
1
2
3
4
5
6
7
* Indexed file selection SELECT INDEXED-FILE ASSIGN TO 'DATA/INDEXED.IDX' ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS INDEXED-KEY FILE STATUS IS WS-INDEXED-STATUS.

SELECT for indexed file with random access.

Printer File Selection

cobol
1
2
3
4
5
6
* Printer file selection SELECT PRINT-FILE ASSIGN TO PRINTER ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS WS-PRINT-STATUS.

SELECT for printer output.

Best Practices

Understanding best practices ensures effective use of the SELECT statement.

Best Practices

  • Use meaningful names - Choose descriptive logical file names
  • Include FILE STATUS - Always include FILE STATUS for error handling
  • Specify organization - Explicitly specify file organization
  • Consider portability - Use relative paths when possible
  • Document assignments - Document physical file assignments

Test Your Knowledge

1. What is the primary purpose of the SELECT statement in COBOL?

  • To select data from files
  • To define file connections and assign files to external devices
  • To select program logic
  • To select display options

2. In which COBOL division is the SELECT statement used?

  • IDENTIFICATION DIVISION
  • ENVIRONMENT DIVISION
  • DATA DIVISION
  • PROCEDURE DIVISION

3. What does the ASSIGN clause in a SELECT statement do?

  • Assigns values to variables
  • Assigns the logical file name to a physical file location
  • Assigns program control
  • Assigns memory locations

4. Can a SELECT statement be used for both input and output files?

  • Yes, SELECT can be used for any file type
  • No, only for input files
  • No, only for output files
  • Only for temporary files

5. What is the relationship between SELECT and FD statements?

  • They are the same thing
  • SELECT defines the file connection, FD defines the file structure
  • SELECT is optional, FD is required
  • They cannot be used together

Frequently Asked Questions