× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



I'm somewhat new to RPG and AS/400 programming in general, and I'm attempting to write a trigger program in RPG ILE with embedded SQL. I've been digging through the archives and found lots of info about the trigger buffer data structure. First I mistakenly tried to use the new version (with I type members) before realizing that the structure was changed in V5R1. My trigger should be *INSERT and *BEFORE on my CUSTOMERS file and the functionality I'm trying to accomplish is to find the maximum value in the CUSTID column or 0 if there are none, add 1 to it, and use this value as the CUSTID for the insert. CUSTID is the physical file's key and it is defined as UNIQUE. Below is my code to accomplish this task, I can't seem to figure out why it does not do what it is supposed to do. (It gives me a duplicate key error when I attempt to insert, so I assume that it is not modifying the value properly. I have attached the trigger using the ADDPFTRG command.) Sorry for the long message.

==TRGHDR==


     DTRGBUFFER        DS
     D TFILENAME                     10
     D TLIBNAME                      10
     D TMEMNAME                      10
     D TTRGEVENT                      1
     D TTRGTIME                       1
     D TCOMMITLOCK                    1
     D TFILLER1                       3
     D TCCSID                         4B 0
     D TFILLER2                       4
     D TFILLER3                       4
     D TOLDRECOFF                     4B 0
     D TOLDRECLEN                     4B 0
     D TOLDNULLOFF                    4B 0
     D TOLDNULLLEN                    4B 0
     D TNEWRECOFF                     4B 0
     D TNEWRECLEN                     4B 0
     D TNEWNULLOFF                    4B 0
     D TNEWNULLLEN                    4B 0
     D RESERVED                      16
      *
     DTRGBUFFERLEN     S              4B 0

==CUSTOMERK==

     D*
     D/COPY QRPGLESRC,TRGHDR
     D*
     D* Record format for New and Old Records
     D*
     D OldRecord     E DS                  ExtName(CUSTOMERS)
     D                                     Prefix(O)
     D                                     Based(OldRecPtr)
     D*
     D NewRecord     E DS                  ExtName(CUSTOMERS)
     D                                     Prefix(N)
     D                                     Based(NewRecPtr)
     D*
     D* Record Layout for New and Old Null Maps.
     D*
     D OldNullMap      DS                  Based(OldNullPtr)
     D ONullFld                       1
     D NewNullMap      DS                  Based(NewNullPtr)
     D NNullFld                       1
     D*
     C     *ENTRY        PLIST
     C                   PARM                    TrgBuffer
     C                   PARM                    TrgBufferLen
     C*
C EVAL OldRecPtr = %ADDR(TrgBuffer) + TOldRecOff C EVAL NewRecPtr = %ADDR(TrgBuffer) + TNewRecOff
     C*
C EVAL OldNullPtr = %ADDR(TrgBuffer) + TOldNullOff C EVAL NewNullPtr = %ADDR(TrgBuffer) + TNewNullOff
     C*
     C/EXEC SQL
C+ SELECT COALESCE(MAX(CUSTID), 0) + 1 INTO :NCUSTID FROM CUSTOMERS
     C/END-EXEC
     C*
     C                   EVAL      NNullFld = *OFF
     C                   EVAL      *INLR = '0'
     C                   RETURN

Thanks!

Kyle Goodwin

As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.