# Writing the work files Dynamically using Define work file

**URL:** <https://techcommunity.softwareag.com/t/writing-the-work-files-dynamically-using-define-work-file/191417>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-Code-Samples\
**Created:** [November 7, 2016, 5:05pm UTC](https://techcommunity.softwareag.com/t/writing-the-work-files-dynamically-using-define-work-file/191417 "2016-11-07T17:05:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![karthikeyan\_Baluswamy1](https://avatars.discourse-cdn.com/v4/letter/k/8491ac/32.png) [@karthikeyan\_Baluswamy1](https://techcommunity.softwareag.com/u/karthikeyan_Baluswamy1)\
**Post date:** [November 7, 2016, 5:05pm UTC](https://techcommunity.softwareag.com/t/writing-the-work-files-dynamically-using-define-work-file/191417/1 "2016-11-07T17:05:57Z")

</div>

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

 ![Program logic.png](https://global.discourse-cdn.com/techcommunity/original/2X/9/9bb70d2cbbe5051ab800f458d8be3a7acf7c49f8.png)

---

<div class="post-metadata">

**Author:** ![Ralph\_Zbrog](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/ralph_zbrog/32/78_2.png) [@Ralph\_Zbrog](https://techcommunity.softwareag.com/u/Ralph_Zbrog)\
**Post date:** [November 7, 2016, 10:29pm UTC](https://techcommunity.softwareag.com/t/writing-the-work-files-dynamically-using-define-work-file/191417/2 "2016-11-07T22:29:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![karthikeyan\_Baluswamy1](https://avatars.discourse-cdn.com/v4/letter/k/8491ac/32.png) [@karthikeyan\_Baluswamy1](https://techcommunity.softwareag.com/u/karthikeyan_Baluswamy1)\
**Post date:** [November 8, 2016, 10:56am UTC](https://techcommunity.softwareag.com/t/writing-the-work-files-dynamically-using-define-work-file/191417/3 "2016-11-08T10:56:53Z")

</div>

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. 😃

Regards  
Karthikeyan

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [April 2, 2021, 3:37pm UTC](https://techcommunity.softwareag.com/t/writing-the-work-files-dynamically-using-define-work-file/191417/4 "2021-04-02T15:37:16Z")

</div>


