• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: Sorting Options For CPYTOIMPF

    March 27, 2017 Ted Holt

    I hope that whoever came up with the idea for the Copy to Import File (CPYTOIMPF) command was well compensated. When I think of the time and effort that that command has saved me and countless others, I feel deep gratitude. The addition of the ORDERBY parameter increased the usefulness of CPYTOIMPF, and I’d like to share that with you.

    CPYTOIMPF copies a single-format database file (table, physical file, view, or logical file) to a stream file or physical file in a format that is acceptable to another system or application. Probably the most common use of this command is to build a file of comma-separated values (CSV) for import into Microsoft Excel. Understanding a bit of the internals of this command can help you make even better use of it.

    When you run CPYTOIMPF, the underlying program creates a dynamic SQL statement. That is, it builds a SELECT statement in a character string variable, uses the PREPARE statement to convert the string into an executable statement, and executes the statement. Since the statement is prepared in a string variable, you can add clauses that normally follow ORDER BY in SQL queries to the end of the ORDERBY parameter.

    These are the clauses that SQL syntax permits to follow an ORDER BY expression.

    FETCH FIRST n ROWS 
    OPTIMIZE FOR n ROWS
    FOR UPDATE
    FOR READ ONLY
    WITH <isolation-level>
    SKIP LOCKED DATA
    USE CURRENTLY COMMITTED 
    WAIT FOR OUTCOME

    Most of these options are not applicable in the context of CPYTOIMPF. For example, FOR UPDATE is useless because CPYTOIMPF doesn’t change the retrieved data. The only clauses that I know of that will benefit you are FETCH FIRST n ROWS and SKIP LOCKED DATA. You can use FETCH FIRST to limit the number of rows to be copied. SKIP LOCKED DATA ignores data that is locked by other jobs.

    Here’s a CPYTOIMPF command to serve as an example. As you should have physical file QIWS/QCUSTCDT on your system, you can experiment as you desire. This command copies the data in the physical file to stream file custcdt-1.csv in the current directory.

    CPYTOIMPF FROMFILE(QIWS/QCUSTCDT)
              TOSTMF('custcdt-1.csv')
              MBROPT(*REPLACE)       
              STMFCCSID(*PCASCII)    
              RCDDLM(*CRLF)          
              RMVBLANK(*TRAILING)    
              ORDERBY('state, city') 
              ADDCOLNAM(*SYS)

    Notice the ORDERBY parameter. When the system builds the SQL SELECT, it places the value state, city immediately after ORDER BY. Here are more examples.

    ORDERBY(*arrival)

    Copy the data in arrival sequence (i.e. by relative record number).

    ORDERBY('baldue desc')

    Copy in descending order by balance due.

    ORDERBY('baldue desc fetch first 5 rows only')

    Copy in descending order by balance due, retrieving only the first five rows (records).

              ORDERBY(6,5)

    Sort the data by sixth column, and within that by the fifth column. This is equivalent to the first example, as the fifth and sixth columns of QCUSTCDT are city and state.

    What a wonderful utility!

    RELATED STORIES

    What’s New With CPYFRMIMPF And CPYTOIMPF?

    ADD ORDERBY parameter to CPYTOIMPF command

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: Copy To Import File, CPYTOIMPF, Four Hundred Guru, Guru, IBM i

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    As I See It: The Politics of Self-Disclosure IBM i Shops Seeking More Services

    3 thoughts on “Guru: Sorting Options For CPYTOIMPF”

    • Kelly Cookson says:
      March 27, 2017 at 10:56 am

      Nice tip. I appreciate the info.

      Reply
    • jonboy49 says:
      March 27, 2017 at 10:23 pm

      Had to chuckle when I read this Ted – your “wonderful utility” is my bête noire! I hate it with a passion. It is the reason why the very first Open Access handler I ever wrote was to produce CSVs directly – so I could avoid it!

      Reply
    • Allister Jenks says:
      February 19, 2019 at 8:30 pm

      As cool as the ORDERBY parameter is, this change has a nasty catch. It might seem obvious by the provision of an *ARRIVAL option for the new parameter that *NONE is not necessarily going to give you arrival sequence. But because *NONE is the default, any old code you have using CPYTOIMPF may be affected by this. We had a tool that worked fine for over 9 years suddenly start doing weird stuff after we upgraded to 7.2 recently. It seems SOMETIMES when you specify *NONE (or are just using old code that gets that as a default) it will essentially preserve arrival sequence, then split the file in half and swap the halves!!

      Reply

    Leave a Reply Cancel reply

TFH Volume: 27 Issue: 20

This Issue Sponsored By

  • Fresche
  • Quadrant Software
  • WorksRight Software
  • Computer Keyes
  • Northeast User Groups Conference

Table of Contents

  • The Bang For The Buck Of Entry IBM i Servers
  • IBM i Shops Seeking More Services
  • Guru: Sorting Options For CPYTOIMPF
  • As I See It: The Politics of Self-Disclosure
  • IBM Jacks Up Hardware Maintenance Fees

Content archive

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

Recent Posts

  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23

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