无法使用 SQL 中的触发器生成或更新 table 行
Failing to produce or update a table row using a trigger in SQL
我正在尝试在 Oracle SQL 12c 中创建触发器。
这个概念是我有两个 table,一个存储 table(Storage)
用于保存物品,一个交付 table(Supplier_Orders)
记录有多少项目(Units)
被传送到存储。触发器应该检测正在被传送到存储(new_product_id
)的项目是否已经存在于存储(product_id
),如果不存在,它会自动添加它,并要求用户输入一些强制值(barcode
和 sell_price
)。如果存在,它会通过添加更新 table正在交付的项目 (Units
) 占项目总数 (remaining_units
).
create or replace trigger SL_ORD_ADDER_TR
before insert on Supplier_Orders
for each row
begin
if :new.new_product_id in (select s.product_id from storage s where (:new.new_product_id=s.product_id)) then
update storage
set remaining_units = remaining_units + new.units
where new.new_product_id=product_id;
end if;
if :new.new_product_id not in(select s.product_id from storage s where (:new.new_product_id=s.product_id)) then
insert into storage(product_id,product_name,remaining_units,barcode,sell_price_per_unit) values(new.new_product_id,new.product_name,new.units,'&barcode','&sell price');
end if;
end;
产生的错误如下:
错误(5,5):PL/SQL:语句被忽略
错误(5,31):PLS-00405:此上下文中不允许子查询
错误(10,5):PL/SQL:语句被忽略
错误(10,34):PLS-00405:此上下文中不允许子查询
在您的代码中的 IF 条件之后,PL/SQL 不允许子查询。仅在 SQL.
中允许
您可以使用变量来检查“存储”中的“product_id” table 并确定其上的 IF 条件。
CREATE OR REPLACE TRIGGER SL_ORD_ADDER_TR
BEFORE INSERT on Supplier_Orders
REFERENCING
NEW AS new
OLD AS old
for each row
DECLARE
-- New variable
v_cnt number;
begin
select count(s.product_id) into v_cnt from storage s where s.product_id=:new.new_product_id;
if v_cnt>0
-- It exists, so updating
then
update storage
set remaining_units = remaining_units + :new.units
where product_id=:new.new_product_id;
end if;
if v_cnt=0
-- It not exists, so inserting
then
insert into storage(product_id,product_name,remaining_units,barcode,sell_price_per_unit)
values(:new.new_product_id,:new.product_name,:new.units,'&barcode','&sell price');
end if;
end;
我正在尝试在 Oracle SQL 12c 中创建触发器。
这个概念是我有两个 table,一个存储 table(Storage)
用于保存物品,一个交付 table(Supplier_Orders)
记录有多少项目(Units)
被传送到存储。触发器应该检测正在被传送到存储(new_product_id
)的项目是否已经存在于存储(product_id
),如果不存在,它会自动添加它,并要求用户输入一些强制值(barcode
和 sell_price
)。如果存在,它会通过添加更新 table正在交付的项目 (Units
) 占项目总数 (remaining_units
).
create or replace trigger SL_ORD_ADDER_TR
before insert on Supplier_Orders
for each row
begin
if :new.new_product_id in (select s.product_id from storage s where (:new.new_product_id=s.product_id)) then
update storage
set remaining_units = remaining_units + new.units
where new.new_product_id=product_id;
end if;
if :new.new_product_id not in(select s.product_id from storage s where (:new.new_product_id=s.product_id)) then
insert into storage(product_id,product_name,remaining_units,barcode,sell_price_per_unit) values(new.new_product_id,new.product_name,new.units,'&barcode','&sell price');
end if;
end;
产生的错误如下:
错误(5,5):PL/SQL:语句被忽略
错误(5,31):PLS-00405:此上下文中不允许子查询
错误(10,5):PL/SQL:语句被忽略
错误(10,34):PLS-00405:此上下文中不允许子查询
在您的代码中的 IF 条件之后,PL/SQL 不允许子查询。仅在 SQL.
中允许您可以使用变量来检查“存储”中的“product_id” table 并确定其上的 IF 条件。
CREATE OR REPLACE TRIGGER SL_ORD_ADDER_TR
BEFORE INSERT on Supplier_Orders
REFERENCING
NEW AS new
OLD AS old
for each row
DECLARE
-- New variable
v_cnt number;
begin
select count(s.product_id) into v_cnt from storage s where s.product_id=:new.new_product_id;
if v_cnt>0
-- It exists, so updating
then
update storage
set remaining_units = remaining_units + :new.units
where product_id=:new.new_product_id;
end if;
if v_cnt=0
-- It not exists, so inserting
then
insert into storage(product_id,product_name,remaining_units,barcode,sell_price_per_unit)
values(:new.new_product_id,:new.product_name,:new.units,'&barcode','&sell price');
end if;
end;