-- Trigger block. create or replace trigger tfwTgrUserAdd before insert -- Before or after some table action. on tfw_dash_user -- Target table of the action. for each row -- Row-level trigger. declare -- Block local variable(s) declaring. vch2UserRemark tfw_dash_user.remark%type; begin dbms_output.put_line('Trigger - Modify remark of new user data before inserting:'); dbms_output.put_line('U s e r I D : ['||:new.id||']'); dbms_output.put_line('Before modification: ['||:new.remark||']'); vch2UserRemark := :new.remark; vch2UserRemark := 'New inserted: '||vch2UserRemark; :new.remark := vch2UserRemark; dbms_output.put_line('After modification: ['||:new.remark||']'); end; / -- Block executing command?说明:
[typhoon@TFW-CENT6-LT ~]$ NLS_LANG="SIMPLIFIED CHINESE_CHINA.AL32UTF8" ORACLE_SID=███ sqlplus ███/███; SQL*Plus: Release 11.2.0.1.0 Production on █████ █ ████ ██ Copyright (c) 1982, 2009, Oracle. All rights reserved. 连接到: Oracle Database 11g Release 11.2.0.1.0 - 64bit Production SQL> variable vch2SysdateText varchar2(32); SQL> desc tfw_dash_user; 名称 是否为空? 类型 ----------------------------------------- -------- ---------------------------- ID NOT NULL NUMBER(8) NAME VARCHAR2(48) PASSWD VARCHAR2(96) STAT NUMBER(1) REMARK VARCHAR2(768) STATUS NUMBER(3) SQL> set serveroutput on; SQL> insert into tfw_dash_user values(tfw_seq_dash_user_id.nextval, 'user_'||tfw_seq_dash_user_id.currval, 'passwd_'||tfw_seq_dash_user_id.currval, 0, 'remark_'||tfw_seq_dash_user_id.currval, null); Trigger - Modify remark of new user data before inserting: U s e r I D : [10148] Before modification: [remark_10148] After modification: [New inserted: remark_10148] 已创建 1 行。 SQL> select remark from tfw_dash_user where id=10148; REMARK -------------------------------------------------------------------------------- New inserted: remark_10148 SQL>说明:
-- Trigger block. create or replace trigger tfwTgrUserEdit before update -- Before or after some table action. on tfw_dash_user -- Target table of the action. for each row -- Row-level trigger. begin dbms_output.put_line('Trigger - Modify remark of existing user data before updating:'); dbms_output.put_line('U s e r I D : ['||:new.id||']'); dbms_output.put_line('Before modification: ['||:new.remark||']'); :new.remark := 'Updated : '||:new.remark; dbms_output.put_line('After modification: ['||:new.remark||']'); end; / -- Block executing command?
SQL> update tfw_dash_user set name='用户_'||10148 where id=10148; Trigger - Modify remark of existing user data before updating: U s e r I D : [10148] Before modification: [New inserted: remark_10148] After modification: [Updated : New inserted: remark_10148] 已更新 1 行。 SQL> select remark from tfw_dash_user where id=10148; REMARK -------------------------------------------------------------------------------- Updated : New inserted: remark_10148 SQL> update tfw_dash_user set remark='备注_'||10148 where id=10148; Trigger - Modify remark of existing user data before updating: U s e r I D : [10148] Before modification: [备注_10148] After modification: [Updated : 备注_10148] 已更新 1 行。 SQL> select remark from tfw_dash_user where id=10148; REMARK -------------------------------------------------------------------------------- Updated : 备注_10148 SQL> exit; 从 Oracle Database 11g Release 11.2.0.1.0 - 64bit Production 断开 [typhoon@TFW-CENT6-LT typhoon]$