Copy File (CPYF) command in AS400

Copy File (CPYF) command in AS400
Copy File (CPYF) command in AS400, CPYF,copy file,CPYF,Copy member,*MAP,*DROP,*NOCHECK,*CVTSRC,Copy file without checking the formatting,INCCHAR,CRTDUBOBJ,difference between CRTDUPOBJ & COPYF,copy file example,copy file example
Copy File (CPYF) command in AS400

A Brief Introduction

The Copy file (CPYF) command copies all or some part of data from one database file to the other database file.

  • CPYF command can copy data between the database files and the source files.
  • CPYF command can copy data records from the physical file (PF) or Logical file (LF) to the physical file (PF) not the Logical file (LF). This means the CPYF from the Logical file results in a physical file creation if CRTFILE is specified as *YES.
  • When CRTFILE is set as *YES on CPYF command and FROM FILE has associated trigger, then the file created as per TO FILE parameter does not have the associated trigger. One must have to run ADDPFTRG (Add physical file trigger) command to add a trigger to the TO FILE same as FROM FILE.
  • CPYF command parameters

                                   Copy File (CPYF)              
                                                                 
    Type choices, press Enter.                                   
                                                                 
    From file  . . . . . . . . . . . FROMFILE                    
      Library  . . . . . . . . . . .                  *LIBL      
    To file  . . . . . . . . . . . . TOFILE                      
      Library  . . . . . . . . . . .                  *LIBL      
    From member  . . . . . . . . . . FROMMBR        *FIRST       
    To member or label . . . . . . . TOMBR          *FIRST       
    Replace or add records . . . . . MBROPT         *NONE        
    Create file  . . . . . . . . . . CRTFILE        *NO          
    Print format . . . . . . . . . . OUTFMT         *CHAR        
                                                                 
                               Additional Parameters             
                                                                 
    Which records to print . . . . . PRINT          *NONE        
                              + for more values                  
    Record format of logical file  . RCDFMT         *ONLY        
    Copy from record number  . . . . FROMRCD        *START        
    Copy to record number  . . . . . TORCD          *END                          
    Copy from record key:            FROMKEY                                         
    Number of key fields . . . . .                *NONE                            
    Key value  . . . . . . . . . .                                                 
                                                                                      
                                                                                      
                                                                                      
                               + for more values                                      
                                                                                      
                                                                                      
                                                                                      
    Copy to record key:              TOKEY                                         
    Number of key fields . . . . .                *NONE                          
    Key value  . . . . . . . . . .                                               
                                                                                    
                                                                                    
                                                                                    
                               + for more values                                    
                                                                                    
                                                                                    
                                                                                    
    Number of records to copy  . . . NBRRCDS        *END                                                                                                             
    Include records by char test:    INCCHAR                                       
      Field  . . . . . . . . . . . .                *NONE                          
      Character position . . . . . .                                               
      Relational operator  . . . . .                                               
      Value  . . . . . . . . . . . .                                               
                                                                                    
                                                                                    
    Include records by field test:   INCREL                                        
      Relationship . . . . . . . . .                *NONE                         
      Field  . . . . . . . . . . . .                                               
      Relational operator  . . . . .                                               
      Value  . . . . . . . . . . . .                                               
                                                                                    
                                                                                    
                                                                                    
                               + for more values                                    
     Record format field mapping  . . FMTOPT         *NONE                          
                                                                                    
    Source update options  . . . . . SRCOPT         *SAME                          
                                                                                    
    Source sequence numbering:       SRCSEQ                                        
      Starting sequence number . . .                1.00                           
      Increment number . . . . . . .                1.00                           
      Errors allowed . . . . . . . . . ERRLVL         0           
     Compress out deleted records . . COMPRESS       *YES    
    

    Record format field mapping (FMTOPT)

    We need to specify this option when we are copying physical files or logical files to the physical file or copying data between data file and source file.

    *NONE

  • The field is not mapped during the copy file (CPYF) operation.
  • This value is specified when both FROM FILE and TO FILE are not the database files.
  • This value is specified when both FROM FILE and TO FILE are the database files with the same record format. Record format is the same when every field in FROM FILE is present into TO FILE and has the same starting positions in buffer and the same attributes as well.
  • *NOCHK

  • Specify this option when the Record formats of the FROM FILE and TO FILE are different.
  • Copy operation will be successful even if the differences exist between FROM FILE and TO FILE.
  • Records are copied from left to right fashion from FROM FILE to the TO FILE.
  • *CVTSRC

  • This option is specified to copy data between data files or source files.
  • If the TO FILE is a data file, the FROM FILE sequence number field and date field are dropped and the source data part gets copied from FROM FILE to the TO FILE.
  • If the TO FILE is a source file, the sequence number field and date field are added to the TO FILE and the FROM FILE source data gets copied from FROM FILE to the source data part of the TO FILE.
  • *MAP

  • Fields with the same name in FROM FILE and TO FILE record formats get copied.
  • Fields into TO FILE not mapped with FROM FILE field gets set to the default value specified in the DDS definition of the TO FILE during the copy operation.
  • With *MAP, we can specify *DROP if the mapped fields have different stating positions in FROM FILE and TO FILE.
  • *DROP

  • Specify this value when any field in FROM FILE does not exist in the TO FILE. NULL values get copied.
  • Examples

    Now we will see several examples to better understand how Copy File (CPYF) command actually works.

    Copy Physical file to physical file with all records in the first member

    The following command will be used:

    CPYF FROMFILE(EASYCLASS1/FILE1) 
         TOFILE(EASYCLASS2/FILE1)     
         MBROPT(*ADD)                
         CRTFILE(*YES)               
         ERRLVL(5)                  
    
  • EASYCLASS1 is FROM Library and EASYCLASS2 is TO Library.
  • FILE named FILE1 gets copied from EASYCLASS1 library to EASYCLASS2 library.
  • MBROPT(*ADD) will add records into TO FILE.
  • CRTFILE(*YES) will Create the file named FILE1 in library EASYCLASS2 if it does not exist.
  • ERRLVL(5) will allow the CPYF command to run up to 5 encountered errors and on the 6th error occurrence, CPYF will fail.
  • Copy Physical file to physical file with different Record Format

    The following command will be used:

    CPYF FROMFILE(EASYCLASS1/FILE1) 
         TOFILE(EASYCLASS2/FILE1)     
         FROMMBR(*ALL)                
         TOMBR(*ALL)   
         MBROPT(*ADD)                   
         FMTOPT(*MAP *DROP)
         ERRLVL(5)  
  • FMTOPT(*MAP *DROP) will allow different record format physical files to be copied successfully.
  • Copy Physical data file to physical source file

    The following command will be used:

    CPYF FROMFILE(EASYCLASS1/FILE1) 
         TOFILE(EASYCLASS2/QDDSSRC)     
         FROMMBR(FILE1)  
         TOMBR(*FROMMBR)  
         MBROPT(*REPLACE)
         FMTOPT(*CVTSRC)
  • EASYCLASS1 is FROM Library and EASYCLASS2 is TO Library.
  • Physical data file named FILE1 gets copied from the EASYCLASS1 library to the source physical file QDDSSRC in the EASYCLASS2 library.
  • FROMMBR(FILE1) and TOMBR(*FROMMBR) will copy FILE1 member in from file to the same name member into TO FILE source physical file.
  • MBROPT(*REPLACE) will replace records into TO FILE source member.
  • FMTOPT(*CVTSRC) is used to copy physical data files to source physical files since the two database files are of different types.
  • Copy Logical file to physical file

    The following command will be used:

    CPYF FROMFILE(EASYCLASS1/FILE1LF) 
         TOFILE(EASYCLASS2/FILE1)     
         FROMMBR(FILE1)  
         TOMBR(FILE1)  
         RCDFMT(RFILE1)
         MBROPT(*REPLACE)
  • EASYCLASS1 is FROM Library and EASYCLASS2 is TO Library.
  • The logical file named FILE1LF gets copied from the EASYCLASS1 library to the physical file FILE1 in the EASYCLASS2 library.
  • FROMMBR(FILE1) and TOMBR(FILE1) will copy FILE1 members of FROM FILE to the same name member into TO FILE.
  • RCDFMT(RFILE1) will copy record format named RFILE1 from logical file FILE1LF to the physical file FILE1 since a logical file can have up to 32 record formats and a physical file always have only one record format specified.
  • MBROPT(*REPLACE) will replace records into the TO FILE source member.
  • Post a Comment

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