我有一个触发器:
CREATE OR REPLACE FUNCTION process_fillDerivedFromGenby() RETURNS TRIGGER AS $fillDerivedFromgenby$
DECLARE
prog varchar(255);
curent varchar(255);
BEGIN
SELECT u.iduseentity ,g.idCreatedEntity into prog,curent
FROM entity e
JOIN used u ON e.identity=u.iduseentity
JOIN activity a ON a.idactivity=u.idusedactivity
JOIN generatedby g ON g.idcreatoractivity=a.idactivity
WHERE g.idCreatedEntity =NEW.idCreatedEntity;
--raise notice 'curent: "%" prog by "%"', curent, prog;
INSERT INTO DERIVEDFROM VALUES(prog,curent);
return new;
END;
$fillDerivedFromgenby$ LANGUAGE plpgsql;
CREATE TRIGGER fillDerivedFromgenby AFTER INSERT ON GENERATEDBY
FOR EACH ROW EXECUTE PROCEDURE process_fillDerivedFromGenby();
它工作正常,但如果我的选择返回3行,我的触发器将只插入我的选择的最后一个值而不是3行。我试图用“for”做一个循环,但它不起作用。
有什么建议吗?
答案 0 :(得分:1)
我已经在other question中提供了答案。具有讽刺意味的是,问题不是重复,但答案是。
这将插入所有行而不经过变量:
CREATE OR REPLACE FUNCTION process_fillDerivedFrom_used() RETURNS TRIGGER AS $fillDerivedFrom_used$
BEGIN
INSERT INTO DERIVEDFROM (prog, current) -- or whatever the column names are
SELECT u.iduseentity as prog, g.idCreatedEntity as current
FROM entity e JOIN
used u
ON e.identity = u.iduseentity JOIN
activity a
ON a.idactivity = u.idusedactivity JOIN
generatedby g
ON g.idcreatoractivity = a.idactivity;
END;
答案 1 :(得分:0)
按“desc”命令和“限制1”。
SELECT u.iduseentity ,g.idCreatedEntity into prog,curent
FROM entity e
JOIN used u ON e.identity=u.iduseentity
JOIN activity a ON a.idactivity=u.idusedactivity
JOIN generatedby g ON g.idcreatoractivity=a.idactivity
WHERE g.idCreatedEntity =NEW.idCreatedEntity
ORDER BY u.iduseentity DESC 限制1;