-1

我在 PL-SQL 中进行触发器以限制我的员工输入表单上部分/部门中的员工我得到 ORA-01403: no data found 。请任何人帮助我

create or replace trigger DEPT_STRENTH
  after insert on empmasterinfo
  for each row
DECLARE
  -- local variables here
  EMP_Count        NUMBER;
  MAX_Strength     NUMBER;
  V_Mainid         VARCHAR2(100);
  V_orgelementname VARCHAR2(100);
BEGIN

--taking value from form

 V_Mainid         := :new.mainid;
  V_orgelementname := :new.orgelementname;

--Comparing values with existing 

  select d.strength
    into MAX_Strength
    from dept_strength d 

-- Master table 


 where d.Mainid = V_Mainid
     and d.orgelementname = V_orgelementname;

  select count(e.employeeid)
    into EMP_Count

-- Master table 


 from empmasterinfo e 
   where e.emp_status = 0
     and e.Mainid = V_Mainid
     and e.orgelementname = V_orgelementname;

  if EMP_Count >= MAX_Strength then

    RAISE_APPLICATION_ERROR(-20101,
                            'Maximum Number of Employees in Department Reached');

  end if;

end DEPT_STRENTH;
4

1 回答 1

2

这是调试代码的练习。

第一步是看你写了什么。NO_DATA_FOUND 异常由不返回行的查询引发。您的触发器中有两个查询。但是聚合查询不会引发该异常,因为计数将返回 0。

所以只有一个查询可以抛出 ORA-01403,这清楚地表明您没有任何行dept_strength与您要插入的行匹配empmasterinfo。大概您认为该表中应该有行,在这种情况下,您需要重新访问您的事务逻辑。

无论如何,您可能应该这样做,因为试图在触发器中强制执行这种业务规则是一个严重的错误。它不能扩展,也不能在多用户环境中工作。

于 2013-11-23T09:11:55.447 回答