Skip to main content

CRTDUPOBJ, CPYF & The difference

CRTDUPOBJ & CPYF

CRTDUPOBJ & CPYF are the two commands that are often used when it comes to creating a copy of the object and copying the data from a physical file to another. 

Let's have a detailed look at each of these commands in detail and see the difference between two of them.

CRTDUPOBJ (Create Duplicate Object)

The Create Duplicate Object (CRTDUPOBJ) command copies a single object or a group of objects. It does not create an exact duplicate of files.

CRTDUPOBJ can be used to copy a group of related objects by specifying a generic object name or by specifying *ALL or more than one object type.

When copying a group of related objects, 'To Library (TOLIB)' should be different from From Library (FROMLIB).

You can specify whether data in physical files or save files is copied.  You can also specify whether any constraints or triggers associated with an existing database file are to be associated with the newly-created file and whether the file level and member level identifiers of an existing database file are to be copied to the newly-created database file.

CRTDUPOBJ on a Logical File will create Logical File. But, Dependent Physical File should be available on Target Library.

Below are some important parameters to note while copying Files.
  • Duplicate Data (DATA) - *YES to copy the data from 'FROM FILE', '*NO' to not copy the data from 'FROM FILE'.
  • Duplicate Constraints (CST) - *YES to copy the constraints from 'FROM FILE', '*NO' to not copy the constraints from 'FROM FILE'.
  • Duplicate Triggers (TRG) - '*YES' to copy the triggers from 'FROM FILE', '*NO' to not copy the triggers from 'FROM FILE'.
  • Duplicate File Identifiers (FILEID) - '*YES' to use the File level and Member level identifiers to create the 'TO FILE', '*NO' to not use the File level and Member level identifiers to create the 'TO FILE'.

CPYF (Copy File)

The Copy File (CPYF) command copies all or part of a file from the database or from an external device to the database or to an external device.  It can,
  • Copy data and source files between database files.  Records can be copied from physical or logical files.  However, records can be copied  only to physical files, not to logical files.
  • Copy data and source files from external devices, such as diskette and tape, to the database.
  • Copy data and source files from the database to external devices.
  • Copy data and source files from external devices to other external devices.
  • Copy data and source files from inline data files to the database or to external devices.   
Below are some important parameters while using CPYF. 
  • Replace or Add records (MBROPT) - '*ADD' to add the records to the end of the current set of records, '*REPLACE' to replace the records in TOFILE, '*UPDADD' - to update the duplicate key records and add the new records.
  • Create File (CRTFILE) - '*YES' to create the TOFILE, '*NO' if TOFILE is already present. CPYF on Logical file with CRTFILE(*YES) will create a Physical file and copies data.
  • Record Format Field Mapping (FMTOPT) - '*NONE' no field mapping is done, '*NOCHK' if the record formats are different, no check would happen and records are directly copied, '*MAP *DROP' is used to map the fields and copy the data and drop if no matching fields present. 
  • Include Records by char test (INCCHAR) and/or Include Records by Field test (INCREL) - used to validate the record in the from file and copy to TOFILE if the condition satisfies. 
  • Number of Records to copy (NBRRCDS) - Specify the number of records to be copied. '*END' to copy all the records.

And, the difference (CRTDUPOBJ vs CPYF)?

  • CRTDUPOBJ needs to allocate the file in order to create the duplicate. CPYF doesn't need to allocate the file to copy the data. 
  • CRTDUPOBJ would create a copy of the file with either no data or full data. With CPYF Subset of data can be copied using RRN, Key values, INCCHAR or INCREL.
  • CPYF is also useful when populating a new version of the file which has new fields added (or removed) as fields can be transposed using FMTOPT(*MAP *DROP).

Comments

Popular posts from this blog

All about READ in RPGLE & Why we use it with SETLL/SETGT?

READ READ is one of the most used Opcodes in RPGLE. As the name suggests main purpose of this Opcode is to read a record from Database file. What are the different READ Opcodes? To list, Below are the five Opcodes.  READ - Read a Record READC - Read Next Changed Record READE - Read Equal Key Record READP - Read Prior Record READPE - Read Prior Equal Record We will see more about each of these later in this article. Before that, We will see a bit about SETLL/SETGT .  SETLL (Set Lower Limit) SETLL accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Corresponding Record (or Next Record if exact match isn't found).  SETGT (Set Greater Than) SETGT accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Next Record (Greater Than the Key value). Syntax: SETLL SEARCH-ARGUMENTS/KEYFIELDS FILENAME SETGT  SEARCH-ARGUMENTS/KEYFIELDS FILENAME One of the below can be passed as Search Arguments. Key Fiel

What we need to know about CHAIN (RPGLE) & How is it different from READ?

CHAIN READ & CHAIN, These are one of the most used (& useful) Opcodes by any RPG developer. These Opcodes are used to read a record from file. So, What's the difference between CHAIN & READ?   CHAIN operation retrieves a record based on the Key specified. It's more like Retrieving Random record from a Database file based on the Key fields.  READ operation reads the record currently pointed to from a Database file. There are multiple Opcodes that start with READ and all are used to read a record but with slight difference. We will see more about different Opcodes and How they are different from each other (and CHAIN) in another article. Few differences to note.  CHAIN requires Key fields to read a record where as READ would read the record currently pointed to (SETLL or SETGT are used to point a Record).  If there are multiple records with the same Key data, CHAIN would return the same record every time. READE can be used to read all the records with the specified Ke

Extract a portion of a Date/Time/Timestamp in RPGLE - IBM i

%SUBDT Extracting Year, Month, Day, Hour, Minutes, Seconds or Milli seconds of a given Date/Time/Timestamp is required most of the times.  This can be extracted easily by using %SUBDT. BIF name looks more similar to %SUBST which is used to extract a portion of string by passing from and two positions of the original string. Instead, We would need to pass a value (i.e., Date, Time or Timestamp ) and Unit (i.e., *YEARS, *MONTHS, *DAYS, *HOURS, *MINUTES, *SECONDS or *MSECONDS) to %SUBDT.  Valid unit should be passed for the type of the value passed. Below are the valid values for each type. Date - *DAYS, *MONTHS, *YEARS Time - *HOURS, *MINUTES, *SECONDS Timestamp - *DAYS, *MONTHS, *YEARS, *HOURS, *MINUTES, *SECONDS, *MSECONDS Syntax: %SUBDT(value : unit { : digits { : decpos} }) Value and Unit are the mandatory arguments.  Digits and Decimal positions are optional and can only be used with *SECONDS for Timestamp. We can either pass the full form for the unit or use the short form. Below i