我是Postgres的新手。我的问题是,当这些新行插入“源表”时,我想在“目标表”中插入一些新行。
我写了一个触发器来做到这一点但是每当插入源
触发功能:
CREATE OR REPLACE FUNCTION public.rec_insert()
RETURNS trigger AS
$BODY$
BEGIN
INSERT INTO target_table ("TIME","REGION","CITY","DISTRICT","Population")
SELECT NEW."TIME",NEW."REGION",NEW."CITY",NEW."DISTRICT",(100*(NEW."SAMPLES_MALE_Available")/(NULLIF((NEW."Total_AVAIL"-NEW."Female_AVAIL"),0)))
FROM source_table;
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql;
我的触发器是
CREATE TRIGGER ins_same_rec
AFTER UPDATE
ON source_table
FOR EACH ROW
EXECUTE PROCEDURE rec_insert();
答案 0 :(得分:0)
您必须省略FROM
。这就是原因,因为插入了许多行(表格的前一个状态中的每一行一个)
CREATE OR REPLACE FUNCTION public.rec_insert()
RETURNS trigger AS
$BODY$
BEGIN
INSERT INTO target_table ("TIME","REGION","CITY","DISTRICT","Population")
SELECT NEW."TIME",NEW."REGION",NEW."CITY",NEW."DISTRICT",(100*(NEW."SAMPLES_MALE_Available")/(NULLIF((NEW."Total_AVAIL"-NEW."Female_AVAIL"),0)));
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql;