我目前是Firebird的新手,特别是触发器。通常我手动在脚本中完成此操作,但我真的很有兴趣用触发器创建它。
请先让我解释一下我的桌子。
***STOCK*** CODE NAME TOTAL GOOD BROKEN SERVICE ***DETAIL*** ID STOCK_CODE SERIAL ***BROKEN*** DETAIL_ID MARK ***SERVICE*** DETAIL_ID START_DATE END_DATE COST ***LOGS*** DETAIL_ID MARK START_DATE END_DATE COST
现在我的问题是:
如何在BROKEN中插入新记录后修改STOCK.GOOD和STOCK.BROKEN值?那将是:STOCK.GOOD-1,STOCK.BROKEN + 1.
如何在删除SERVICE中的当前记录之前将BROKEN和SERVICE中的所有记录插入LOGS?
我希望我的问题能被接受。
非常感谢你。
此致 阿卜迪
答案 0 :(得分:0)
以下是两个触发器:
CREATE TRIGGER bi_broken FOR broken
BEFORE INSERT
POSITION 0
AS
BEGIN
UPDATE stock SET good = good - 1, broken = broken + 1
WHERE code = (SELECT d.stock_code
FROM detail d WHERE d.id = NEW.detail_id);
END
CREATE TRIGGER bd_service FOR service
BEFORE DELETE
POSITION 0
AS
BEGIN
INSERT INTO logs (detail_id, mark, start_date, end_date, cost)
SELECT detail_id, (SELECT b.mark FROM broken b WHERE b.detail_id = OLD.detail_id),
start_date, end_date, cost
FROM service
WHERE detail_id = OLD.detail_id;
END
顺便说一下,将标记放入单独的表中的原因是什么?它属于STOCK,不是吗?