Progress0 of 0 lessons

CICS WRITE - Record Writing

CICS WRITE provides record writing capabilities in CICS environments. It enables programs to write records, manage file operations, and handle record writing for data storage and file management purposes.

Command Syntax

cobol
1
2
3
4
5
6
7
8
9
EXEC CICS WRITE [FILE(file-name)] [FROM(data-area)] [LENGTH(data-length)] [RIDFLD(record-id)] [KEYLENGTH(key-length)] [RESP(response-code)] [RESP2(response-code-2)] END-EXEC.

Parameters

FILE(file-name)

Specifies the name of the file to which the record will be written. This parameter identifies the target file for write operations.

FROM(data-area)

Specifies the data area containing the record data to be written. This parameter provides the source data for write operations.

LENGTH(data-length)

Specifies the length of the record data to be written. This parameter controls the amount of data written to the file.

RIDFLD(record-id)

Specifies the record identifier field containing the key of the record to be written. This parameter identifies the record for write operations.

KEYLENGTH(key-length)

Specifies the length of the key field used to identify the record. This parameter controls the key matching for record identification.

RESP(response-code)

Specifies the response code variable to receive the operation result. This parameter provides error handling and status information.

RESP2(response-code-2)

Specifies the secondary response code variable for additional error information. This parameter provides extended error details when available.

Writing Types

New Record Writing

WRITE creates new records in files, enabling programs to add new data to existing files for data expansion and growth.

Key-Based Writing

The command supports key-based writing for creating records with specific key values and maintaining file organization.

Sequential Writing

Sequential writing enables creation of records in key order for maintaining sorted file structures and efficient data access.

Batch Writing

Batch writing supports creation of multiple records efficiently in data loading and bulk insert operations.

Programming Examples

Basic Record Writing

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
WORKING-STORAGE SECTION. 01 FILE-NAME PIC X(8) VALUE 'CUSTOMER'. 01 RECORD-DATA PIC X(100) VALUE 'Customer data here'. 01 DATA-LENGTH PIC S9(8) COMP VALUE 100. 01 RECORD-KEY PIC X(10) VALUE 'CUST001'. 01 KEY-LENGTH PIC S9(8) COMP VALUE 10. 01 RESPONSE-CODE PIC S9(8) COMP. 01 RESPONSE-CODE-2 PIC S9(8) COMP. PROCEDURE DIVISION. EXEC CICS WRITE FILE(FILE-NAME) FROM(RECORD-DATA) LENGTH(DATA-LENGTH) RIDFLD(RECORD-KEY) KEYLENGTH(KEY-LENGTH) RESP(RESPONSE-CODE) RESP2(RESPONSE-CODE-2) END-EXEC IF RESPONSE-CODE = 0 DISPLAY 'Record written successfully' DISPLAY 'File: ' FILE-NAME DISPLAY 'Key: ' RECORD-KEY ELSE DISPLAY 'Error writing record: ' RESPONSE-CODE END-IF.

Multiple Record Writing

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
WORKING-STORAGE SECTION. 01 FILE-NAME PIC X(8) VALUE 'CUSTOMER'. 01 RECORD-DATA PIC X(100). 01 DATA-LENGTH PIC S9(8) COMP VALUE 100. 01 RECORD-KEY PIC X(10). 01 KEY-LENGTH PIC S9(8) COMP VALUE 10. 01 RESPONSE-CODE PIC S9(8) COMP. 01 RESPONSE-CODE-2 PIC S9(8) COMP. 01 WRITE-COUNT PIC S9(8) COMP VALUE 0. PROCEDURE DIVISION. PERFORM WRITE-CUSTOMER-001 PERFORM WRITE-CUSTOMER-002 PERFORM WRITE-CUSTOMER-003. WRITE-CUSTOMER-001. MOVE 'Customer 001 data' TO RECORD-DATA MOVE 'CUST001' TO RECORD-KEY EXEC CICS WRITE FILE(FILE-NAME) FROM(RECORD-DATA) LENGTH(DATA-LENGTH) RIDFLD(RECORD-KEY) KEYLENGTH(KEY-LENGTH) RESP(RESPONSE-CODE) RESP2(RESPONSE-CODE-2) END-EXEC IF RESPONSE-CODE = 0 ADD 1 TO WRITE-COUNT DISPLAY 'Customer 001 written' END-IF. WRITE-CUSTOMER-002. MOVE 'Customer 002 data' TO RECORD-DATA MOVE 'CUST002' TO RECORD-KEY EXEC CICS WRITE FILE(FILE-NAME) FROM(RECORD-DATA) LENGTH(DATA-LENGTH) RIDFLD(RECORD-KEY) KEYLENGTH(KEY-LENGTH) RESP(RESPONSE-CODE) RESP2(RESPONSE-CODE-2) END-EXEC IF RESPONSE-CODE = 0 ADD 1 TO WRITE-COUNT DISPLAY 'Customer 002 written' END-IF. WRITE-CUSTOMER-003. MOVE 'Customer 003 data' TO RECORD-DATA MOVE 'CUST003' TO RECORD-KEY EXEC CICS WRITE FILE(FILE-NAME) FROM(RECORD-DATA) LENGTH(DATA-LENGTH) RIDFLD(RECORD-KEY) KEYLENGTH(KEY-LENGTH) RESP(RESPONSE-CODE) RESP2(RESPONSE-CODE-2) END-EXEC IF RESPONSE-CODE = 0 ADD 1 TO WRITE-COUNT DISPLAY 'Customer 003 written' END-IF.

Dynamic Record Writing

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
WORKING-STORAGE SECTION. 01 FILE-NAME PIC X(8) VALUE 'CUSTOMER'. 01 RECORD-DATA PIC X(200). 01 DATA-LENGTH PIC S9(8) COMP. 01 RECORD-KEY PIC X(10). 01 KEY-LENGTH PIC S9(8) COMP VALUE 10. 01 RESPONSE-CODE PIC S9(8) COMP. 01 RESPONSE-CODE-2 PIC S9(8) COMP. 01 PROCESSING-COUNT PIC S9(8) COMP VALUE 0. PROCEDURE DIVISION. PERFORM VARYING PROCESSING-COUNT FROM 1 BY 1 UNTIL PROCESSING-COUNT > 5 PERFORM BUILD-RECORD-DATA PERFORM BUILD-RECORD-KEY PERFORM WRITE-RECORD END-PERFORM. BUILD-RECORD-DATA. STRING 'Customer ' DELIMITED BY SIZE PROCESSING-COUNT DELIMITED BY SIZE ' data created at ' DELIMITED BY SIZE FUNCTION CURRENT-DATE DELIMITED BY SIZE INTO RECORD-DATA. MOVE FUNCTION LENGTH(RECORD-DATA) TO DATA-LENGTH. BUILD-RECORD-KEY. STRING 'CUST' DELIMITED BY SIZE PROCESSING-COUNT DELIMITED BY SIZE INTO RECORD-KEY. WRITE-RECORD. EXEC CICS WRITE FILE(FILE-NAME) FROM(RECORD-DATA) LENGTH(DATA-LENGTH) RIDFLD(RECORD-KEY) KEYLENGTH(KEY-LENGTH) RESP(RESPONSE-CODE) RESP2(RESPONSE-CODE-2) END-EXEC IF RESPONSE-CODE = 0 DISPLAY 'Record ' RECORD-KEY ' written successfully' ELSE DISPLAY 'Error writing record ' RECORD-KEY ': ' RESPONSE-CODE END-IF.

Error Handling

Response Code 0

Successful record writing. The record has been successfully written to the file.

Response Code 8

Invalid file name. The specified file name is invalid or not supported.

Response Code 12

Record already exists. A record with the specified key already exists in the file.

Response Code 16

Invalid data length. The specified data length is invalid or exceeds maximum allowed length.

Response Code 20

Write operation failed. The record writing operation failed due to system conditions.

Performance Considerations

Write Efficiency

WRITE efficiently creates records, but large write operations may impact system performance temporarily.

File Management

Proper file management ensures efficient write operations and prevents file corruption and resource issues.

Data Validation

Data validation ensures that write operations create valid records and maintain data integrity.

Best Practices

Error Handling

Always check response codes and handle errors appropriately, especially for record existence and data validation.

Data Validation

Validate record data before writing to ensure data integrity and prevent invalid records from being created.

Key Management

Implement proper key management to ensure unique record identification and prevent duplicate key errors.

Write Optimization

Optimize write operations by using appropriate data lengths and implementing efficient write strategies.

Explain It Like I's 5 Years Old

Imagine you have a notebook and you want to write something new in it. CICS WRITE is like writing a new page in your notebook with a specific title.

The file name is like the name of your notebook, the record data is like what you want to write on the page, and the record key is like the title of the page. When you write it, the new page is added to your notebook.

Just like you need to make sure you don't write the same title twice in your notebook, the program needs to make sure it doesn't create records with the same key.

Exercises

Exercise 1: Basic Record Writing

Write a program that uses WRITE to create a new record with specific data and verify the writing was successful.

Exercise 2: Multiple Record Creation

Create a program that writes multiple records with different keys and validates each write operation.

Exercise 3: Dynamic Data Writing

Implement a data writing system that dynamically creates records based on business data and requirements.