Oracle 行触发与语句触发
Oracle 行触发与语句触发
Oracle 行触发与语句触发的区别
1、行触发器有 for each row子句。语句触发器没有for each row 子句。
2、行触发器,可以有 when 作为触发限制,可以使用new/old。语句触发器不能有when 作为触发限制。
3、行触发器:对应DML语句所影响到的表中的每一行,触发器都要执行一遍。
4、语句触发:对应DML语句所影响到的表中的所有行,触发器只执行一遍。
例子:
- --测试表
- create table wdt_test(test number(20));
- --日志表
- create table wdt_log(log_no number(20), log_date date);
- --触发器
- create or replace trigger buf_wdt_test
- before update on wdt_test
- --for each row
- declare
- ln_log_no wdt_log.log_no%type default 0;
- begin
- select nvl(max(log_no), 0) + 1
- into ln_log_no
- from wdt_log;
- insert into wdt_log
- (log_no,
- log_date)
- values
- (ln_log_no,
- sysdate);
- end;
- --测试
- SQL> insert into wdt_tset (test) values(11);
- SQL> insert into wdt_tset (test) values(22);
- SQL> insert into wdt_tset (test) values(33);
- SQL> update wdt_test set test = 88;
- SQL> select * from wdt_log order by log_no; --结果只有1条记录、证明触发器只工作了一次
- 将触发器代码中的 for each row 解开注释,变成行触发,再继续测试。
- SQL> update wdt_test set test = 99;
- 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
评论暂时关闭