Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested tables using depending

I am facing problem with nested DEPENDING ON Clause. I need to generate the table with below requirement.

For example.

  ....
  ....  
     20 Person occurs 1 to 20
               depending on cnt-person.
        25 Id                  pic x(05).
        25 Name                pic x(10).
        25 Address             pic x(20).
        25 paymentA.
           30 Subsidydata.
             35 Subsidy occurs 0 to 10 
                        depending on cnt-A-subsidy.
                40 Begin      pic x(02).
                40 End        pic x(02).
                40 Amount     pic x(03).
           30 Provdata.
             35 Provision occurs 0 to 10 
                        depending on count-A-provis.
                40 Begin      pic x(02).
                40 End        pic x(02).
                40 Amount     pic x(03).
        25 paymentB.
           30 Subsidydata.
             35 Subsidy occurs 0 to 10 
                        depending on count-B-subsidy.
                40 Begin      pic x(02).
                40 End        pic x(02).
                40 Amount     pic x(03).
           30 Provdata.
             35 Provision occurs 0 to 10 
                        depending on count-B-provis.
                40 Begin      pic x(02).
                40 End        pic x(02).
                40 Amount     pic x(03).
 
 01 count-details.                                            
   05 cnt-person                   pic S9(04) binary.
   05 cnt-A-subsidy                pic S9(04) binary.   
   05 count-A-provis               pic S9(04) binary.   
   05 count-B-subsidy              pic S9(04) binary.   
   05 count-B-provis               pic S9(04) binary.
....
....

I have tried to execute the code, during execution the first occurance gets executed perfectly fine for Person 1. But from next occurance for Person 2 onwards as soon as ADD 1 to cnt-A-subsidy, my first occurences values are overwritten (overlayed) in PaymentA and PaymentB.

Once the whole table with variable length gets populated, my requirement is to generate the XML without empty tags (variable length). Because there is a chance or possibility that PaymentA or PaymentB with subsidydata or provdata can be available or not.

My code is like this,

In a loop reading db2 table...person 

   add  1                     to cnt-person.        
   move spaces                to Person(cnt-person). 
   move table-idnr            to Id
                              of Person(cnt-person). 
   move table-addr            to address
                              of Person(cnt-person).   

  then again reading another table in loop...fetch records and then,
  
    if subsidydataA.                                 
        add  1                  to cnt-A-subsidy.                                                                     
        move table-begin        to Begin                     
                                of subsidy                   
                                of subsidydata              
                                of paymentA            
                                  (cnt-person,cnt-A-subsidy).                                    
       ...  
       ...     
    end.
    if ProvdataA.                                 
        add  1                  to cnt-A-provis.                                                                     
        move table-begin        to Begin                     
                                of Provision                   
                                of provdata              
                                of paymentA            
                                  (cnt-person,cnt-A-provis).                                    
       ... 
       ...     
    end.
    if subsidydataB.                                 
        add  1                  to cnt-B-subsidy.                                                                     
        move table-begin        to Begin                     
                                of subsidy                   
                                of subsidydata              
                                of paymentB            
                                  (cnt-person,cnt-B-subsidy).                                    
       ...      
       ... 
    end.
    if ProvdataB.                                 
        add  1                  to cnt-B-provis.                                                                     
        move table-begin        to Begin                     
                                of Provision                   
                                of provdata              
                                of paymentB            
                                  (cnt-person,cnt-B-provis).                                    
       ... 
       ...     
    end.
..... then ended...... 

First occurance works fine and the second occurance for the person 2 overlays person 1 data.

I can understand the error that for Person 2 (cnt-person is 2) , I need to point the cnt-A-subsidy to Person 2. How to do it? Since it is pointing to cnt-person 1, it is overwritten.

Please suggest. Thanks in advance.

like image 627
MaMu Avatar asked May 30 '26 05:05

MaMu


2 Answers

Everything is righ but... you are using global counters.

01 count-details.                                            
   05 cnt-person           pic S9(04) binary.
   05 cnt-A-subsidy        pic S9(04) binary.   
   05 count-A-provis       pic S9(04) binary.   
   05 count-B-subsidy      pic S9(04) binary.   
   05 count-B-provis       pic S9(04) binary.

So, for each Person(cnt-person), the nested OCCURS DEPENDING ON clause is referencing the same global variable.

Therefore, every person share the same variables. When Person 1 sets cnt-A-subsidy = 3,and Person2 says cnt-A-subsidy = 1 , both Person1 and Person2 hold the same value for cnt-A-subsidy, that's it, the last modified one (1).

This is messing data in PaymentA, PaymentB etc.

In order to solve this and avoid shared state, define the count fields inside the Person structure.

01 Persons.
   05 Person occurs 1 to 20 depending on cnt-person.
      10 Id             pic x(05).
      10 Name           pic x(10).
      10 Address        pic x(20).
      10 Cnt-A-Subsidy  pic S9(04) binary.
      10 Cnt-A-Provis   pic S9(04) binary.
      10 Cnt-B-Subsidy  pic S9(04) binary.
      10 Cnt-B-Provis   pic S9(04) binary.
      10 paymentA.
...
like image 162
aran Avatar answered May 31 '26 17:05

aran


When you increase cnt-A-subsidy, your array Subsidy increases, changing the length of your record, causing your data to overwrite. This will get worse as the other indexes increase such as count-A-provis increase during processing. Might need to break these into multiple working records.

like image 36
Zachary Hardin Avatar answered May 31 '26 17:05

Zachary Hardin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!