Oracle 行触发与语句触发


Oracle 行触发与语句触发的区别

1、行触发器有 for each row子句。语句触发器没有for each row 子句。
2、行触发器,可以有 when 作为触发限制,可以使用new/old。语句触发器不能有when 作为触发限制。
3、行触发器:对应DML语句所影响到的表中的每一行,触发器都要执行一遍。
4、语句触发:对应DML语句所影响到的表中的所有行,触发器只执行一遍。

例子:

  1. --测试表   
  2. create table wdt_test(test number(20));   
  3.   
  4. --日志表   
  5. create table wdt_log(log_no number(20), log_date date);   
  6.   
  7. --触发器   
  8. create or replace trigger buf_wdt_test   
  9.     before update on wdt_test   
  10.     --for each row   
  11. declare  
  12.     ln_log_no wdt_log.log_no%type default 0;   
  13. begin  
  14.   
  15.     select nvl(max(log_no), 0) + 1   
  16.       into ln_log_no   
  17.       from wdt_log;   
  18.   
  19.     insert into wdt_log   
  20.         (log_no,   
  21.          log_date)   
  22.     values  
  23.         (ln_log_no,   
  24.          sysdate);   
  25. end;   
  26.   
  27. --测试   
  28. SQL> insert into wdt_tset (test) values(11);   
  29. SQL> insert into wdt_tset (test) values(22);   
  30. SQL> insert into wdt_tset (test) values(33);   
  31.   
  32. SQL> update wdt_test set test = 88;   
  33. SQL> select * from wdt_log order by log_no; --结果只有1条记录、证明触发器只工作了一次   
  34.   
  35. 将触发器代码中的 for each row 解开注释,变成行触发,再继续测试。   
  36.   
  37. SQL> update wdt_test set test = 99;   
  38. SQL> select * from wdt_log order by log_no; --结果有3条记录、证明触发器按行工作了3次  

注释1:NVL方法

从两个表达式返回一个非 null 值。

语法

NVL(eExpression1, eExpression2)

参数
eExpression1, eExpression2

如果 eExpression1 的计算结果为 null 值,则 NVL( ) 返回 eExpression2。如果 eExpression1 的计算结果不是 null 值,则返回 eExpression1。eExpression1 和 eExpression2 可以是任意一种数据类型。如果 eExpression1 与 eExpression2 的结果皆为 null 值,则 NVL( ) 返回 .NULL.。

返回值类型

字符型、日期型、日期时间型、数值型、货币型、逻辑型或 null 值

说明

在不支持 null 值或 null 值无关紧要的情况下,可以使用 NVL( ) 来移去计算或操作中的 null 值。

更多Oracle相关信息见Oracle 专题页面 http://www.bkjia.com/topicnews.aspx?tid=12

相关内容