• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • What’s New With CPYFRMIMPF And CPYTOIMPF?

    January 9, 2013 Michael Sansoterra

    As my years as a developer whiz by, I often find myself ignorant of the newest features available. For example, I’ve used the Copy From Import File (CPYFRMIMPF) and Copy To Import File (CPYTOIMPF) commands so often that I just became (as my old boss used to say) “fat, dumb, and happy” when using them, not realizing IBM has added new features since the time I first studied these commands. I’ll briefly share a few of the relatively recent enhancements to these commands.

    First of all, in i7.1 Technology Refresh 5 (TR5), CPYTOIMPF has been enhanced with a new parameter that controls whether it will include the table’s column names as column headings in the first row of the export.

    Consider the following simple example of an ITEMS table:

    CREATE TABLE DEV/ITEMS
    (ITEM_NO INT NOT NULL PRIMARY KEY,
    ITEM_DESCRIPTION VARCHAR(50) CCSID 37 NOT NULL,
    ITEM_PRICE DEC(19,4) NOT NULL DEFAULT 0 
    )
    
    INSERT INTO DEV/ITEMS
    VALUES(1,'Whirlpool Washing Machine',750),
          (2,'Whirlpool Dryer (Gas)',775),
          (3,'Maytag Dishwasher',600)
    

    To export this data to a text file on the IFS, simply include the new Add Column Names (ADDCOLNAM) parameter to the CPYTOIMPF command:

    CPYTOIMPF FROMFILE(DEV/ITEMS)
              TOSTMF('/tmp/Items.csv')
              RCDDLM(*CRLF)
              ADDCOLNAM(*SYS)
    

    The allowed values for this parameter are *NONE (default), *SYS, and *SQL. Specifying *SYS will include the system (short) field names in the export file’s heading row. The combination of defaults and options on the above CPYTOIMPF example will create a comma delimited text file, and the raw output looks like this:

    ITEM_NO,ITEM_00001,ITEM_PRICE
    1          ,"Whirlpool Washing Machine",750.0000
    2          ,"Whirlpool Dryer (Gas)",775.0000
    3          ,"Maytag Dishwasher",600.0000
    

    Specifying *SQL will include the long column names as the headings:

    CPYTOIMPF FROMFILE(DEV/ITEMS)
              TOSTMF('/tmp/Items.csv')
              RCDDLM(*CRLF)
              ORDERBY(ITEM_DESCRIPTION)
              ADDCOLNAM(*SQL)
    

    This time, the raw output has the long column names in the first row:

    ITEM_NO,ITEM_DESCRIPTION,ITEM_PRICE
    3          ,"Maytag Dishwasher",600.0000
    2          ,"Whirlpool Dryer (Gas)",775.0000
    1          ,"Whirlpool Washing Machine",750.0000
    

    Since column names are often “ugly” and not aesthetically appealing for users, you can first copy your query results to a temp table with friendly column names:

    CREATE TABLE QTEMP/ITEM_EXP AS (
    SELECT
    ITEM_NO AS "Item No",
    ITEM_DESCRIPTION "Description",
    ITEM_PRICE       "Unit Price"
      FROM DEV/ITEMS) WITH DATA
    

    And then run your export from the temp table to get this export file:

    "Item No","Description","Unit Price"
    1          ,"Whirlpool Washing Machine",750.0000
    2          ,"Whirlpool Dryer (Gas)",775.0000
    3          ,"Maytag Dishwasher",600.0000
    

    Those column names will look much nicer when the export file is viewed with Excel.

    In the future, I hope IBM will add additional options for the heading export. It would be grand to export the column text or column heading information instead of column names. DDS file fields and DB2 for i table columns can have text descriptions and column headings defined, so why not give an option to export them? Incidentally, you can use SQL’s LABEL ON statement to define column text and/or headings.

    You may have noticed I also included the ORDERBY parameter in an earlier example of CPYTOIMPF. This parameter will accept any column list that is valid in an SQL ORDER BY clause. A special option of *ARRIVAL is allowed as well. This little gem will allow you to sort your results without having to create an intermediate sorted temporary table. This option is available by PTF for i6.1 and i7.1.

    CPYFRMIMPF also has a new option to ignore the heading row on import (in i7.1 TR5). Specifying RMVCOLNAM(*YES) will cause the import to skip the first row (although this has been possible for a long time by just specifying FROMRCD(2) on the command).

    CPYFRMIMPF FROMSTMF('/tmp/Items.csv')
               TOFILE(QTEMP/ITEM_EXP)
               MBROPT(*ADD)
               RCDDLM(*CRLF)
               RMVCOLNAM(*YES)
    

    One more noteworthy option is the MBROPT(*UPDADD), which will cause the CPYFRMIMPF to do an “upsert.” If the imported row is already in the table, it will be updated. Otherwise, it will be appended as a new row. The only requirement for this functionality is the table must have a unique index or primary key. For example, if we want to copy data back into our temporary ITEM_EXP table using the *UPDADD option, we would simply need to make sure that a unique index is present:

    CREATE UNIQUE INDEX QTEMP/UDX_ITEM_EXP
    ON QTEMP/ITEM_EXP ("Item No")
    

    Now, *UPDADD will update existing data already present in the table using “Item No” as the unique key:

    CPYFRMIMPF FROMSTMF('/tmp/Items.csv')
               TOFILE(QTEMP/ITEM_EXP)
               MBROPT(*UPDADD)
               RCDDLM(*CRLF)
               RMVCOLNAM(*YES)
    

    The *UPDADD option has been available since at least V5R3.

    While not an exhaustive list of recent changes, this simply shows there may be more functionality present then we realize. When using those familiar i/OS commands, make sure that you’re always looking over your shoulder. You never know when IBM is going to enhance a command to make the developer’s life easier.

    Michael Sansoterra is a DBA for Broadway Systems in Grand Rapids, Michigan. Send your questions or comments for Mike via the IT Jungle Contact page.



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    ARCAD Software

    DevSecOps & Peer Review – The Power of Automation

    In today’s fast-paced development environments, security can no longer be an afterthought. This session will explore how DevSecOps brings security into every phase of the DevOps lifecycle—early, consistently, and effectively.

    In this session, you’ll discover:

    • What DevSecOps is and why it matters?
    • Learn how to formalize your security concerns into a repeatable process
    • Discover the power of automation through pull requests, approval workflows, segregation of duties, peer review, and more—ensuring your data and production environments are protected without slowing down delivery.

    Whether you’re just getting started or looking to enhance your practices, this session will provide actionable insights to strengthen your security posture through automation and team alignment to bring consistency to the process.

    Watch Now!

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    New Generation Software:  Bring your IBM i Query, Reporting, & Analytics to the NGS-IQ Cloud
    Bytware:  Protect your IBM Power Systems from security threats. Download the IFS security bundle!
    Four Hundred Monitor Calendar:  Latest info on national conferences, local events, & Webinars

    More IT Jungle Resources:

    System i PTF Guide: Weekly PTF Updates
    IBM i Events Calendar: National Conferences, Local Events, and Webinars
    Breaking News: News Hot Off The Press
    TPM @ The Reg: More News From ITJ EIC Timothy Prickett Morgan

    Infinite to Migrate IBM i Apps for Department of the Interior What’s Happening In IBM i App Dev?

    Leave a Reply Cancel reply

Volume 13, Number 1 -- January 9, 2013
THIS ISSUE SPONSORED BY:

WorksRight Software
ProData Computer Services
Adsero Optima

Table of Contents

  • What’s New With CPYFRMIMPF And CPYTOIMPF?
  • Tracing Routines Explain Why The Computer Did What It Did
  • Admin Alert: Four Things To Do For Your IBM i In 2013

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Power Systems Grows Nicely In Q3, Looks To Grow For All 2025, Too
  • Beta Of MCP Server Opens Up IBM i For Agentic AI
  • Sundry IBM i And Power Stack Announcements For Your Consideration
  • Please Take The IBM i Marketplace Survey
  • IBM i PTF Guide, Volume 27, Number 43
  • IBM Pulls The Curtain Back A Smidge On Project Bob
  • IBM Just Killed Merlin. Here’s Why
  • Guru: Playing Sounds From An RPG Program
  • A Bit More Insight Into IBM’s “Spyre” AI Accelerator For Power
  • IBM i PTF Guide, Volume 27, Number 42

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle