来自41.9.1. Triggers on Data Changes:
要更改要存储的行,可以直接在
NEW
中替换单个值并返回修改后的NEW
,或构建完整的新记录/行以返回。
我想做后者。我有一个SELECT
保证可以返回一行。如何使用?
答案 0 :(得分:1)
以下是示例:
CREATE TABLE sample (
id integer PRIMARY KEY,
sthg text NOT NULL,
ip inet
);
CREATE FUNCTION sample_trig() RETURNS trigger
LANGUAGE plpgsql AS
$$BEGIN
SELECT 42, 'yes!', '127.0.0.1'::inet INTO NEW;
RETURN NEW;
END;$$;
CREATE TRIGGER sample_trig
BEFORE INSERT ON sample FOR EACH ROW
EXECUTE PROCEDURE sample_trig();
INSERT INTO sample VALUES (1, 'new', NULL);
TABLE sample;
id | sthg | ip
----+------+-----------
42 | yes! | 127.0.0.1
(1 row)