How to Create a trigger for a DB2 database

Source: Internet
Author: User

InDB2 databaseOperation, we often useTriggerThis seems a little difficult for beginners. But it doesn't matter. This article introduces some knowledge about DB2 database triggers and provides an instance for creating triggers. For Beginners, if you don't know much about them, you can also directly apply the following instances to create a trigger. Next let's take a look at this part.

Create a trigger that includes the following parts:

Trigger name :......

Trigger trigger event: insert, delete, update

Activation time: before, after

Granularity: for each statement, for each row

Transition variable:

Old row: indicates the modified value before the event is triggered:

New row indicates the value modified after the event is triggered.

Old table indicates a read-only hypothetical table of all modified rows before the event is triggered.

New table indicates a hypothetical table in which all rows are modified after the event is triggered.

Trigger condition: starting from WHEN, it can contain one or more predicates, including transition variables and subqueries.

Trigger body: it consists of one or more SQL statements.

Create an instance of the DB2 trigger:

 
 
  1. CREATE TRIGGER REORDER  
  2.  
  3. AFTER UPDATE OF ON_HAND, MAX_STOCKED ON PARTS  
  4.  
  5. REFERENCING NEW AS N_ROW  
  6.  
  7. FOR EACH ROW MODE DB2SQL  
  8.  
  9. WHEN (N_ROW.ON_HAND < 0.10 * N_ROW.MAX_STOCKED  
  10.  
  11. AND N_ROW.ORDER_PENDING = 'N')  
  12.  
  13. BEGIN ATOMIC  
  14.  
  15. VALUES(ISSUE_SHIP_REQUEST(N_ROW.MAX_STOCKED -  
  16.  
  17. N_ROW.ON_HAND,  
  18.  
  19. N_ROW.PARTNO));  
  20.  
  21. UPDATE PARTS SET PARTS.ORDER_PENDING = 'Y' 
  22.  
  23. WHERE PARTS.PARTNO = N_ROW.PARTNO;  
  24.  
  25. END 

The introduction of the DB2 database trigger is here. I hope this introduction will be helpful to you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.