Writing the work files Dynamically using Define work file

Dear Guys,

Im facing an issue while writing the work files dynamically. Kindly find the details below :

Input file :
2ARB54
7MOR58
7MOR58
7MOR58
2ARB55

Expected OUPUT FILE :

Work file 1
2ARB54
7MOR58
7MOR58
7MOR58

Work file 2
2ARB55

Program Logic :
Kindly check the attachment.

Issue:
I’m getting only few records in the work file though the work file name are assigned correctly . Some of them are getting skipped. Pls note the program has only one if condition

output :

BUYER CODE : 2ARB54 DD-NAME : CMWKF02
BUYER CODE : 7MOR58 DD-NAME : CMWKF02
BUYER CODE : 2ARB55 DD-NAME : CMWKF03
NAT9995 Natural session terminated normally.

Actual output :
Work file 1
7MOR58
7MOR58
7MOR58

Work file 2
2ARB55

DEFINE WORK FILE issues an OPEN for the file. If already open, Natural first issues an implicit close.

When you execute the DEFINE WORK FILE for the first 7MOR58 record, the CLOSE/OPEN causes the contents of CMWKF02 (2ARB54) to be overwritten by the new record.

A BAD solution would be to add the APPEND attribute to the DEFINE WORK FILE statement. This is a bad idea because you would be causing Natural to communicate with the operating system (i.e. open and close the file) more than once, which is unnecessary overhead. Ideally you want only one OPEN (either implicitly by Natural, or explicitly with a DEFINE WORK FILE) and one CLOSE (typically, implicitly by Natural at end of program, or explicitly via CLOSE WORK) for each WORK file created.

Consider two WRITE WORK statements, one for CMWKF02 and one for CMWKF03, and forget the DEFINE WORK FILE.

Hello Ralph,

Thanks for the reply!
I still remember that I have implemented this logic before for a similar requirement. Based on your explanation , I found out what has went wrong. The sorting of the records didn’t happen properly which leads to open and close work file issue. Now I have include work file name also in the layout and that’s worked properly.

Input work file :
7MOR58 CMWKF02
7MOR58 CMWKF02
7MOR58 CMWKF02
7MOR58 CMWKF02
2ARB54 CMWKF02
2ARB54 CMWKF02
2ARB54 CMWKF02
2ARB55 CMWKF03
2ARB55 CMWKF03
2ARB55 CMWKF03

Output :
CMWKF02

7MOR58
7MOR58
7MOR58
7MOR58
2ARB54
2ARB54
2ARB54

CMWKF03

2ARB55
2ARB55
2ARB55

Thank a lot Ralph. :smiley:

Regards
Karthikeyan