Programming Languages on Power

Power Programming Languages

IBM Power, including the AIX, IBM i, and Linux operating systems, support a wide range of programming languages, catering to both traditional enterprise applications and modern development needs.


#Power

 View Only
Expand all | Collapse all

using select to read a file in the IFS

  • 1.  using select to read a file in the IFS

    Posted Fri May 12, 2023 11:58 AM

    is it possible to use select to read records from a file (csv, xlsx...) from the IFS?



    ------------------------------
    David Strawn
    ------------------------------

    #SQL


  • 2.  RE: using select to read a file in the IFS

    Posted Fri May 12, 2023 09:16 PM
    Edited by Satid Singkorapoom Fri May 12, 2023 09:17 PM

    Dear David

    The fortunate answer is yes through new IBM i services table functions but you need to run at least IBM i 7.3 with recent TR PTF level.  More information in these articles

    Reading a file in the IFS with SQL at https://www.rpgpgm.com/2020/11/reading-file-in-ifs-with-sql.html
    Copying data from IFS file using SQL, real life example at  https://www.rpgpgm.com/2021/11/copying-data-from-ifs-file-using-sql.html
    Using SQL to list directories and files in IFS  at  https://www.rpgpgm.com/2019/11/using-sql-to-list-directories-and-files.html
    Exploring the IFS with DB2 services at  https://blog.faq400.com/en/db2-for-i/exploring-the-ifs-with-db2-services/

    The entire list of useful IBM i services via SQL interface that you can use to do typically non-SQL operations is here  https://www.ibm.com/support/pages/ibm-i-services-sql     There are many useful IBM i services' table functions and procedures in the section IFS Services.


    ------------------------------
    Education is not the learning of facts but the training of the mind to think. -- Albert Einstein.
    ------------------------------
    Satid S.
    ------------------------------



  • 3.  RE: using select to read a file in the IFS

    Posted Fri May 12, 2023 09:43 PM

    For these IBM i services through SQL interface, Run SQL Script tool in IBM i ACS provides samples to help you use many of them. This is how you access it :

    From Run SQL Script main menu bar, select  Edit --> Examples --> Insert from Examples


    Select IBM i Services from the drop-down field and scroll down to the section for IFS and select one of your interest 


    You can write your own and add it to this example repository. 



    ------------------------------
    Education is not the learning of facts but the training of the mind to think. -- Albert Einstein.
    ------------------------------
    Satid S.
    ------------------------------



  • 4.  RE: using select to read a file in the IFS

    Posted Tue May 16, 2023 11:03 AM

    Thanks for the references.  I took a quick look at the links you sent, but it doesn't look like these methods give you any way to preserve column integrity, which I was hoping to be able to do.  but i will dig deeper into it...



    ------------------------------
    David Strawn
    ------------------------------



  • 5.  RE: using select to read a file in the IFS

    Posted Wed May 17, 2023 03:15 AM
    Edited by Satid Singkorapoom Wed May 17, 2023 08:45 AM

    Dear David

    I need to understand what you mean by "preserve column integrity". Do you want each column value of each line from a CSV file to be inserted into each corresponding column of each record in the target physical file?  If so, I'm not sure if your are aware that an IBM i command CPYFRMIMPF is available for importing a CSV file from IFS to a physical file (and CPYTOIMPF to do the data export)?  It's not SQL but you can run any IBM i command in an SQL script text file by starting the command line with CL: and ending the command line with a colon symbol. 



    ------------------------------
    Education is not the learning of facts but the training of the mind to think. -- Albert Einstein.
    ------------------------------
    Satid S.
    ------------------------------



  • 6.  RE: using select to read a file in the IFS

    Posted Wed May 17, 2023 04:23 AM

    You never have "column integrity" in IFS streamfiles, which are just a sequence of bytes! The SQL functions referenced by @Satid Singkorapoom can return these bytes for you but you will have to interpret them yourself.

    SQL is not suited for these kind of operations, and I would go for CPYFRMIMPF for CSV files. This command will copy the streamfile into a SQL table and validate the "column integrity".

    For Excel files, I would recommend Python and Pandas to convert an Excel file into a SQL table. But this is a very advanced technique, which requires knowledge of open source tools on IBM i.

    Best regards,
    Christian



    ------------------------------
    Christian Jorgensen
    IT System Administrator
    Network of Music Partners A/S
    ------------------------------



  • 7.  RE: using select to read a file in the IFS

    Posted Wed May 17, 2023 08:38 AM
    Edited by Satid Singkorapoom Wed May 17, 2023 08:56 AM

    Dear Christian

    For importing Excel (or Calc) spreadsheet file into a DB2i table (and the reverse - export), there is another long-lasting non-programmatic tool which is Data Transfer to/from IBM i available as well, since the time of AS/400 Client Access for Windows and now from IBM i ACS - Data Transfer which now looks like this : 



    ------------------------------
    Education is not the learning of facts but the training of the mind to think. -- Albert Einstein.
    ------------------------------
    Satid S.
    ------------------------------



  • 8.  RE: using select to read a file in the IFS

    Posted Wed May 17, 2023 09:43 AM

    Ah yes, had forgotten about ACS since I chased a programmatic solution.

    But ACS is a Java program and can run inside IBM i - it is probably already installed on systems via PTF in /QIBM/ProdData/Access/ACS/Base.
    There are some great examples of how to utilize ACS in IBM i by Scott Forstie and Tim Rowe here:
    https://www.ibm.com/support/pages/node/6340297/

    Hope this is useful to the OP!

    Best regards,
    Christian



    ------------------------------
    Christian Jorgensen
    IT System Administrator
    Network of Music Partners A/S
    ------------------------------



  • 9.  RE: using select to read a file in the IFS

    Posted Wed May 17, 2023 02:48 PM

    Thanks for all the input!



    ------------------------------
    David Strawn
    ------------------------------



  • 10.  RE: using select to read a file in the IFS

    Posted Wed May 17, 2023 08:29 PM

    Dear David

    I forgot to say that CPYFROMIMPF and any IBM i command at all can also be invoked from SQL code using QCMDEXC scalar function:  CALL QSYS2.QCMDEXC( '<command line text>' ) ;



    ------------------------------
    Education is not the learning of facts but the training of the mind to think. -- Albert Einstein.
    ------------------------------
    Satid S.
    ------------------------------



  • 11.  RE: using select to read a file in the IFS

    Posted Thu May 18, 2023 09:28 AM

    Yeah, that procedure is very helpful...



    ------------------------------
    David Strawn
    ------------------------------