我想制作触发器,它会计算不同表格的价格。
看起来像这样:
PART_PRICE(from PARTS) + TIME(from PARTS) * SALARY_PER_HOUR(from MECHANIC)
并且结果应该保存在PRICE(来自预订),表格保留包含连接到PARTS&的表格ID_PART和ID_MECHANIC。力学。
我真的不知道该怎么做,你能帮助我吗?
答案 0 :(得分:0)
首先应该在DDL语句上触发触发器。从问题中不清楚您想要触发哪个表更改。根据您的问题,如果ID_PART或ID_MECHANIC有任何更改,我认为您要更新预订表的价格栏。请找到以下代码。
CREATE TRIGGER trgAfterUpdate AFTER UPDATE ON RESERVATIONS
FOR EACH ROW
AS
declare part_id int;
declare mechanic_id int;
declare time long;
declare part_price decimal(10,2);
declare mechanic_sal decimal(10,2);
if NEW.ID_PART <> OLD.ID_PART
part_price = select PART_PRICE from parts where id_part=new.id_part;
time = select TIME from parts where id_part=new.id_part;
mechanic_sal = select SALARY_PER_HOUR from mechanic where id_mechanic=old.id_mechanic;
update RESERVATIONS set PRICE = part_price+time*mechanic_sal where ID_PART=NEW.ID_PART and ID_MECHANIC=OLD.id_mechanic;
else if NEW.ID_MECHANIC <> OLD.ID_MECHANIC
part_price = select PART_PRICE from parts where id_part=old.id_part;
time = select TIME from parts where id_part=old.id_part;
mechanic_sal = select SALARY_PER_HOUR from mechanic where id_mechanic=new.id_mechanic;
update RESERVATIONS set PRICE = part_price+time*mechanic_sal where ID_PART=OLD.ID_PART and ID_MECHANIC=NEW.id_mechanic;
GO