• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Don’t Ignore the View

    April 22, 2009 Paul Tuohy

    I recently had a discussion with some of my colleagues about the increased use of Data Definition Language (DDL) in SQL to define a database as opposed to traditional DDS. One of the items that came up in the discussion was that while people were using DDL to define tables and indexes, there seemed to be very few views being defined.

    In this article I will discuss some of the benefits to be gained from using views–both within your applications and as a means to make data more readily accessible to your users.

    What Is a View?

    Those of us who come from the more traditional route on the system are inclined to think of a view as a non-keyed logical file. While this is true, it is also a little misleading since a view offers a lot more functionality then is found in a traditional logical file.

    Another way of looking at a view is to think of it as an SQL statement that exists as a permanent object. By thinking of it as an SQL statement you start to get some idea of what may be defined in a view. So let us look at some of the advantages to be gained in defining a view as opposed to a traditional logical file:

    • More join options are available. You may use inner joins, left outer joins, right outer joins, exception joins and cross joins.
    • Joins are not limited to columns being equal, and you may also specify joins based on derived columns.
    • You may define derived columns, the complexity of which far exceeds anything remotely possible in DDS. As well as normal expressions you also have full access to the SQL scalar functions. Therefore it is very easy to define a view that recasts or redefines the content of a column. We will see an example in a moment.
    • You may define selection criteria that, again, far exceed anything that is remotely possible in DDS. A view definition may make full use of an SQL where clause.
    • You may define a view that contains summary information, e.g., you could define a view that provides a summary of the sales as a region level. You can also make use of a having clause at the summary level.
    • You may define a view of a view.

    As you can see, these are features that go well beyond those offered by DDS. If there is a certain ring of familiarity about the above points, it is because they also aptly describe an SQL Select statement, which brings me full circle to thinking of a view as an SQL statement that exists as a permanent object.

    The Perceived Problem

    If views are so all powerful, why aren’t they being used more in our applications? The key (if you will pardon the pun) is that a view does not have a key. Therefore, we are inclined to think of them as un-usable in our traditional RPG or COBOL programs. How many non-keyed logical files do you have in your application?

    But views are not unusable. It is true that you cannot use a view in the same way you would a logical file; you cannot define it as a keyed file on an RPG F spec. You just have to use embedded SQL to access the view. And this is not such a bad thing when you consider the amount of work being done for you by the view.

    Using a View in an Application

    The following code shows an embedded SQL statement declaring a cursor for a select statement that selects a number of columns from four tables with selection criteria based on a host variable. Of course the host variables have to be defined to receive the column values from the subsequent Fetch statement.

    Declare C1 Scroll Cursor For
     select a.session, a.daynum, c.showseq, a.agendaid, a.room,
            b.shorttitle, d.day, d.dayname, c.fromtime, 
            c.totime
    
     from schedule a inner join sessions b
       on a.session = b.session
       inner join agenda c
       on a.event = c.event and
          a.daynum = c.daynum and
          a.agendaid = c.agendaid
       inner join daynums d
       on a.event = d.event and
          a.daynum = d.daynum
     where a.event = :ForEvent
    
     order by 1, 2, 3
     for read only;
    

    We can make life a little easier for ourselves by defining a view, as shown in the next piece of code. The select statement used in the creation of the view is the same as that used in the above code example, but without the where and order by clauses (although we could specify a where clause if required):

    Create View MyView as (
     select a.session, a.daynum, c.showseq, a.agendaid, a.room,
            b.shorttitle, d.day, d.dayname, c.fromtime, 
            c.totime
    
     from schedule a inner join sessions b
       on a.session = b.session
       inner join agenda c
       on a.event = c.event and
          a.daynum = c.daynum and
          a.agendaid = c.agendaid
       inner join daynums d
       on a.event = d.event and
          a.daynum = d.daynum); 

    With the view in place, the embedded SQL statement can now be simplified to that shown in the next piece of code. There is also the added benefit of having a *FILE object that may be used to define an externally defined data structure to be used as the host variable on the fetch.

    Declare C1 Scroll Cursor For
     select *
    
     from MyView
     where a.event = :ForEvent
    
     order by 1, 2, 3
     for read only;
    

    Make Data More Accessible

    The use of views can make any data we have defined on our system that much easier for users to decipher. Say we have a DDS defined physical file that contains an employee ID, name, and date of birth. The date of birth is defined as a numeric field. To make the data that little bit more accessible we can define the view shown in the code below, which redefines the numeric field as a proper date field.

    CREATE VIEW EMPVIEWA ( EMPNO , NAME , BIRTHDATE )
    AS SELECT EMPNO, NAME,
    DATE( SUBSTR( CHAR(DATEBIRTH) ,1 ,4 ) || '-'
    || SUBSTR( CHAR(DATEBIRTH) ,5 ,2 ) || '-' 
    || SUBSTR( CHAR(DATEBIRTH) ,7 ,2 ) )   FROM EMPLOYEES ;  
    

    But why stop there? Now that we have the basic view in place, we define another view of that view, which shows us the age of the employee. For instance:

    CREATE VIEW EMPVIEWB ( EMPID , NAME , BIRTHDATE , AGE )
      AS SELECT EMPNO, NAME, BIRTHDATE, 
                YEAR( CURDATE( ) - BIRTHDATE)
         FROM EMPVIEWA ;
    

    This gives you an idea of how views may be used to re-define and extend even DDS defined files and also provides a simple example of defining a view of a view.

    Getting Started With Views

    If the thought of having to learn all that DDL syntax for views seems a little bit daunting never fear: iSeries Navigator to the rescue. Navigator offers a GUI intuitive interface to defining views, as shown in Figure 6. I’m sure you recognize a join logical when you see one! Just click on the Show SQL button at any stage to see the DDL you are defining.

    Defining a View in Navigator.

    And Finally. . .

    A quick word about performance: since views are not keyed (i.e., they do not have an access path associated with them), they do not incur maintenance overhead. The performance overhead is only incurred when the view is used–at which point the Query Engine determines which index best provides the requested data.

    So, if you have not already done so, please go have a play with views. Ignore them and you may lose out.

    Have fun!

    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.



                         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
    Midrange Dynamics North America

    With MDRapid, you can drastically reduce application downtime from hours to minutes. Deploying database changes quickly, even for multi-million and multi-billion record files, MDRapid is easy to integrate into day-to-day operations, allowing change and innovation to be continuous while reducing major business risks.

    Learn more.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    Essex Technology Group:  AS/400 to Power & Beyond Roadshow with Dr. Frank Soltis, May 18-21
    Halcyon Software:  Do more with less resource with Message Management
    COMMON:  Join us at the 2009 annual meeting and expo, April 26-30, Reno, Nevada

    IT Jungle Store Top Book Picks

    Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
    The iSeries Express Web Implementer's Guide: List Price, $49.95
    The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
    The System i Pocket RPG & RPG IV Guide: List Price, $69.95
    The iSeries Pocket Database Guide: List Price, $59.00
    The iSeries Pocket SQL Guide: List Price, $59.00
    The iSeries Pocket Query Guide: List Price, $49.00
    The iSeries Pocket WebFacing Primer: List Price, $39.00
    Migrating to WebSphere Express for iSeries: List Price, $49.00
    Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
    Getting Started with WebSphere Express for iSeries: List Price, $49.00
    Can the AS/400 Survive IBM?: List Price, $49.00
    Chip Wars: List Price, $29.95

    DataDirect Updates ODBC Drivers IBM Launches Power6+ Servers–Again

    One thought on “Don’t Ignore the View”

    • Tom Tufankjian says:
      February 10, 2021 at 1:01 pm

      How did you get to the GUI to define the view?

      Reply

    Leave a Reply Cancel reply

Volume 9, Number 14 -- April 22, 2009
THIS ISSUE SPONSORED BY:

WorksRight Software
Profound Logic Software
Halcyon Software

Table of Contents

  • Don’t Ignore the View
  • Releasing File Member Locks With QSH
  • Trouble-Shooting i5/OS Printer Problems in a Warehouse Environment

Content archive

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

Recent Posts

  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23

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