触发器
--创建临时缓存表
CREATE TABLE "GSINFO"."T1"
( "ID_PRODUCT_ORDER" VARCHAR2(50),
"ID_COLUMN" VARCHAR2(50),
"GRANT_TIME" DATE,
"GRANT_TYP" VARCHAR2(100),
"REVOKE_TIME" DATE,
"ID_USER_MODIFY" VARCHAR2(50),
"MODIFY_TIME" DATE
);
--创建trigger跟踪表
CREATE TABLE "GSINFO"."trigger_log"
( "TIME1" DATE,
"SID" NUMBER,
"SERIAL#" NUMBER,
"USERNAME" VARCHAR2(30),
"OSUSER" VARCHAR2(30),
"MACHINE" VARCHAR2(64),
"TERMINAL" VARCHAR2(30),
"PROGRAM" VARCHAR2(48),
"PARSING_SCHEMA_NAME" VARCHAR2(50),
"STATUS" VARCHAR2(30),
"CLIENT_IP" VARCHAR2(30),
"ID_PRODUCT_ORDER" VARCHAR2(30),
"ID_COLUMN" VARCHAR2(30),
"COL_DESC" VARCHAR2(50),
"ID_USER" VARCHAR2(30),
"USER_NAME" VARCHAR2(30),
"FULL_NAME" VARCHAR2(30),
"UKEY_NO" VARCHAR2(50),
"OLD_COLUMN" VARCHAR2(40)
);
---创建insert 存储过程
CREATE OR REPLACE PROCEDURE trigger_insert(s1 in varchar2, s2 in varchar2) as
begin
insert into trigger_log
select time1,
sid,
SERIAL#,
USERNAME,
OSUSER,
MACHINE,
TERMINAL,
PROGRAM,
PARSING_SCHEMA_NAME,
STATUS,
CLIENT_IP,
id_product_order,
id_column,
col_desc,
id_user,
user_name,
full_name,
ukey_no,
''
from (select sysdate time1,
s.SID,
s.SERIAL#,
s.USERNAME,
s.OSUSER,
s.MACHINE,
s.TERMINAL,
s.PROGRAM,
q.PARSING_SCHEMA_NAME,
'INSERT' STATUS,
sys_context('userenv', 'ip_address') CLIENT_IP
from sys.v_$sql q, sys.v_$session s
where s.audsid = (select userenv('SESSIONID') from dual)
and s.prev_sql_addr = q.address
AND s.PREV_HASH_VALUE = q.hash_value) ab,
(select a.id_product_order,
id_column,
col_desc,
id_user,
d.user_name,
full_name,
(select user from dual) currname,
ukey_no,
''
from t1 a,
t_product_user b,
t_column c,
t_user d,
t_customer cu,
t_ukey uk
where a.id_product_order = b.id_product_order
and a.id_column = c.id
and b.id_user = d.id
and cu.id = d.id_customer
and b.id_user = uk.id_user_use) cd
where ab.PARSING_SCHEMA_NAME = cd.currname
and id_product_order = s1
and id_column = s2
group by time1,
sid,
SERIAL#,
USERNAME,
OSUSER,
MACHINE,
TERMINAL,
PROGRAM,
PARSING_SCHEMA_NAME,
STATUS,
CLIENT_IP,
id_product_order,
id_column,
col_desc,
id_user,
user_name,
full_name,
ukey_no;
commit;
end;
---创建delete存储过程
CREATE OR REPLACE PROCEDURE trigger_delete(s1 in varchar2, s2 in varchar2) as
begin
insert into trigger_log
select time1,
sid,
SERIAL#,
USERNAME,
OSUSER,
MACHINE,
TERMINAL,
PROGRAM,
PARSING_SCHEMA_NAME,
STATUS,
CLIENT_IP,
id_product_order,
id_column,
col_desc,
id_user,
user_name,
full_name,
ukey_no,
''
from (select sysdate time1,
s.SID,
s.SERIAL#,
s.USERNAME,
s.OSUSER,
s.MACHINE,
s.TERMINAL,
s.PROGRAM,
q.PARSING_SCHEMA_NAME,
'DELETE' STATUS,
sys_context('userenv', 'ip_address') CLIENT_IP
from sys.v_$sql q, sys.v_$session s
where s.audsid = (select userenv('SESSIONID') from dual)
and s.prev_sql_addr = q.address
AND s.PREV_HASH_VALUE = q.hash_value) ab,
(select a.id_product_order,
id_column,
col_desc,
id_user,
d.user_name,
full_name,
(select user from dual) currname,
ukey_no
from t1 a,
t_product_user b,
t_column c,
t_user d,
t_customer cu,
t_ukey uk
where a.id_product_order = b.id_product_order
and a.id_column = c.id
and b.id_user = d.id
and cu.id = d.id_customer
and b.id_user = uk.id_user_use) cd
where ab.PARSING_SCHEMA_NAME = cd.currname
and id_product_order = s1
and id_column = s2
group by time1,
sid,
SERIAL#,
USERNAME,
OSUSER,
MACHINE,
TERMINAL,
PROGRAM,
PARSING_SCHEMA_NAME,
STATUS,
CLIENT_IP,
id_product_order,
id_column,
col_desc,
id_user,
user_name,
full_name,
ukey_no;
commit;
end;
----创建触发器
create or replace trigger t_trigger
after INSERT OR UPDATE or DELETE OF id_product_order, id_column on gsinfo.t_product_order_col
For each row
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
Begin
IF INSERTing THEN
insert into t1
select :new.id_product_order, :new.id_column, sysdate, '', '', '', ''
from t_product_order_col
where rownum = 1;
commit;
trigger_insert(:new.id_product_order, :new.id_column);
ELSIF deleting THEN
insert into t1
select :old.id_product_order, :old.id_column, sysdate, '', '', '', ''
from t_product_order_col
where rownum = 1;
commit;
trigger_delete(:old.id_product_order, :old.id_column);
ELSIF updating THEN
insert into trigger_log
select time1,
sid,
SERIAL#,
USERNAME,
OSUSER,
MACHINE,
TERMINAL,
PROGRAM,
PARSING_SCHEMA_NAME,
STATUS,
CLIENT_IP,
id_product_order,
id_column,
col_desc,
id_user,
user_name,
full_name,
ukey_no,
:old.id_column
from (select sysdate time1,
s.SID,
s.SERIAL#,
s.USERNAME,
s.OSUSER,
s.MACHINE,
s.TERMINAL,
s.PROGRAM,
q.PARSING_SCHEMA_NAME,
'UPDATE' STATUS,
sys_context('userenv', 'ip_address') CLIENT_IP
from sys.v_$sql q, sys.v_$session s
where s.audsid = (select userenv('SESSIONID') from dual)
and s.prev_sql_addr = q.address
AND s.PREV_HASH_VALUE = q.hash_value) ab,
(select a.id_product_order,
id_column,
col_desc,
id_user,
d.user_name,
full_name,
(select user from dual) currname,
ukey_no
from t1 a,
t_product_user b,
t_column c,
t_user d,
t_customer cu,
t_ukey uk
where a.id_product_order = b.id_product_order
and a.id_column = c.id
and b.id_user = d.id
and cu.id = d.id_customer
and b.id_user = uk.id_user_use) cd
where ab.PARSING_SCHEMA_NAME = cd.currname
and id_product_order = :new.id_product_order
and id_column = :new.id_column
group by time1,
sid,
SERIAL#,
USERNAME,
OSUSER,
MACHINE,
TERMINAL,
PROGRAM,
PARSING_SCHEMA_NAME,
STATUS,
CLIENT_IP,
id_product_order,
id_column,
col_desc,
id_user,
user_name,
full_name,
ukey_no;
commit;
end if;
end;
-----利用触发器避免数据重复插入
create or replace trigger trig_duplication_col
before insert on t_product_order_col
for each row
declare
v_count number(5);
PRAGMA AUTONOMOUS_TRANSACTION;
begin
select count(*)
into v_count
from t_product_order_col
where id_product_order = :new.id_product_order
and id_column = :new.id_column;
if (v_count > 0) then
raise_application_error(-20000, 'data is duplication');
end if;
end;
------测试
--查看123是否存在
select USER_NAME,
full_name
,ID_USER
,UKEY_NO
,ID_PRODUCT_ORDER
,CODE_PRODUCT_TPL
,PRODUCT_TPL_NAME,
col_desc,
'GSINFO'
from gsinfo.v_product_order_test
where id_product_order='16231' and id in(1,2,3)
---如果不存在插入
insert into gsinfo.t_product_order_col
select 16231, id, sysdate, 1, sysdate, null, null
from gsinfo.t_column c
where id in (1, 2, 3);
---跟踪trigger_log表
select * from trigger_log;
---删除
delete from (select * from gsinfo.t_product_order_col c where c.id_product_order='16231' and c.id_column in (1,2,3));
---跟踪trigger_log表
select * from trigger_log;
--update
update t_product_order_col set id_column=1 where id_product_order='16231' and id_column =2;
---跟踪trigger_log表
select * from trigger_log;
#######################################################
# #
# 功能:触发t_order_mon,如果更新改表数据,将旧数据存 #
# 放在新表,如果插入往该表插入新数据,新表同时也插入 #
# 时间:16、05、27 #
# 作者:陈 #
# #
#######################################################
--创建监控表
CREATE TABLE "GSINFO"."T_ORDER_MON_LOG"
( "TMO_ID" NUMBER(*,0),
"TMO_PARTITIONKEY" VARCHAR2(20),
"ORDER_NO" VARCHAR2(100) NOT NULL ENABLE,
"TMO_PRIPID" VARCHAR2(200),
"TMO_REGNO" VARCHAR2(100),
"TMO_ENTNAME" VARCHAR2(100),
"TMO_STARTDATE" DATE,
"TMO_LASTCHANGEDATE" DATE,
"TMO_ENDDATE" DATE,
"TMO_STATUS" VARCHAR2(50),
"TMO_UDT" DATE,
"TMO_DESCINFO" VARCHAR2(200),
"CHANGENO" VARCHAR2(200),
"TMO_ISREAD" VARCHAR2(50),
"TMO_CHANGE_GRADE" VARCHAR2(200),
"STATUS" VARCHAR2(30)
);
----创建触发器
create or replace trigger t_trigger_mon after INSERT OR UPDATE
on gsinfo.t_order_mon
For each row
--DECLARE PRAGMA AUTONOMOUS_TRANSACTION;
Begin
IF updating THEN
insert into t_order_mon_log values (:old.TMO_ID
,:old.TMO_PARTITIONKEY
,:old.ORDER_NO
,:old.TMO_PRIPID
,:old.TMO_REGNO
,:old.TMO_ENTNAME
,:old.TMO_STARTDATE
,:old.TMO_LASTCHANGEDATE
,:old.TMO_ENDDATE
,:old.TMO_STATUS
,:old.TMO_UDT
,:old.TMO_DESCINFO
,:old.CHANGENO
,:old.TMO_ISREAD
,:old.TMO_CHANGE_GRADE
,'UPDATE' );
ELSIF INSERTing THEN
insert into t_order_mon_log values (:new.TMO_ID
,:new.TMO_PARTITIONKEY
,:new.ORDER_NO
,:new.TMO_PRIPID
,:new.TMO_REGNO
,:new.TMO_ENTNAME
,:new.TMO_STARTDATE
,:new.TMO_LASTCHANGEDATE
,:new.TMO_ENDDATE
,:new.TMO_STATUS
,:new.TMO_UDT
,:new.TMO_DESCINFO
,:new.CHANGENO
,:new.TMO_ISREAD
,:new.TMO_CHANGE_GRADE
,'INSERT' );
end if;
end;
---测试
--对t_order_mon进行插入操作
insert into t_order_mon values (111,'11','11','11','11','11',to_date('2015/10/18','yyyy/MM/dd'),to_date('2015/10/18', 'yyyy/MM/dd'),to_date('9999/10/20','yyyy/MM/dd'),'1',to_date('9999/10/20','yyyy/MM/dd') ,'','ChangeNo401' ,1,'' );
--监控t_order_mon_log表
select * from t_order_mon_log
---对t_order_mon进行更新操作
update t_order_mon set tmo_lastchangedate=to_date('2016/10/18', 'yyyy/MM/dd') where tmo_id=111
--监控t_order_mon_log表
select * from t_order_mon_log