Creating a Logical File or Index

A logical file provides a different view of a physical file.

The most common and simplest use of logical files is to change the order of the data.

To define a logical file, you use the DDS specifications. You must start the Programming Development Manager and create a member with TYPE of LF (for logical file). Usually, this goes in the object QDDSSRC.

Here is how to make a logical file named CUS01 which orders the CUS physical file by name.

For simple logical files like this, the first line defines the record name of the physical file that has the data. This line also has the PFILE function that names the physical file.

Then, add a K definition for each key file.

 
A          R CUSREC                    PFILE(CUS01)       
A*                                                        
A          K NAME                                         
Name the member in QDDSRC the same as the desired file. In this case, I named it CUS01.

Then, you must compile or create the file. In PDM you can do this with the option 14. When you use option 14, you actually execute the command:

 
CRTLF   FILE(MYLIB/CUS01)   SRCFILE(MYLIB/QDDSSRC)   SRCMBR(CUS01)
The AS/400 knows to use this command because you specified that this member is describing a LF.

Add a SELECT spec to select only customers with STATUS of 'A'

 
A          R CUSREC                    PFILE(CUS01)  
A*                                                   
A          K NAME                                    
A          S STATUS                    COMP(EQ 'A')  

Or to omit records with a STATUS of 'D'
A          R CUSREC                    PFILE(CUS01)  
A*                                                   
A          K NAME                                    
A          O STATUS                    COMP(EQ 'D')  
An RPG program uses logical files almost identically to physical files. In this last example, an RPG program that reads the file CUS01 would automatically bypass any records with STATUS of D.




Back to Basic AS/400 Tips    |    Back to Main Page    |   Contact Info