为什么这个触发器不能正常工作?
Why doesn't this trigger work properly?
根据我对触发器及其工作原理的了解,我认为在我向其中插入值后,此触发器会将数据插入与我的 table 电话号码相关的关系中。我正在使用 dbms_random 创建一个随机的 5 位 usageID,它尚未在使用 table 中(或者至少这是我原以为它会做的)。
create or replace TRIGGER addPhoneLine
AFTER INSERT ON phoneNumber
REFERENCING NEW AS NEW
FOR EACH ROW
DECLARE
primNum varchar(12);
acctNum numeric(5);
NEWusageID_new varchar(5);
BEGIN
if :new.primaryNumber is not NULL then
select acctID, primaryNumber into acctNum, primNum
from account A
where A.primaryNumber = :new.primaryNumber;
select to_char(round(dbms_random.value(10000, 99999),0)) into
NEWusageID_new from dual
minus
select usageID from usage;
INSERT INTO acct_num VALUES
(acctNum, primNum, :new.phonenumber);
INSERT INTO phone_usage VALUES
(NEWusageID_new, :new.phonenumber);
end if;
END;
但是当我尝试插入 phoneNumber table:
时它会抛出以下错误
ORA-01403: no data found
ORA-06512: at "ADDPHONELINE", line 9
ORA-04088: error during execution of trigger 'ADDPHONELINE'
相关 table 的创建如下:
create table phoneNumber(phoneNumber varchar(12) PRIMARY KEY, primaryNumber varchar(12));
create table acct_num(acctID numeric(5) references ACCOUNT, primaryNumber varchar(12) references ACCOUNT, phoneNumber varchar(12) references phoneNumber);
create table phone_usage(usageID varchar(5) references USAGE, phoneNumber varchar(12) references PHONENUMBER)
根据您发布的错误描述,以下查询未返回任何行。您的 :new.primaryNumber 不正确,或者帐户 table.
中没有匹配的记录
Begin
select acctID, primaryNumber into acctNum, primNum
from account A
where A.primaryNumber = :new.primaryNumber;
exception when others then
insert your favorite logging method here logging out your :new.primaryNumber
end;
我在这里向您展示当 dbms_random 给出一个值 (NEWusageID_new) 时会发生什么,该值存在于用法中:
DECLARE
i NUMBER;
BEGIN
SELECT 15 INTO i FROM DUAL
MINUS
SELECT 15 FROM DUAL;
END;
ORA-01403: no data found
ORA-06512: in line 4
改为使用序列。
您的触发器基于 table phoneNumber
上的插入,并且在使用 SELECT INTO 时抛出错误 "No Data Found" 并且找不到任何信息插入。
所以问题一定出在这个语句上。
select acctID, primaryNumber into acctNum, primNum
from account A
where A.primaryNumber = :new.primaryNumber;
当此触发器处于活动状态时,您确定 :new.primaryNumber
存在于 account
table 中吗?
也许您只在插入完成后填充 account
table?
根据我对触发器及其工作原理的了解,我认为在我向其中插入值后,此触发器会将数据插入与我的 table 电话号码相关的关系中。我正在使用 dbms_random 创建一个随机的 5 位 usageID,它尚未在使用 table 中(或者至少这是我原以为它会做的)。
create or replace TRIGGER addPhoneLine
AFTER INSERT ON phoneNumber
REFERENCING NEW AS NEW
FOR EACH ROW
DECLARE
primNum varchar(12);
acctNum numeric(5);
NEWusageID_new varchar(5);
BEGIN
if :new.primaryNumber is not NULL then
select acctID, primaryNumber into acctNum, primNum
from account A
where A.primaryNumber = :new.primaryNumber;
select to_char(round(dbms_random.value(10000, 99999),0)) into
NEWusageID_new from dual
minus
select usageID from usage;
INSERT INTO acct_num VALUES
(acctNum, primNum, :new.phonenumber);
INSERT INTO phone_usage VALUES
(NEWusageID_new, :new.phonenumber);
end if;
END;
但是当我尝试插入 phoneNumber table:
时它会抛出以下错误ORA-01403: no data found
ORA-06512: at "ADDPHONELINE", line 9
ORA-04088: error during execution of trigger 'ADDPHONELINE'
相关 table 的创建如下:
create table phoneNumber(phoneNumber varchar(12) PRIMARY KEY, primaryNumber varchar(12));
create table acct_num(acctID numeric(5) references ACCOUNT, primaryNumber varchar(12) references ACCOUNT, phoneNumber varchar(12) references phoneNumber);
create table phone_usage(usageID varchar(5) references USAGE, phoneNumber varchar(12) references PHONENUMBER)
根据您发布的错误描述,以下查询未返回任何行。您的 :new.primaryNumber 不正确,或者帐户 table.
中没有匹配的记录Begin
select acctID, primaryNumber into acctNum, primNum
from account A
where A.primaryNumber = :new.primaryNumber;
exception when others then
insert your favorite logging method here logging out your :new.primaryNumber
end;
我在这里向您展示当 dbms_random 给出一个值 (NEWusageID_new) 时会发生什么,该值存在于用法中:
DECLARE
i NUMBER;
BEGIN
SELECT 15 INTO i FROM DUAL
MINUS
SELECT 15 FROM DUAL;
END;
ORA-01403: no data found
ORA-06512: in line 4
改为使用序列。
您的触发器基于 table phoneNumber
上的插入,并且在使用 SELECT INTO 时抛出错误 "No Data Found" 并且找不到任何信息插入。
所以问题一定出在这个语句上。
select acctID, primaryNumber into acctNum, primNum
from account A
where A.primaryNumber = :new.primaryNumber;
当此触发器处于活动状态时,您确定 :new.primaryNumber
存在于 account
table 中吗?
也许您只在插入完成后填充 account
table?