在触发器函数内对 table 的每一行执行查询

Execute a query for every row of a table inside a trigger function

我做了以下查询,它本身运行良好,但是当我在触发器函数中调用它时,我遇到了问题。

select insert_new_grade('title0', return3_6(0), return3_6(1), return3_6(2), s.code)
FROM "student" as s
where find_st(s.grade)>=5;

insert_new_grade 是一个函数,每次调用时都会在 table 中插入一个新行。

这是触发器:

CREATE OR REPLACE FUNCTION insert_d() 
RETURNS TRIGGER AS $$ 
BEGIN
    select insert_new_grade('title0', return3_6(0), return3_6(1), return3_6(2), s.code)
    FROM "student" as s
    where find_st(s.grade)>=5;

    return new;
END;
$$ LANGUAGE plpgsql; 

这是插入函数:

CREATE OR REPLACE FUNCTION insert_new_grade(title0 character(100), prof0 character(11), prof1 character(11)) 
RETURNS VOID AS $$
BEGIN
    INSERT INTO "d_table"(thes0, title, grade, prof, secProf) 
    VALUES (null, title0, null, prof0, prof1);
END
$$
LANGUAGE 'plpgsql';

有没有办法让查询在触发器函数内工作?如果我使用 perform 而不是 select,则插入函数没有结果。我读过游标,但我是 postgresql 的新手,我不知道该怎么做。有什么帮助吗?

我修改了你的触发函数:

检查你在 compose pgAdmin 中的触发函数,可见 raise info text

CREATE OR REPLACE FUNCTION insert_d() 
RETURNS TRIGGER AS $$ 
declare
    rec record;
BEGIN

    for rec in (select * from student s where find_st(s.grade)>=5) loop

        raise info 'LOOP code = %',rec.code;
        PERFORM insert_new_grade('title0', return3_6(0), return3_6(1), return3_6(2), rec.code);

    end loop;

    return new;
END;
$$ LANGUAGE plpgsql;