ORACLE trigger usage notes

Source: Internet
Author: User
Syntax Rules: Create [or replace] trigger [mode.] trigger name

Before | after insert | Delete | (update of column name)

On Table Name

[For each row]

When Condition

PL/SQL Block

Note:

The meaning of for each row is that in a statement that operates a table, each row that succeeds is triggered once. If you do not write a row that is a table-Level Trigger, no matter how many rows are operated, trigger only once;

The emergence of the when condition indicates that the trigger may be triggered during DML operations, but the trigger may not necessarily do the actual work. For example, when the condition after the when is not true, the trigger simply skips the PL/SQL block;

Example:

SQL Code

  1. Create Or Replace TriggerWf_tri_user_list beforeInsert Or Update Or Delete OnUser_list
  2. ForEach row
  3. Declare
  4. UID varchar2 (10); useq varchar2 (10); asql varchar2 (200); namea varchar2 (200); nameb varchar2 (200 );
  5. Begin
  6. Namea: =Null;
  7. Nameb: =Null;
  8. If insertingThen
  9. Insert IntoWflow. bpm_org_user (userid, username, diaplayname, SEQ)Values(: New. user_id,: New. user_name,: New. user_realname,: New. user_id );
  10. Dbms_output.put_line ('Insert Trigger IsChufale .....');
  11. EndIf;
  12. If updatingThen
  13. If (: New. user_name <>: Old. user_name)And(: New. user_realname <>: Old. user_realname)Then
  14. Namea: =: New. user_name;
  15. Nameb: =: New. user_realname;
  16. Asql: ='UpdateWflow. bpm_org_userSetDiaplayname =: 1WhereUsername =: 2 ';
  17. ExecuteImmediate asql using namea, nameb;
  18. Else
  19. If: New. user_name <>: Old. user_nameThen
  20. Namea: =: New. user_name;
  21. Asql: ='UpdateWflow. bpm_org_userSetUser_name =: 1WhereUsername =: 2 ';
  22. ExecuteImmediate asql using namea;
  23. Else
  24. If: New. user_realname <>: Old. user_realnameThen
  25. Nameb: =: New. user_realname;
  26. Asql: ='UpdateWflow. bpm_org_userSetDiaplayname =: 1WhereUsername =: 2 ';
  27. ExecuteImmediate asql using nameb,: Old. user_id;
  28. EndIf;
  29. EndIf;
  30. EndIf;
  31. EndIf;
  32. If deletingThen
  33. UpdateWflow. bpm_org_jobusersSetUserid = 0Where: Old. user_id = useridAndParentid =-1;
  34. Delete FromWflow. bpm_org_jobusersWhereUserid =: Old. user_id;
  35. DeleteWflow. bpm_org_userWhereUserid =: Old. user_id;
  36. EndIf;
  37. Commit;
  38. End;

 

Keywords:

: New and: Old use methods and meanings. New only appears in insert and update, and old only appears in update and delete. New indicates the row data to be inserted during insert, new indicates the new data to be replaced during update, and old indicates the original row to be changed, old indicates the data to be deleted.

Note:

Commit cannot be used in triggers.

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.