Wednesday, December 17, 2014

Using aliases can be simple

using alias in rpg

The discussion of whether to use the alias names for files’ fields has become a lengthy here amongst the programming staff. The major argument against has been that there does not appear to be an easy way to perform input or output to record formats in display and printer files. I needed to find a simpler way to define the alias and its uses.

Fortunately IBM came to my aid with PTFs to make the using of alias simple in IBM i 7.1 and 7.2. In my opinion this is the way alias handling should have been from the start.

If you are still using 6.1 you will have to use the method described in my post Using Alias for longer field names.

You will need these PTF(s) applied to your IBM i before you can do what I am going to describe:

Release PTF id
7.1 SI54502
7.2 SI54155
For TGTRLS(*PRV)   SI54521

I find that the quickest way to check if a PTF is present and applied is using the method described in Quick way to find if PTF present and applied.

In this example I am going to use the following three files. I am going to list their source at the bottom of this post so not to clutter this part, you can view them by clicking on the links below:

Thank to these PTFs using aliases is easy. All I have to do is add the keyword ALIAS to the file definitions. There are no data structures needed, unless you want to use them, for file input/update. I can perform input and output at the record format level. For display and printer files this makes using aliases so easy everyone can do it.

Below is my example program. I am only giving an "all free" RPG example as this PTF is only available after the introduction of the "all free" RPG in TR7.

01  ctl-opt option(*nodebugio:*srcstmt:*nounref) ;

02  dcl-ds PgmDs
03    extname('RPG4DS') psds qualified
04  end-ds ;

05  dcl-f TESTDSPF workstn indds(IndDs) alias ;
06  dcl-ds IndDs qualified ;
07    F3_Exit ind pos(3) ;
08    ErrorInvoice ind pos(50) ;
09  end-ds ;

10  dcl-f TESTFILE keyed alias ;
11  dcl-f TESTPRTF printer alias ;

12  Z1_SCREEN_ID = %trimr(PgmDs.ProcNme) + '-1' ;

13  dow (1 = 1) ;
14    exfmt SCREEN ;
15    if (IndDs.F3_Exit) ;
16      leave ;
17    endif ;

18    if (Z1_INVOICE_NUMBER = ' ') ;
19      IndDs.ErrorInvoice = *on ;
20      iter ;
21    endif ;

23    if not(%equal) ;
24      IndDs.ErrorInvoice = *on ;
25      iter ;
26    endif ;

27    write PRHEADER ;

28    dow (2 = 2) ;
30      if (%eof) ;
31        leave ;
32      endif ;

33      ITEM_DESCRIPTION = 'Description for ' + ITEM ;

35      write PRDETAIL ;
36    enddo ;

37    write PREND ;
38    leave ;
39  enddo ;

40  *inlr = *on ;

You can see where I have defined the ALIAS keyword in the file definitions on lines 5, 10, and 11. For those of you who have not seen it before I use a Indicator Area data structure to interaction of indicators to and from the display file, on lines 6 – 9. You can learn more about how I use an Indicator Area data structure in the post No more numbered indicators.

On lines 12, 18, 22, 29 you can see where I have used the alias name from the display file field rather than its field name. For those of you wondering why I validate using a SETLL rather than a CHAIN read the post Validation: CHAIN versus SETLL.

A big difference with the example program in the previous post about using alias versus this one is that when I read the TESTFILE I am using record format name rather than the file's name, and there is no data structure to receive the input.

Lines 33 and 34 show I can simply use the alias names from TESTFILE to fill the alias fields, ITEM_DESCRIPTION and EXTENDED_PRICE, from the printer file format PRDETAIL.

In my opinion this all so straight forward and easy no-one has a reasonable excuse not use the alias names, if they are present, for files' fields. And why it is now a good idea to add alias names to any new files (physical, display, and printer) you create.

TESTDSPF – Display file

A                                      DSPSIZ(24 80 *DS3)
A                                      PRINT
A                                      ERRSFL
A                                      INDARA
A          R SCREEN
A                                      CA03(03 'F3=Exit')
A                                  1  2USER
A                                      COLOR(BLU)
A                                  1 31'R P G P G M . C O M'
A                                      DSPATR(HI)
A                                  1 63TIME
A                                      COLOR(BLU)
A                                  1 72DATE
A                                      EDTCDE(Y)
A                                      COLOR(BLU)
A            Z1SCREEN      12A  O  2  2COLOR(BLU)
A                                      ALIAS(Z1_SCREEN_ID)
A                                  2 30'Enter invoice to print'
A                                      DSPATR(HI)
A                                  2 72SYSNAME
A                                      COLOR(BLU)
A                                  3  2'                                  -
A                                                                         -
A                                               '
A                                      COLOR(BLU)
A                                      DSPATR(UL)
A                                  5  3'Invoice  .'
A            INVNBR    R        B  5 14REFFLD(INVNBR  TESTFILE)
A                                      DSPATR(HI)
A                                      ALIAS(Z1_INVOICE_NUMBER)
A  50                                  ERRMSG('Invoice not found. Try agai-
A                                      n')
A                                 23  3'F3=Exit'
A                                      COLOR(BLU)


TESTFILE – Physical file

A                                      UNIQUE
A          R TESTFILER
A            INVNBR        10A         TEXT('Invoice number')
A                                      COLHDG('Invoice' 'No.')
A                                      ALIAS(INVOICE)
A            LINENBR        3P 0       TEXT('Invoice line number')
A                                      COLHDG('Line' 'No.')
A                                      ALIAS(INVOICE_LINE)
A            ITEM          15A         TEXT('Item number')
A                                      COLHDG('Item')
A            LINEQTY        7P 0       TEXT('Line quantity')
A                                      COLHDG('Line' 'Qty')
A                                      ALIAS(LINE_QUANTITY)
A            UNITPRICE      7P 2       TEXT('Unit price')
A                                      COLHDG('Unit' 'price')
A                                      ALIAS(UNIT_PRICE)
A            DUEDTE          L         TEXT('Due date')
A                                      COLHDG('Due' 'date')
A                                      ALIAS(LINE_DUE_DATE)
A                                      DATFMT(*USA)
A            SHIPPED        1A         TEXT('Line shipped flag')
A                                      COLHDG('Line' 'shipped')
A          K INVNBR
A          K LINENBR


TESTPRTF – Printer file

A                                      REF(TESTFILE)
A                                      INDARA
A          R PRHEADER
A                                     1'TESTRPG'
A                                      SKIPB(001)
A                                    +2DATE
A                                      EDTCDE(Y)
A                                   124'Page'
A                                   129PAGNBR
A                                      EDTCDE(Z)
A                                     1' '
A                                      SPACEB(001)
A          R PRDETAIL
A            LINENBR   R        O     1EDTCDE(Z)
A                                      SPACEB(001)
A            ITEM      R        O    +2
A            ITEMDESC      25A       +2ALIAS(ITEM_DESCRIPTION)
A            LINEQTY   R        O    +2EDTCDE(J)
A            UNITPRICE R        O    +2EDTCDE(J)
A            EXTPRICE  R        O    +2REFFLD(UNITPRICE)
A                                      EDTCDE(J)
A                                      ALIAS(EXTENDED_PRICE)
A            DUEDTE    R             +2
A            SHIPPED   R             +2
A          R PREND
A                                    47
A                                      '* * *   E N D   O F   R E P O R T -
A                                        * * *'
A                                      SPACEB(002)


You can learn more about the alias from IBM's developerWorks website here.


This article was written for IBM i 7.2, and it should work with 7.1 TR7 and greater too.


  1. Now for the hard part .......... getting my organization to jump in pool and start using these modern techniques. I am still trying to teach them how to spell SQL.

    1. I fully understand your plight.Here we have those who claim "I can do everything you do using RPG III", who do not give anything developed after 2000 a look.

      I keep saying we ought to "upgrade" their PCs to Windows 95 as they are stuck in that era

    2. So funny and so true

  2. Simon, thanks for sharing . You’re correct long names can be a problem the “alias“ key word is a great solution.
    Great read and thanks for another great teaching moment .

  3. Too good to know this one, thanks for your sharing such a creespy and important article


To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.