IBM i Global

IBM i 

A space for professionals working with IBM’s integrated OS for Power systems to exchange ideas, ask questions, and share expertise on topics like RPG and COBOL development, application modernization, open source integration, system administration, and business continuity.


#Power


#IBMi
 View Only
Expand all | Collapse all

LFs pointing to PFs from incorrect libraries

  • 1.  LFs pointing to PFs from incorrect libraries

    Posted Fri February 14, 2025 03:55 AM

    Hi,

    I need to extract all such LFs which are created on PFs (which are created in incorrect libraries)

    for example LF1 which is there in Lib1 and has been created on PF1 in Lib2.

    So any SQL query to extract names of all such LFs ?

    Thanks...



  • 2.  RE: LFs pointing to PFs from incorrect libraries

    Posted Fri February 14, 2025 08:04 AM

    tried below but It's been running for so long and consuming too much CPU%

    :

    SELECT lfs.TABLE_SCHEMA AS LFS_LIBRARY,
           lfs.TABLE_NAME AS LFS_NAME,
           pfs.TABLE_SCHEMA AS PFS_LIBRARY,
           pfs.TABLE_NAME AS PFS_NAME
    FROM QSYS2.SYSTABLES lfs
    JOIN QSYS2.SYSTABLES pfs
      ON lfs.REMARKS = pfs.TABLE_NAME   -- Assuming physical file info is in REMARKS for LFS
    WHERE lfs.TYPE = 'L'  -- Logical File
      AND pfs.TYPE = 'P'  -- Physical File
      AND pfs.TABLE_SCHEMA NOT IN ('VALID_LIBRARY_1', 'VALID_LIBRARY_2', 'VALID_LIBRARY_3');



    ------------------------------
    jerry ven
    ------------------------------



  • 3.  RE: LFs pointing to PFs from incorrect libraries

    Posted Fri February 14, 2025 09:28 AM

    You mean something like this:

    select *
    from qsys2.sysindexes
    where SYSTEM_INDEX_SCHEMA <> SYSTEM_TABLE_SCHEMA

    which catches DDS created logical files also?

    No guarantees about join logical files, or some of the sql type stuff.



    ------------------------------
    Robert Berendt IBMChampion
    Business Systems Analyst, Lead
    Dekko
    Fort Wayne
    ------------------------------



  • 4.  RE: LFs pointing to PFs from incorrect libraries

    Posted Sun February 16, 2025 10:38 PM

    You should try querying a catalog view named SYSVIEWDEP.  You select VIEW_NAME (and other relevant catalog columns) with OBJECT_TYPE = 'VIEW' and compare VIEW_SCHEMA against TABLE_SCHEMA ( or OBJECT_SCHEMA?) to get the ones that do not correspond.   

    When making queries to Db2 catalog view, you basically have no control on its run-time but if you want the fastest result and you still have some CPU and disk IO performance (not disk space) capacity left, you can enable DB2 SMP for your query by preceding your SELECT with SET CURRENT DEGREE = '<an integer number for parallel degree>'  (try 3 or 4 first). 



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



  • 5.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 01:54 AM
    Edited by Birgitta Hauser Mon February 17, 2025 01:55 AM

    You may try the following query which will check the depending logical files for all physical files within a specific schema. The example will return all logical files that are NOT in the same library as the physical.

    Select Table_Schema as PFTable_Schema, Table_Name as PFTable, 
           Schema_Name  as LFSchema,       SQL_Name   as LFSQLName, System_Name as LFSYSName
      from SysTables a cross join
           Lateral(Select *
                      from Table(Systools.Related_Objects(
                                          Library_Name => a.Table_Schema
                                          File_Name => a.Table_Name))
                      Where SQL_Object_Type like '%LOGICAL%'
                            and Schema_Name <> a.Table_Schema
                            )  
      Where     a.Table_Schema = 'YOURSCHEMA'   
            and Table_Type in ('P''T');

    Note: May also run sometime!

    ------------------------------
    Birgitta Hauser
    Database and Software Engineer
    Birgitta Hauser - Modernization-Education-Consulting on IBM i (selfemployed)
    Kaufering
    +49 170 5269964
    ------------------------------



  • 6.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 07:42 AM

    So,

    below is below SQL not sufficient to provide the desired LFs details here?

    "select *
    from qsys2.sysindexes
    where SYSTEM_INDEX_SCHEMA <> SYSTEM_TABLE_SCHEMA

    "

    Thanks...




  • 7.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 08:02 AM

    What did you find out by trying it?

    Is it more or less inclusive than "assuming" the PF is in the remarks?

    What files were you finding that it missed?

    But Birgitta's suggestion bears looking into.



    ------------------------------
    Robert Berendt IBMChampion
    Business Systems Analyst, Lead
    Dekko
    Fort Wayne
    ------------------------------



  • 8.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 08:06 AM

    i found different data set in both sql 



    ------------------------------
    jerry ven
    ------------------------------



  • 9.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 08:10 AM

    for SQL  where i have mentioned 'YOURSCHEMA'  in that SQL i got  some records but that schema it self was not found when i ran 'select *
    from qsys2.sysindexes
    where SYSTEM_INDEX_SCHEMA <> SYSTEM_TABLE_SCHEMA'



    ------------------------------
    jerry ven
    ------------------------------



  • 10.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 08:14 AM

    i was expecting this one would cover all records as i am not specifically providing any library name here and it will cover all such libraries as it's based on 'qsys2.sysindexes' but it appears it's not showing that library itself  for which i can fetch records by supplying 'YOURSCHEMA' in another SQL query.

    Thanks..



    ------------------------------
    jerry ven
    ------------------------------



  • 11.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 08:22 AM

    It's also getting to where I cannot understand if you are replying to me, Birgitta, or are combining both our solutions into one.



    ------------------------------
    Robert Berendt IBMChampion
    Business Systems Analyst, Lead
    Dekko
    Fort Wayne
    ------------------------------



  • 12.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 08:17 AM

    Can you reply with an sql example?  I'm not understanding what you are saying.

    Are you saying you tried

    'select *
    from qsys2.sysindexes
    where SYSTEM_INDEX_SCHEMA <> SYSTEM_TABLE_SCHEMA

      and system_table_schema = 'YOURSCHEMA'

    ?  If so, they just try it out first without that 'and'.  You may have it backward and need to try system_index_schema instead.  But, again, try it first without the 'and'



    ------------------------------
    Robert Berendt IBMChampion
    Business Systems Analyst, Lead
    Dekko
    Fort Wayne
    ------------------------------



  • 13.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 08:28 AM

    Yes, I had tried this SQL as advised on post number 3 here "select *
    from qsys2.sysindexes
    where SYSTEM_INDEX_SCHEMA <> SYSTEM_TABLE_SCHEMA"

    and when i compared it's output with SQL query advised in post number 5 then i found that schema which i had supplied in SQL query ( as per post 5) that schema itself  was not found when i searched it under where INDEX_SCHEMA = 'YOURSCHEMA' ( The same SCHEMA name which i had supplied as per post 5 SQL query)



    ------------------------------
    jerry ven
    ------------------------------



  • 14.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 08:37 AM

    and i thought SQL advised in post number 3 is better than post number 5 as in sql from post 3 we don't need to mention any specific library name ( 'YOURSCHEMA') and across system it will list down all such LFs.

    Thanks



    ------------------------------
    jerry ven
    ------------------------------



  • 15.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 09:18 AM

    Hi Jerry,

    a query on SYSINDEXES doesn't help so much, as it only contains SQL indexes and no traditional files.

    A combination of SYSTABLES and SYSTOOLS.RELATED_OBJECTS should do the job:

    select systables.system_table_schema, systables.system_table_name,
           related_objects.library_name, related_objects.system_name, related_objects.sql_object_type
    from qsys2.systables as systables
    join lateral (
         select *
         from table(
              systools.related_objects(systables.system_table_schema, systables.system_table_name)
         )
    ) as related_objects on true
    where systables.system_table_schema = '...your-lib...'
      and systables.table_type in ('P', 'T')
      and systables.system_table_schema <> related_objects.library_name;

    This should only give you rows with dependent objects which are not in the same library as the PF or table.

    HTH and kind regards,

    Daniel



    ------------------------------
    Daniel Gross
    #IBMChampion
    Senior Core Developer, Geis Group
    Pegnitz, Germany
    https://blog.qpgmr.de/
    ------------------------------



  • 16.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 09:05 AM

    I ran it with and without 'AND'  but could not find  that record where i supplied system_table_schema = 'My lib.' and it's appearing when i run sql query advised in post no. 5 here.

    Thanks...



    ------------------------------
    jerry ven
    ------------------------------



  • 17.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 09:19 AM

    not sure where i got this one:
    --  description: _logical-not-same-lib-as-pf.sql
    -- DBFTDP =  Dependency: D-data,V-view,I-indirect 
    -- 
    SELECT DBFFIL AS Physical_file,
           DBFLIB as Physical_file_lib, 
           DBFFDP as Logical_file, 
           DBFLDP AS LogLib_file_lib 
           ,i.*
    FROM QSYS.QADBFDEP i
    WHERE dbflib <> dbfldp 
    and DBFLIB  not in('QSYS');



    ------------------------------
    Bryan Dietz
    ------------------------------



  • 18.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 09:30 AM

    it's giving me an error "Not authorized to object QADBFDEP in QSYS type *FILE"  also in advice post no. 5 and 15 we need to supply library name manually which i want to avoid and want SQL query to track and list down all such LFs without providing library name there manually.

    Thanks..



    ------------------------------
    jerry ven
    ------------------------------



  • 19.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 09:36 AM

    Jerry - no, you don't need to supply a library in my example (post #15) - just try this:

    select systables.system_table_schema, systables.system_table_name,
           related_objects.library_name, related_objects.system_name, related_objects.sql_object_type
    from qsys2.systables as systables
    join lateral (
         select *
         from table(
              systools.related_objects(systables.system_table_schema, systables.system_table_name)
         )
    ) as related_objects on true
    where systables.table_type in ('P', 'T')
      and systables.system_table_schema <> related_objects.library_name;

    But be prepared, that this might run a while - depending on your machine.

    HTH

    Daniel



    ------------------------------
    Daniel Gross
    #IBMChampion
    Senior Core Developer, Geis Group
    Pegnitz, Germany
    https://blog.qpgmr.de/
    ------------------------------



  • 20.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 10:20 AM

    I am getting this error for post no. 19 

    Message ID : SQL0199

    Message: "Keyword WHERE not expected. valid tokens: <> = <> <=  !<

    Cause : The keyword WHERE was not expected here. A sysntax error was detected at keyword WHERE.

    Thanks..



    ------------------------------
    jerry ven
    ------------------------------



  • 21.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 10:26 AM
    Edited by jerry ven Mon February 17, 2025 10:36 AM

    in this example also (as per post no. 5) under where systables.system_table_schema = '...your-lib...'  we have to provide libary name manually which i want to avoid , so that when we run this sql query system automatically searches all such libraries and give us such LFs list .



    ------------------------------
    jerry ven
    ------------------------------



  • 22.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 10:56 AM

    If you want to loop through all libraries, just remove the WHERE Condition ... but do not complain about the query is running too long



    ------------------------------
    Birgitta Hauser
    Database and Software Engineer
    Birgitta Hauser - Modernization-Education-Consulting on IBM i (selfemployed)
    Kaufering
    +49 170 5269964
    ------------------------------



  • 23.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 02:06 PM

    So tried below SQL query for the same :

    "

    Select Table_Schema as PFTable_Schema, Table_Name as PFTable, 
           Schema_Name  as LFSchema,       SQL_Name   as LFSQLName, System_Name as LFSYSName
      from SysTables a cross join
           Lateral(Select *
                      from Table(Systools.Related_Objects(
                                          Library_Name => a.Table_Schema
                                          File_Name => a.Table_Name))
                      Where SQL_Object_Type like '%LOGICAL%'
                            and Schema_Name <> a.Table_Schema
                            )  

    "

    But i got this error : Message ID SQ20484

    Message: Parameter 2 required for routine RELATED_OBJECTES in SYSTOOLS.

    Cause: A Call statement for routine RELATED_OBJECTS in schema SYSTOOLS specified with a named parameter is not valid. Parameter 2 must be specified on the call statment because it has no default value.

    Recovery:  Specify a value for parameter 2.  Try the request again.

    So what should be the coorected SQL query here now?

    Thanks ...



    ------------------------------
    jerry ven
    ------------------------------



  • 24.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 02:28 PM

    Jerry,

    you have to reduce the SYSTABLES results to PFs and TABLEs:

    systables.table_type in ('P', 'T')

    Because other object types might not give the needed 2nd parameter to  RELATED_OBJECTS.

    Try that please - it should work.

    HTH

    Daniel



    ------------------------------
    Daniel Gross
    #IBMChampion
    Senior Core Developer, Geis Group
    Pegnitz, Germany
    https://blog.qpgmr.de/
    ------------------------------



  • 25.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 11:44 PM

    Could we please  have corrected complete  SQL query here ?

    Thanks...



    ------------------------------
    jerry ven
    ------------------------------



  • 26.  RE: LFs pointing to PFs from incorrect libraries

    Posted Tue February 18, 2025 01:10 AM

    #19 should work the way you described. 



    ------------------------------
    Daniel Gross
    #IBMChampion
    Senior Core Developer, Geis Group
    Pegnitz, Germany
    https://blog.qpgmr.de/
    ------------------------------



  • 27.  RE: LFs pointing to PFs from incorrect libraries

    Posted Tue February 18, 2025 04:32 AM

    for that i already replied in #20 what error i got and same error for below SQL as well where i have trimmed this SQL as per advice here:

    "select systables.system_table_schema, systables.system_table_name,
           related_objects.library_name, related_objects.system_name, related_objects.sql_object_type
    from qsys2.systables as systables
    join lateral (
         select *
         from table(
              systools.related_objects(systables.system_table_schema, systables.system_table_name)
         )
    ) as related_objects on true
    where systables.table_type in ('P', 'T')

    "

    Same error--- Message ID SQL0199

    Message : Keyword Where not expected......

    Thanks...



    ------------------------------
    jerry ven
    ------------------------------



  • 28.  RE: LFs pointing to PFs from incorrect libraries

    Posted Tue February 18, 2025 07:39 AM

    Jerry - I cannot confirm, that the query from #19 has any error:

    Screenshot of iACS running Query #19
    Maybe your system isn't up-to-date with TR's or PTF's. And I can only guess - probably your system doesn't support the Boolean keywords True and False, since they are somehow new.
    Please try to replace ON TRUE with ON 1=1 in query #19 - that should work in any case.
    And finally - we all are trying to help you - but most of us don't like doing someone else's "homework". So we at least expect some effort from your side.
    I hope that you can get the query running and it helps you.
    Kind regards,
    Daniel


    ------------------------------
    Daniel Gross
    #IBMChampion
    Senior Core Developer, Geis Group
    Pegnitz, Germany
    https://blog.qpgmr.de/
    ------------------------------



  • 29.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 10:20 AM

    This is exactly my example what I posted earlier, not sure why he did not try it



    ------------------------------
    Birgitta Hauser
    Database and Software Engineer
    Birgitta Hauser - Modernization-Education-Consulting on IBM i (selfemployed)
    Kaufering
    +49 170 5269964
    ------------------------------



  • 30.  RE: LFs pointing to PFs from incorrect libraries

    Posted Mon February 17, 2025 10:47 AM

    i tried it but here we need to supply library(schema) name manually -->"Where     a.Table_Schema = 'YOURSCHEMA' "  which will not automatically list down all such LFs and manually we might miss any library or schema name so wanted this process to be handeled automatically in SQL query itself.

    Thanks...



    ------------------------------
    jerry ven
    ------------------------------