oracle - 防止无效删除操作的触发器
问题描述
set SERVEROUT ON;
create or replace TRIGGER tr_check_status
BEFORE DELETE ON supplier
for each row
declare
active_suppliers NUMBER;
begin
active_suppliers := 0;
select count(1) into active_suppliers
from supplier where supplier_id = :old.supplier_id
AND supplier_status='active';
IF(active_suppliers > 0) THEN
raise_application_error(-20001, 'Active supplier can not be deleted');
END IF;
end;
/
delete from supplier
where supplier_id=1;
我被要求创建一个触发器,如果supplier_status ='inactive',它将在供应商表中执行删除操作,并且如果supplier_status ='active'则不允许删除。
我在上面的代码中收到以下错误:
Error starting at line : 19 in command -
delete from supplier
where supplier_id=1
Error report -
ORA-04091: table DARSHAK.SUPPLIER is mutating, trigger/function may not see it
ORA-06512: at "DARSHAK.TR_CHECK_STATUS", line 6
ORA-04088: error during execution of trigger 'DARSHAK.TR_CHECK_STATUS'
解决方案
除非供应商可以有多行——这似乎是一个非常糟糕的主意——否则你不需要查询。只需使用:
begin
if (:old.supplier_status = 'active') then
raise_application_error(-20001, 'Active supplier can not be deleted');
endif;
end;
推荐阅读
- api - 如何通过 fastify 调用第三方 API 数据?
- r - 将风险比值(和置信区间)留在 metafor 包中的 forest() 中
- swift - 更新到 watchOS 6 后,Scrollviews 停止工作,有什么变化吗?
- java - 如何为不同的凭据和应用程序 URL 迭代 testng.xml 文件
- javascript - 将 props 传递给 react-quill 处理程序
- sql - 是否可以让表函数返回空行?
- swift - 关于 if 语句的预期声明
- php - Laravel 6.0 & Telescope - 长度必须是正整数
- amazon-s3 - 手动调用 lambda 函数模拟 S3 ObjectCreated 事件触发器?(php aws SDK)
- python-3.x - 如何利用 where 参数使用 fill_between