Trigers

pts.
Tags:
AS/400
DB2 Universal Database
RPG
Can anyone give me an example of using a trigger when a record is inserted into a file? I am using RPGLE.

Answer Wiki

Thanks. We'll let you know when a new response is added.

I am unclear on what exactly you want to know. The general process is to 1) write the RPGLE program that contains the instructions you want to execute when a record is added to the PF; 2) compile the program; 3) attach the RPGLE object to the physical file using the CHGPF command or the ADDPFTRG command (you must have the proper object authority on the PF to do this and I believe the PF cannot already be locked by another user); 4) in the CL command parameters identify when you want the trigger to fire (*BEFORE *INSERT and/or *AFTER INSERT).

It has been my experience that you will encounter fewer authority problems if the owner of the trigger program is the same as the owner of the PF (or at least has *ALL authority on the PF object).

===========================================================

The <a href=”http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp”>IBM Information Center</a> is where you should be looking for information on triggers and for examples, not here. You should come here for explanation of parts that you don’t understand or for help when IBM examples don’t work for you. Go look where you are supposed to look and get solid information.

One place you would have found sooner is the <a href=”http://www.redbooks.ibm.com/abstracts/sg246503.html”>Stored Procedures, Triggers, and User-Defined Functions on DB2 Universal Database for iSeries</a> RedBook. That’s where you’ll find every related example you’ll need.

Tom

Discuss This Question: 3  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • RSILVA
    Here's an example of an Before Insert trigger of the file PFILE, in RPGLE, used to calculate the next sequencial number, which is the unique key of the LFBYNUM logical file. *========================================================== * Logical file by sequnecial number of * physical file being triggered *==========================================================FLFBYNUM IF E K DISK prefix(F_) *======================================================== * Data Structures *======================================================== *--------------------------------------------------------- * External file DS of the physical file being triggered * PFILE record format *--------------------------------------------------------- D FileDS E DS extname(PFILE:FORMAT) D based(pBuffer) *-------------------------------------------------------- * Buffer received *--------------------------------------------------------- D@Buffer DS D T@PFName 1 10 D T@LibName 11 20 D T@MbrName 21 30 D T@TriggerE 31 31 D T@TriggerT 32 32 D T@Commit 33 33 D T@Reserv1 34 36 D T@CCSID 37 40B 0 D T@Reserv2 41 48 D T@OldOff 49 52B 0 D T@OldLen 53 56B 0 D T@OldOffN 57 60B 0 D T@OldLenN 61 64B 0 D T@NewOff 65 68B 0 D T@NewLen 69 72B 0 D T@NewOffN 73 76B 0 D T@NewLenN 77 80B 0 D T@Reserv3 81 96 D T@Data 97 37000 *--------------------------------------------------------- * Buffer length *---------------------------------------------------------D@Length DS D Len 1 4B 0 *--------------------------------------------------------- * Buffer pointer *--------------------------------------------------------- D pBuffer S * *========================================================= * Program Parameters *=================================================== C *ENTRY PLIST C @Buffer parm @Buffer C @Length parm @Length *========================================================= * MAIN PROGRAM *========================================================= C eval pBuffer = %ADDR(@Buffer) + T@NewOff C exsr CalcNum_Seq C eval *inlr = *ON ****************************************** * Each new record is assigned a unique seq * Num_Seq ****************************************** C CalcNum_Seq BEGSR C eval F_Num_Seq = 0 C *END setll LFBYNUM C readp LFBYNUM C eval Num_Seq = F_Num_Seq + 1 C ENDSR
    0 pointsBadges:
    report
  • Hex29a
    How does one determine the to value (37000) for T@Data? T@Data 97 37000 How is the size of Len determined? D Len 1 4B 0 Len doesn?t seem to be used anywhere?
    0 pointsBadges:
    report
  • JPLamontre
    DATA length 37000 is a maximum not depending the file the trigger is attached. Usefull length is depending on attachment, and DB2 give you the information : D T@OldOff 49 52B 0 D T@OldLen 53 56B 0 D T@OldOffN 57 60B 0 D T@OldLenN 61 64B 0 D T@NewOff 65 68B 0 D T@NewLen 69 72B 0 D T@NewOffN 73 76B 0 D T@NewLenN 77 80B 0 D T@Data 97 37000 usefulldatalen = oldlen + oldlenN + newlen + newlenN ie, the before image + the after image of the record you are working with.
    0 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following