--创建唯一性触发器
CREATE OR REPLACE TRIGGER Tg_Completion_Test
BEFORE INSERT OR UPDATE ON bz_funds_voucher
FOR EACH ROW --行触发,没有本行则为语句级触发
DECLARE
Too_Many EXCEPTION;
PRAGMA EXCEPTION_INIT(Too_Many, -20001);
v_Exist_Count NUMBER := 0;
BEGIN
Select Count(*)
Into v_Exist_Count
From bz_funds_voucher t
Where t.document_code is not null
and t.territory_code = :new.territory_code
and t.document_code = :new.document_code ;
If v_Exist_Count > 0 Then
Raise_Application_Error(-20001, '数据重复存储!');
End If;
End;
CREATE OR REPLACE TRIGGER Tg_Bz_rent_payment_voucher
BEFORE INSERT OR UPDATE ON bz_rent_payment_voucher
FOR EACH ROW --行触发,没有本行则为语句级触发
DECLARE
Too_Many EXCEPTION;
PRAGMA EXCEPTION_INIT(Too_Many, -20002);
v_Exist_Count NUMBER := 0;
BEGIN
Select Count(*)
Into v_Exist_Count
From bz_rent_payment_voucher t
Where
t.collection_way not in ('转款', '优惠活动')
and t.create_date > to_date('2015-01-01', 'yyyy-mm-dd')
and t.serial_no is not null
and t.reserve_code is null
and t.serial_no = :new.serial_no ;
If v_Exist_Count > 0 Then
Raise_Application_Error(-20002, '同一流水收款单重复存储!');
End If;
End;

本文介绍两个Oracle数据库触发器实现防止数据重复插入或更新的例子。第一个触发器用于检查凭证文档是否存在重复记录,第二个触发器确保流水收款单据不被重复存储。通过行级触发器,使用条件判断和异常抛出机制来阻止不符合唯一性的数据进入数据库。
2137

被折叠的 条评论
为什么被折叠?



