简体   繁体   English

我可以使用 DB2 的一组指令创建触发器吗?

[英]Can I create a trigger with a set of instruction with DB2?

I'm working with DB2 and I have to make a trigger that after a certain update on 'Disponibilita' has to do two differentes operation with the table 'Promozioni' here the schemas:我正在使用 DB2,我必须触发,在对“Disponibilita”进行特定更新后,必须对此处的表“Promozioni”执行两个不同的操作:

create table PROMOZIONI (
     PID char(5) not null primary key,
     Valore DEC(4,2) not null,
     NumProdotti INT not null DEFAULT 0 );

create table DISPONIBILITA (
     CodProdotto char(5) not null,
     CodNegozio char(5) not null,
     Quantita INT not null,
     PID char(5) references PROMOZIONI,
     primary key (CodProdotto, CodNegozio));

and this is the trigger that obviously doesn't work:这是显然不起作用的触发器:

Create or replace trigger AggiornaNumProdotti
After Update on Disponibilita 
referencing old as O new as N
for each row
update Promozioni p
SET NumProdotti=NumProdotti+1
Where N.PID is not null and N.PID=p.PID;
UPDATE Promozioni p2
SET NumProdotti=NumProdotti-1
WHERE O.PID is not null and O.PID=p2.PID;

is there any way to make a single trigger or i'm force to create two differentes ones for each specific instruction?有没有办法制作一个触发器,或者我必须为每个特定指令创建两个不同的触发器? Thanks a lot非常感谢

For more than one query you need a BEGIN and END对于多个查询,您需要BEGINEND

create table PROMOZIONI (
     PID char(5) not null primary key,
     Valore DEC(4,2) not null,
     NumProdotti INT not null DEFAULT 0 );


INSERT INTO PROMOZIONI VALUES ('1',1.2,0),
('2',1.2,0)
create table DISPONIBILITA (
     CodProdotto char(5) not null,
     CodNegozio char(5) not null,
     Quantita INT not null,
     PID char(5) references PROMOZIONI,
     primary key (CodProdotto, CodNegozio));

INSERT INTO DISPONIBILITA VALUES ('1','1',1,'1')
Create or replace trigger AggiornaNumProdotti
After Update on Disponibilita 
referencing old as O new as N
for each row
  BEGIN
update Promozioni p
SET NumProdotti=NumProdotti+1
Where N.PID is not null and N.PID=p.PID;
UPDATE Promozioni p2
SET NumProdotti=NumProdotti-1
WHERE O.PID is not null and O.PID=p2.PID;
END;
UPDATE  DISPONIBILITA SET PID = '2' WHERE PID = '1'
SELECT * FROM PROMOZIONI
PID PID VALORE价值 NUMPRODOTTI NUMPRODOTTI 产品
1 1个 1.20 1.20 -1 -1
2 2个 1.20 1.20 1 1个

fiddle小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM