• 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
    FalconStor

    Begin Your Journey to the Cloud with Hybrid Cloud Date Protection and Disaster Recovery

    FalconStor StorSafe optimizes and modernizes your IBM i on-premises and in the IBM Power Virtual Server Cloud

    FalconStor powers secure and encrypted IBM i backups on-premise and now, working with IBM, powers migration to the IBM PowerVS cloud and on-going backup to IBM cloud object storage.

    Now you can use the IBM PowerVS Cloud as your secure offsite copy and take advantage of a hybrid cloud architecture or you can migrate workloads – test & development or even production apps – to the Power VS Cloud with secure cloud-native backup, powered by FalconStor and proven IBM partners.

    Learn More

    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

  • To Comfort The Afflicted And Afflict The Comfortable
  • How FalconStor Is Reinventing Itself, And Why IBM Noticed
  • Guru: When Procedure Driven RPG Really Works
  • Vendors Fill In The Gaps With IBM’s New MFA Solution
  • IBM i PTF Guide, Volume 27, Number 27
  • With Power11, Power Systems “Go To Eleven”
  • With Subscription Price, IBM i P20 And P30 Tiers Get Bigger Bundles
  • Izzi Buys CNX, Eyes Valence Port To System Z
  • IBM i Shops “Attacking” Security Concerns, Study Shows
  • IBM i PTF Guide, Volume 27, Number 26

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