• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Find A View Of A View Of A View. . .

    October 8, 2014 Paul Tuohy

    Note: The code accompanying this article is available for download here.

    SQL’s Data Definition Language (DDL) offers many great features, one of which is the ability to define a view of a view. This can lead to simple or complex structures making use of views of views of views of. . . you get the idea.

    But one of the difficulties with this technique is that, once created, it is difficult to determine the depth of dependencies for views. The Display Database Relations (DSPDBR) command will tell you which views are immediately dependent on a view but it will not tell you what views are dependent on those views.

    In this article I will introduce you to a stored procedure that, given the name of a table or a view, will provide the full list of dependent views and all of their dependents and all of their dependents, etc.

    Setting The Scene

    The following piece of code shows a list of a table (BASESALE) and a number of dependent views. The BASESALE_L1 views are created over the BASESALE table. The BASESALE_L2 views are created over the BASESALE_L1 views and the BASESALE_L3 views are created over the BASESALE_L2 views. The script to create this table and the dependent views can be found here.

    BASESALE     Base Sales Data
    B_L1USE      Sales for Region USE
    B_L1USW      Sales for Region USW
    B_L2USE1     Sales for Region USE, Division 001
    B_L2USE2     Sales for Region USE, Division 002
    B_L2USW1     Sales for Region USW, Division 001
    B_L2USW2     Sales for Region USW, Division 002
    B_L3USE1_S   Sales for Region USE, Division 001, Summary
    B_L3USE2_S   Sales for Region USE, Division 002, Summary
    B_L3USW1_S   Sales for Region USW, Division 001, Summary
    B_L3USW2_S   Sales for Region USW, Division 002, Summary
    

    An attempt to delete the table BASESALE using the Delete File (DLTF) command will fail because the file has dependents. The DSPDBR command will list the 10 dependent views, which is what I need. As an aside, the Show Related option in System i Navigator only shows the two immediately dependent views.

    But what happens if we decide to use the SQL DROP statement or decide to delete one of the dependent views (and you have to delete a view if you want to re-create it). Here are the problems we will confront in this scenario:

    • Using the SQL DROP statement to delete the table BASESALE, will, by default, delete the table and all of the views.
    • Using the SQL DROP statement to delete the view B_L1USE, will, by default, delete the views B_L2USE1, B_L2USE2, B_L3USE1_S and B_L3USE2_S.
    • An attempt to delete one of the views using the DLTF command will fail because the file has dependents but the DSPDBR command will only list views that are immediately dependent on the view being deleted. For example, the DSPDBR for view B_L1USE only shows B_L2USE1 and B_L2USE2 as dependents. It does not show the further dependents of B_L3USE1_S and B_L3USE2_S.

    So, how can we determine which other views are affected when a view is deleted? Introducing the SQL stored procedure GET_VIEW_DEPENDENTS.

    Using GET_VIEW_DEPENDENTS

    The GET_VIEW_DEPENDENTS takes two parameters: the system name (10 characters) of a table or view and a schema. For example:

    CALL GET_VIEW_DEPENDENTS ('BASESALE', 'TESTSTUFF');
    

    A call to the procedure results in a result set listing all dependents, as shown in the code below. The result set shows the SQL name, text description, system table name and system schema name.

    B_L1USE     Sales for Region USE                         B_L1USE    
    TESTSTUFF
    B_L1USW     Sales for Region USW                         B_L1USW    
    TESTSTUFF
    B_L2USE1    Sales for Region USE, Division 001           B_L2USE1   
    TESTSTUFF
    B_L2USE2    Sales for Region USE, Division 002           B_L2USE2   
    TESTSTUFF
    B_L2USW1    Sales for Region USW, Division 001           B_L2USW1   
    TESTSTUFF
    B_L2USW2    Sales for Region USW, Division 002           B_L2USW2   
    TESTSTUFF
    B_L3USE1_S  Sales for Region USE, Division 001, Summary  B_L3USE1_S 
    TESTSTUFF
    B_L3USE2_S  Sales for Region USE, Division 002, Summary  B_L3USE2_S 
    TESTSTUFF
    B_L3USW1_S  Sales for Region USW, Division 001, Summary  B_L3USW1_S 
    TESTSTUFF
    B_L3USW2_S  Sales for Region USW, Division 002, Summary  B_L3USW2_S 
    TESTSTUFF
    

    The code above shows the same information as the DSPBBR command for table BASESALE, with the added benefit that we see the text description. But GET_VIEW_DEPENDENTS comes into its own when we provide the name of a view.

    Now look at this next snippet of code, which shows the result of calling GET_VIEW_DEPENDENTS for the view B_L1USE:

    B_L2USE1    Sales for Region USE, Division 001           B_L2USE1   
    TESTSTUFF
    B_L2USE2    Sales for Region USE, Division 002           B_L2USE2   
    TESTSTUFF
    B_L3USE1_S  Sales for Region USE, Division 001, Summary  B_L3USE1_S 
    TESTSTUFF
    B_L3USE2_S  Sales for Region USE, Division 002, Summary  B_L3USE2_S 
    TESTSTUFF
    

    The GET_VIEW_DEPENDENTS Procedure

    As one might expect, the information we are looking for may be found in the system catalog. The table QSYS/QADBFDEP contains all of the dependencies of views upon views and the table SYSY/QADBXREF contains corresponding text descriptions. GET_VIEW_DEPENDENTS uses a recursive query to perform a recursive retrieve of a table or view and its dependents. An excellent description of recursive queries may be found in Michael Sansoterra’s article Recursive Queries on the iSeries and System i.

    The following code shows the CREATE statement for the GET_VIEW_DEPENDENTS procedure.

    CREATE PROCEDURE GET_VIEW_DEPENDENTS ( 
    	IN DBNAME CHAR(10) , 
    	IN DBLIBRARY CHAR(10) ) 
    	DYNAMIC RESULT SETS 1 
    	LANGUAGE SQL 
    	SPECIFIC GET_VIEW_DEPENDENTS 
    	NOT DETERMINISTIC 
    	READS SQL DATA 
    	CALLED ON NULL INPUT 
    	PROGRAM TYPE SUB 
    	SET OPTION  ALWBLK = *ALLREAD , ALWCPYDTA = *OPTIMIZE , 
    	    COMMIT = *NONE , CLOSQLCSR = *ENDMOD , 
    	    DECRESULT = (31, 31, 00) , DFTRDBCOL = *NONE , 
    	    DYNDFTCOL = *NO , DYNUSRPRF = *USER , SRTSEQ = *HEX   
    BEGIN 
    	DECLARE C1 CURSOR FOR 
    	    WITH RECURSIVE DEPENDENTS ( CALLLEVEL , DBFFIL , DBFLIB , 
                                            DBFFDP , DBFLDP ) AS 
            ( SELECT 1 AS CALLLEVEL , DBFFIL , DBFLIB , DBFFDP , DBFLDP 
                 FROM QADBFDEP 
                 WHERE DBFFIL = DBNAME AND DBFLIB = DBLIBRARY 
              UNION ALL 
              SELECT CALLLEVEL + 1 AS CALLLEVEL , 
                     PR.DBFFIL , PR.DBFLIB , PR.DBFFDP , PR.DBFLDP 
                 FROM DEPENDENTS PH INNER JOIN QADBFDEP PR 
                      ON ( PH.DBFFDP , PH.DBFLDP ) = ( PR.DBFFIL , 
    				  PR.DBFLIB ) 
            ) 
            SELECT DISTINCT DBXLFI, DBXTXT, DBFFDP , DBFLDP 
               FROM DEPENDENTS INNER JOIN QADBXREF
                    ON (DBFFDP , DBFLDP) = (DBXFIL , DBXLIB)
               ORDER BY DBFFDP , DBFLDP 
               FOR READ ONLY ; 
      
            OPEN C1 ; 
      
            SET RESULT SETS CURSOR C1 ; 
    END  ;      
    

    Another Tool in the Belt

    I hope you find the GET_VIEW_DEPENDENTS procedure as useful as I do. More than once it has saved me from causing havoc when dropping a view.

    Paul Tuohy is CEO of ComCon, an iSeries consulting company, and is one of the co-founders of System i Developer, which hosts the RPG & DB2 Summit conferences. He is an award-winning speaker who also speaks regularly at COMMON conferences, and is the author of “Re-engineering RPG Legacy Applications,” “The Programmers Guide to iSeries Navigator,” and the self-study course called “iSeries Navigator for Programmers.” Send your questions or comments for Paul to Ted Holt via the IT Jungle Contact page.

    RELATED STORIES

    A View of a View of a View

    Don’t Ignore the View

    Recursive Queries on the iSeries and System i



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Maxava

    Migrate IBM i with Confidence

    Tired of costly and risky migrations? Maxava Migrate Live minimizes disruption with seamless transitions. Upgrading to Power10 or cloud hosted system, Maxava has you covered!

    Learn More

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    BCD:  Webinar: Deliver First-Rate IBM i Mobile Applications Quickly. October 16
    System i Developer:  Upgrade your skills at the RPG & DB2 Summit in Minneapolis, Sept 30 - Oct 2.
    MiNET:  Automate PDF, email, Fax, Archiving & more with ArtForm400. Try us for a FREE Redbox code!

    More IT Jungle Resources:

    System i PTF Guide: Weekly PTF Updates
    IBM i Events Calendar: National Conferences, Local Events, and Webinars
    Breaking News: News Hot Off The Press
    TPM @ EnterpriseTech: High Performance Computing Industry News From ITJ EIC Timothy Prickett Morgan

    IBM i Shops Turn to Vault400 for Protection App Dev, Database Top IBM i TR9 and TR1 Enhancements

    Leave a Reply Cancel reply

Volume 14, Number 23 -- October 8, 2014
THIS ISSUE SPONSORED BY:

Bug Busters Software Engineering
WorksRight Software
Shield Advanced Solutions

Table of Contents

  • Find A View Of A View Of A View. . .
  • A Bevy Of BIFs: Updates
  • Admin Alert: A Skeleton Utility Program For Controlling IBM i Job Queues

Content archive

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

Recent Posts

  • Public Preview For Watson Code Assistant for i Available Soon
  • COMMON Youth Movement Continues at POWERUp 2025
  • IBM Preserves Memory Investments Across Power10 And Power11
  • Eradani Uses AI For New EDI And API Service
  • Picking Apart IBM’s $150 Billion In US Manufacturing And R&D
  • FAX/400 And CICS For i Are Dead. What Will IBM Kill Next?
  • Fresche Overhauls X-Analysis With Web UI, AI Smarts
  • Is It Time To Add The Rust Programming Language To IBM i?
  • Is IBM Going To Raise Prices On Power10 Expert Care?
  • IBM i PTF Guide, Volume 27, Number 20

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