Now, Get RPG Codes in all three formats only on this website on every RPGLE related programming article.

Fixed, /Free and Fully Free RPG


Data Area Data Structure in RPG AS400

Data Area Data Structure in RPG AS400
Data Area Data Structure in RPG AS400, ds, data area data structure, data areas ds, data area, data structure, ds, rpg data structure, as4000 data structure, about, create, make, introduction.
Data Area Data Structure in RPG AS400

What is Data Area Data Structure

When andy data structure subfields are based upon the Data area then it's a Data area Data Structure.

In position 23 in D specs, specify U for defining a data area data structure.

Prompt type . . .    D      Sequence number . . .  0002.00                
                                                                          
                            Declaration                      To /         
Name              E   S/U      Type              From       Length        
 ds1                   U        DS                                        
Internal               Decimal                                            
Data Type             Positions      Keywords                             
                                     DTAARA('DATAAREA1')                  
Comment                                                                                                                                          

Declaring Data area Data Structure in RPGLE Fixed format

      * ds subfields are based upon data area                  
     D ds1            UDS                  DTAARA('DATAAREA1') 
     D sbfld1                        10                        
     D sbfld2                        10                        

Declaring Data area Data Structure in RPGLE Fully Free format

Use the DTAARA keyword in fully free RPG with the data structure to make it data area data structure.

           
dcl-ds ds1 dtaara('*LIBL/DATAAREA1') ;
  sbfld1 char(10);
  sbfld2 char(10);
end-ds;

Create a 20 Character Data area to be used in Data Structure

  • Use the CRTDTAARA command to create a data structure.
  • CRTDTAARA DTAARA(DATAAREA1)            
              TYPE(*CHAR)
              LEN(20)
    
  • Use command WRKOBJ DATAAREA1 on the command line to see the created object.
  •                                Work with Objects                             
                                                                                 
     Type options, press Enter.                                                  
       2=Edit authority        3=Copy   4=Delete   5=Display authority   7=Rename
       8=Display description   13=Change description                             
                                                                                 
     Opt  Object      Type      Library     Attribute   Text                     
          DATAAREA1   *DTAARA   EASYCLASS1                                       
    
  • Use command to display data area
  • dspdtaara DATAAREA1
                                   Display Data Area             
                                                                 
     Data area . . . . . . . :   DATAAREA1                       
       Library . . . . . . . :     EASYCLASS1                    
     Type  . . . . . . . . . :   *CHAR                           
     Length  . . . . . . . . :   20                              
     Text  . . . . . . . . . :                                   
                                                                 
                Value                                            
     Offset      *...+....1....+....2....+....3....+....4....+...
         0      '                    '                           
    

    Coding Data Area Data Structure in RPGLE Fixed, /Free and Fully Free format

    In this example, we first declare a data area data structure in RPG program and the read that using IN opcode and update the data area using OUT opcode.

    RPG Code in Fixed format for Data Area data structure in RPGLE AS400
          * ds subfields are based upon data area                                
         D ds1            UDS                  DTAARA('DATAAREA1')               
         D sbfld1                        10                                      
         D sbfld2                        10                                      
         C                   IN        DS1                                       
         C                   EVAL      sbfld1 = 'changed1'                       
         C                   EVAL      sbfld2 = 'changed2'                       
         C                   OUT       DS1                                       
         C                   SETON                                            LR 
         C                   RETURN                                                                                     
    
    RPG Code in /Free format for Data Area data structure in RPGLE AS400
          * ds subfields are based upon data area                      
         D ds1            UDS                  DTAARA('DATAAREA1')     
         D sbfld1                        10                            
         D sbfld2                        10                            
                                                                       
          /Free                                                        
              IN ds1;                                                                                           
                                                                       
              sbfld1 = 'changed1';                                     
              sbfld2 = 'changed2';                                     
                                                                       
              OUT ds1;                                                 
                                                                       
              *inlr = *on;                                             
              return;                                                  
          /End-Free                                                                                                                                                                        
    
    RPG Code in Fully Free format for Data Area data structure in RPGLE AS400
    **FREE                           
    dcl-ds ds1 DTAARA('DATAAREA1');  
      sbfld1 char(10);               
      sbfld2 char(10);               
    end-ds;                          
                                     
    IN *lock ds1;                    
                                     
    sbfld1 = 'changed1';             
    sbfld2 = 'changed2';             
                                     
    OUT ds1;                         
                                     
    *inlr = *on;                     
    return;                                              
    
  • When we declare a DS with UDS then its a data area data structure and during program initialization the data area specified in the DTAARA kwyword gets locked automatically by the program for read and once the program ends it updates the data area and unlock it for others use.
  • When we declare data area data structure in free form RPG we use DTAARA keyword with data structure to base the data structure on the data area. Once we read this data area we need to specify *LOCK while reading the data area using IN opcode otherwise we will encounter Run time error as follows.
  • Run Time Error (RNQ0412)!
    Data area *LIBL/DATAAREA1 is not allocated for output (C G D F).

  • While updating data area using OUT opcode there is no need to use UNLOCK data area since OUT opcode in itself first update the data area and then release the locks on the data area.
  • program Output

    Data area value will get updated.To see the changed data area value again run DSPDTAARA DATAAREA1 command on command line.

                                  Display Data Area                             
                                                                System:   PUB400
    Data area . . . . . . . :   DATAAREA1                                       
      Library . . . . . . . :     EASYCLASS1                                    
    Type  . . . . . . . . . :   *CHAR                                           
    Length  . . . . . . . . :   20                                              
    Text  . . . . . . . . . :                                                   
                                                                                
               Value                                                            
    Offset      *...+....1....+....2....+....3....+....4....+....5              
        0      'changed1  changed2  '                                           
    

    Related Post

    Post a Comment

    © AS400 and SQL Tricks. All rights reserved. Developed by Jago Desain