有人可以帮我在 PL/SQL Oracle 中实现以下触发器吗?
Can someone help me implement the following trigger in PL/SQL Oracle?
所以,
我有 4 张桌子:
Suppliers( id_sup, name, city)
Products (id_prod, name, city)
Companies (id_co, name, city)
Deliveries (id_sup, id_prod, id_co)
我需要一个触发器,这样如果我想更新供应商的城市,如果该供应商的交货地点与它交付的产品和它交付的公司具有相同的城市,则不允许我这样做。
这是我迄今为止尝试过的方法,但它不起作用:
CREATE OR REPLACE TRIGGER secure_suppliers
BEFORE UPDATE ON Suppliers
BEGIN
IF UPDATING ('city') THEN
IF (suppliers.id_sup IN (SELECT id_sup FROM Deliveries) AND suppliers.city = (Select p.city From Products p INNER JOIN Deliveries d ON (p.id_prod = d.id_prod)) AND suppliers.city = (Select c.city From Companies c INNER JOIN Deliveries d ON (c.id_co = d.id_co))) THEN
RAISE_APPLICATION_ERROR(-20500, 'Can't update city!');
End if;
End;
您可以使用一个查询来查找 product
、supplier
和 company
是否在一个具有新值 supplier
的城市中,然后继续进行错误处理trigger
如下:
CREATE OR REPLACE TRIGGER secure_suppliers
For each row -- row level trigger
BEFORE UPDATE OF city ON Suppliers -- trigger will execute on update of column city only
DECLARE
cnt NUMBER := 0;
BEGIN
select count(1)
Into cnt
From deliveries d
Join companies c on d.id_co = c.id_co
Join products p on d.id_prod = p.id_prod
Where p.city = c.city
And p.city = :new.city;
If cnt > 0 then
RAISE_APPLICATION_ERROR(-20500, 'Can''t update city!');
End if;
End;
/
干杯!!
所以,
我有 4 张桌子:
Suppliers( id_sup, name, city)
Products (id_prod, name, city)
Companies (id_co, name, city)
Deliveries (id_sup, id_prod, id_co)
我需要一个触发器,这样如果我想更新供应商的城市,如果该供应商的交货地点与它交付的产品和它交付的公司具有相同的城市,则不允许我这样做。
这是我迄今为止尝试过的方法,但它不起作用:
CREATE OR REPLACE TRIGGER secure_suppliers
BEFORE UPDATE ON Suppliers
BEGIN
IF UPDATING ('city') THEN
IF (suppliers.id_sup IN (SELECT id_sup FROM Deliveries) AND suppliers.city = (Select p.city From Products p INNER JOIN Deliveries d ON (p.id_prod = d.id_prod)) AND suppliers.city = (Select c.city From Companies c INNER JOIN Deliveries d ON (c.id_co = d.id_co))) THEN
RAISE_APPLICATION_ERROR(-20500, 'Can't update city!');
End if;
End;
您可以使用一个查询来查找 product
、supplier
和 company
是否在一个具有新值 supplier
的城市中,然后继续进行错误处理trigger
如下:
CREATE OR REPLACE TRIGGER secure_suppliers
For each row -- row level trigger
BEFORE UPDATE OF city ON Suppliers -- trigger will execute on update of column city only
DECLARE
cnt NUMBER := 0;
BEGIN
select count(1)
Into cnt
From deliveries d
Join companies c on d.id_co = c.id_co
Join products p on d.id_prod = p.id_prod
Where p.city = c.city
And p.city = :new.city;
If cnt > 0 then
RAISE_APPLICATION_ERROR(-20500, 'Can''t update city!');
End if;
End;
/
干杯!!