获取 returns 超过请求的行数错误
Getting fetch returns more than requested number of rows error
我需要将数据输入到 table 调用的事务中,但在插入之前对其进行验证。例如,我需要检查客户 table 中是否存在 customerno,如果不存在则引发错误。
然后,当我验证了所有数据后,我需要从事务 table 中获取数据并将其输入到订单和订单规格 table 中。当我 select 将所有客户编号输入变量 customerids 时,我不确定是否需要 select 中的位置。这是我用于触发器的代码:
create or replace trigger transaction_trigger
before insert
on transactions
for each row
declare
customerids number;
employeeids number;
inventoryids number;
begin
select cno into customerids from customers;
select employeeno into employeeids from employees;
select itemno into inventoryids from inventory;
if :NEW.cno not in (customerids) then
raise_application_error(-20001, 'customer does not exist in customer table');
elsif :NEW.employeeno not in (employeeids) then
raise_application_error(-20001, 'employee does not exist in employee table');
elsif :NEW.itemno1 not in (inventoryids) then
raise_application_error(-20001, 'inventory item 1 does not exist in inventory table');
elsif :NEW.itemno2 not in (inventoryids) then
raise_application_error(-20001, 'inventory item 2 does not exist in inventory table');
else
insert into orders
values (:NEW.orderno, :NEW.cno, :NEW.employeeno, :NEW.received, :NEW.shipped);
insert into orderspecs
values (:NEW.orderno, :NEW.itemno1, :NEW.quantity1);
insert into orderspecs
values (:NEW.orderno, :NEW.itemno2, :NEW.quantity2);
update inventory
set qty_on_hand = :NEW.quantity1 + :NEW.quantity2 + :NEW.quantity3
where itemno = :NEW.itemno1 OR itemno = :NEW.itemno2 OR itemno = :NEW.itemno3;
end if;
end;
但是当我 运行 我的函数将执行插入交易 table 使用此触发器验证并插入订单和订单规格时 table 我得到这个错误:
Error code:-1422. Error Message: ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at "SYSTEM.TRANSACTION_TRIGGER", line 6
ORA-04088: error during execution of trigger 'SYSTEM.TRANSACTION_TRIGGER'
PL/SQL procedure successfully completed.
据推测,您想从这样的事情开始:
select count(*) into num_customers
from customers
where cno = :new.cno;
select count(*) into num_employees
from employees
where employeeno = :new.employeeno;
select count(*) into num_inventory
from inventory
where itemno = :new.itemno;
然后您可以测试计数是否为 0
。
也就是说,通过 foreign key
约束而不是触发器可以更好地处理所有这些问题。正确声明的数据模型不需要在触发器中进行此检查。
这是您的代码:
declare
customerids number;
employeeids number;
inventoryids number;
begin
select cno into customerids from customers;
select employeeno into employeeids from employees;
select itemno into inventoryids from inventory;
您不能将查询的多行结果集影响到这样的数字。
为了您的目的,我认为几个 exists
查询就可以完成他的工作:
begin
if not exists (select 1 from customers where cno = :NEW.cno) then
raise_application_error(-20001, 'customer does not exist in customer table');
elsif not exists (select 1 from employees where employeeno = :NEW.employeeno)then
raise_application_error(-20001, 'employee does not exist in employee table');
...
我需要将数据输入到 table 调用的事务中,但在插入之前对其进行验证。例如,我需要检查客户 table 中是否存在 customerno,如果不存在则引发错误。 然后,当我验证了所有数据后,我需要从事务 table 中获取数据并将其输入到订单和订单规格 table 中。当我 select 将所有客户编号输入变量 customerids 时,我不确定是否需要 select 中的位置。这是我用于触发器的代码:
create or replace trigger transaction_trigger
before insert
on transactions
for each row
declare
customerids number;
employeeids number;
inventoryids number;
begin
select cno into customerids from customers;
select employeeno into employeeids from employees;
select itemno into inventoryids from inventory;
if :NEW.cno not in (customerids) then
raise_application_error(-20001, 'customer does not exist in customer table');
elsif :NEW.employeeno not in (employeeids) then
raise_application_error(-20001, 'employee does not exist in employee table');
elsif :NEW.itemno1 not in (inventoryids) then
raise_application_error(-20001, 'inventory item 1 does not exist in inventory table');
elsif :NEW.itemno2 not in (inventoryids) then
raise_application_error(-20001, 'inventory item 2 does not exist in inventory table');
else
insert into orders
values (:NEW.orderno, :NEW.cno, :NEW.employeeno, :NEW.received, :NEW.shipped);
insert into orderspecs
values (:NEW.orderno, :NEW.itemno1, :NEW.quantity1);
insert into orderspecs
values (:NEW.orderno, :NEW.itemno2, :NEW.quantity2);
update inventory
set qty_on_hand = :NEW.quantity1 + :NEW.quantity2 + :NEW.quantity3
where itemno = :NEW.itemno1 OR itemno = :NEW.itemno2 OR itemno = :NEW.itemno3;
end if;
end;
但是当我 运行 我的函数将执行插入交易 table 使用此触发器验证并插入订单和订单规格时 table 我得到这个错误:
Error code:-1422. Error Message: ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at "SYSTEM.TRANSACTION_TRIGGER", line 6
ORA-04088: error during execution of trigger 'SYSTEM.TRANSACTION_TRIGGER'
PL/SQL procedure successfully completed.
据推测,您想从这样的事情开始:
select count(*) into num_customers
from customers
where cno = :new.cno;
select count(*) into num_employees
from employees
where employeeno = :new.employeeno;
select count(*) into num_inventory
from inventory
where itemno = :new.itemno;
然后您可以测试计数是否为 0
。
也就是说,通过 foreign key
约束而不是触发器可以更好地处理所有这些问题。正确声明的数据模型不需要在触发器中进行此检查。
这是您的代码:
declare
customerids number;
employeeids number;
inventoryids number;
begin
select cno into customerids from customers;
select employeeno into employeeids from employees;
select itemno into inventoryids from inventory;
您不能将查询的多行结果集影响到这样的数字。
为了您的目的,我认为几个 exists
查询就可以完成他的工作:
begin
if not exists (select 1 from customers where cno = :NEW.cno) then
raise_application_error(-20001, 'customer does not exist in customer table');
elsif not exists (select 1 from employees where employeeno = :NEW.employeeno)then
raise_application_error(-20001, 'employee does not exist in employee table');
...