ショコラ
PostgreSQL トリガーのサンプル
もっさん先輩
quantityテーブル にインサートとアップデートの処理後に quantity_history テーブルに値を残します。
DROP FUNCTION trg_quantity;
CREATE FUNCTION fnc_quantity() RETURNS trigger AS $$
DECLARE
buf RECORD;
BEGIN
SELECT INTO buf * FROM quantity_history WHERE id=NEW.id ORDER BY updt DESC;
if (buf.stock <> NEW.stock) THEN
INSERT INTO quantity_history SELECT * FROM quantity WHERE id=NEW.id
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER trg_quantity AFTER INSERT OR UPDATE ON quantity FOR EACH ROW EXECUTE PROCEDURE fnc_quantity();
以上