IBM i Global

IBM i Global

Connect, learn, share, and engage with IBM Power.

 View Only
Expand all | Collapse all

Finding the defined row length for a file (not a table) using SQL

  • 1.  Finding the defined row length for a file (not a table) using SQL

    Posted Wed April 10, 2024 04:21 PM

    Hope that i am in the correct forum. 

    Disclaimer, I am an MS SQL developer and want to use SQL to find the defined row length of a file. The file is not a SQL object. I can use TABLE(QSYS2.OBJECT_STATISTICS ... to query the file. Cannot use qsys2.tables, sysibm.tables, or sysibm.sqltables. I can return the data and then use a LEN on the returned data, but want to see how the file row length is defined. Is it possible using SQL? Also, what is this type of file called - one that is not also setup as a table?

    this is the only ways that i have found to "query" the library or the files in it

    SELECT * FROM TABLE(QSYS2.OBJECT_STATISTICS('BXX_WAA360','ALL'))

    SELECT * FROM TABLE(QSYS2.IFS_OBJECT_STATISTICS(START_PATH_NAME => '/QSYS.LIB/BXX_WAA360.LIB'))



    ------------------------------
    brenda grossnickle
    BI Programmer Analyst
    FIS
    ------------------------------


  • 2.  RE: Finding the defined row length for a file (not a table) using SQL

    Posted Wed April 10, 2024 09:29 PM
    Edited by Satid S Wed April 10, 2024 09:35 PM
      |   view attached

    Dear Brenda

    From your post, I see that you are ACTUALLY talking about an SQL table. BXX_WAA360 is the schema name in Db2 for i (it is also called a Library object in IBM i). An SQL table in DB2 for i is also called a physical file object in IBM i jargon (back when SQL was not yet available in DB2/400) but both are the same database entity.   

    >>>> Cannot use qsys2.tables, sysibm.tables, or sysibm.sqltables.   <<<<

    You have the names wrong. I guess you are referring to DB metadata or data dictionary by another name (in IBM i, it is called catalog tables and views) and the full list is here: IBM i catalog tables and views at  https://www.ibm.com/docs/en/i/7.5?topic=views-i-catalog-tables    

    So, you should query SYSCOLUMNS in QSYS2 library like this sample: 

    SELECT table_name, table_schema, column_name, data_type, length FROM  qsys2.syscolumns WHERE table_schema = 'BXX_WAA360' AND table_name = '....' AND column_name = '.....' 

    I also attach a basic article about DB metadata in Db2 for i for you digestion.  

    Let us know when you have more questions. 



    ------------------------------
    Satid S
    ------------------------------

    Attachment(s)

    pdf
    DB-DB2i Catalog Views.pdf   1013 KB 1 version


  • 3.  RE: Finding the defined row length for a file (not a table) using SQL

    Posted Thu April 11, 2024 09:50 AM

    Thanks for the attachment and links. They are very useful. The three views that I listed in my original post all work at my shop. Maybe they are custom views or archive views. I have been using them for over 10 years. My files are not returned with the qsys2.syscolumns or qsys2.systables views. Below Guy suggested using qsys2.sysfiles and this did return my files. One of the columns of sysfiles is SQL_OBJECT_TYPE and it was NULL. Tested sysfiles with the most common objects that I query and they all returned SQL_OBJECT_TYPE = TABLE and they returned table information when using systables. But these files are not SQL tables and my usual queries did return any results.



    ------------------------------
    brenda grossnickle
    BI Programmer Analyst
    FIS
    ------------------------------



  • 4.  RE: Finding the defined row length for a file (not a table) using SQL

    Posted Thu April 11, 2024 08:46 PM
    Edited by Satid S Fri April 12, 2024 03:49 AM

    Dear Brenda

    Your response jogs my faint memory that there are actually 3 sets of DB catalog views in IBM i and further Google search reveals that you are using what is called ANS and ISO Catalog Views which I never had to deal with before:  Db2 for i catalog views at https://www.ibm.com/docs/en/i/7.5?topic=reference-db2-i-catalog-views  

    qsys2.tables, sysibm.tables, or sysibm.sqltables belong to ANS and ISO Catalog Views set and here is its full list:  https://www.ibm.com/docs/en/i/7.5?topic=views-ans-iso-catalog      

    But QSYS2.SYSFILES catalog view belongs to IBM i Catalog Tables and Views and if you read its description from the first URL above, you will see this :  The SYSFILES view contains one row for every Db2 for i database file, including native files. This view includes attributes not returned by QSYS2.SYSTABLES.

    The "native file" is physical or logical file created by native IBM i language (called DDS - Data Definition Specification) that existed on the first day of CPF (the predecessor of OS/400 and IBM i) about 40 years ago before SQL was available.

    Logical file in native IBM i jargon is the same as SQL view or index. When you create SQL table, view, or index, they are seen from IBM i native interface as physical or logical files. The DB metadata of physical and logical files are maintained in DB2 for i catalog just like SQL tables, views, and indexes.  These physical and logical files can be used by SQL interface in/to IBM i in a normal way.   Hope this answers your question "How is it that some files are tables and some are not? ".   


    ------------------------------
    Satid S
    ------------------------------



  • 5.  RE: Finding the defined row length for a file (not a table) using SQL

    Posted Fri April 12, 2024 10:42 AM

    "The "native file" is physical or logical file created by native IBM i language (called DDS - Data Definition Specification) that existed on the first day of CPF (the predecessor of OS/400 and IBM i) about 40 years ago before SQL was available."

    Just to extend Satid's answer a bit.  "native" files can also include files not created with DDS but simply by using CRTPF and a defined length. If I recall correctly such files have a single column named FLD00001 or similar.  A further exception would be files created for the S/36 EE which are similar but (if they are a keyed file) may have multiple columns FLDnnn and KEYnnn (?). There can also be "variable length" files created directly by a COBOL program but these are rare.  In all of these cases, the column definitions would be defined within the program and the database does not know of them.

    As Satid noted all of these will appear in SYSFILES. 



    ------------------------------
    Jon Paris
    ------------------------------



  • 6.  RE: Finding the defined row length for a file (not a table) using SQL

    Posted Wed April 10, 2024 09:41 PM

    By the way, we also have a specific discussion group for SQL in this community. It's here :  https://community.ibm.com/community/user/power/communities/community-home?CommunityKey=3fcb1d91-6b8b-42a1-aa05-26e106041399



    ------------------------------
    Satid S
    ------------------------------



  • 7.  RE: Finding the defined row length for a file (not a table) using SQL
    Best Answer

    Posted Thu April 11, 2024 03:24 AM

    hello Brenda,

    look at qsys2.sysfiles et more specifically to the column maximum_record_length, I think you will find what you are looking for. 

    best regards, Guy



    ------------------------------
    Guy Marmorat
    Security Expert for IBM i - Resiliane
    ------------------------------



  • 8.  RE: Finding the defined row length for a file (not a table) using SQL

    Posted Thu April 11, 2024 09:35 AM

    That is exactly what I needed. Thanks! When running sys2.sysfiles and my files are returned they have SQL_OBJECT_TYPE as NULL whereas all the other objects that i query have SQL_OBJECT_TYPE as 'TABLE'. How is it that some files are tables and some are not? what is it called when the file is not a table (ifs, native, data stream, logical ...)? FYI - These files come into the IBMi from a third party via some type of FTP. 



    ------------------------------
    brenda grossnickle
    BI Programmer Analyst
    FIS
    ------------------------------



  • 9.  RE: Finding the defined row length for a file (not a table) using SQL

    Posted Thu April 11, 2024 01:24 PM

    ok Brenda, happy to hear it was useful. 
    Files that are tables have been created initially with a create table SQL instruction. 

    Files that not SQL based have been created initially with a CRTPF or CRTLF for data files or with a CRTSRCPF for source files. 
    But all these files are part of the QSYS.LIB system, not the IFS (aka what we call stream files). 
    Even if your files are created via FTP or whatever, what matters is the way they have been initially created. 
    Hope this is helpful. 
    thanks and regards, Guy



    ------------------------------
    Guy Marmorat
    Security Expert for IBM i - Resiliane
    ------------------------------